小编Jos*_*dan的帖子

如何使用swift获取UICollectionView中的单元格atIndex?

我正在以编程方式创建Collection视图.这是viewDidLoad func中的代码

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    layout.itemSize = CGSize(width: 90, height: 120)
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView!.dataSource = self
    collectionView!.delegate = self
    collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView!.layer.cornerRadius = 2
    collectionView!.backgroundColor = UIColor.redColor()
    self.containerView.addSubview(collectionView!)
Run Code Online (Sandbox Code Playgroud)

这些是我的集合视图功能

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 8
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview swift

20
推荐指数
3
解决办法
5万
查看次数

当我在Swift中销毁我的对象时,它不会释放我的RAM内存

这是一个测试,一个创建动作和一个破坏对象的动作,但是当我摧毁它时,我的RAM仍然使用相同数量的内存(大约30mb).

var missileImage: UIImageView!
weak var img: UIImage!

@IBAction func createImg(sender: AnyObject) {
    missileImage = UIImageView(frame: CGRectMake(CGFloat(arc4random() % 100), 200, 50, 30))
    img = UIImage(named: "house.jpg")
    missileImage.image = img
    missileImage.tag = 10001
    self.view.addSubview(missileImage)
}

@IBAction func destroyImg(sender: AnyObject) {
    self.view.viewWithTag(10001)?.removeFromSuperview()
    img = nil
    missileImage = nil
}
Run Code Online (Sandbox Code Playgroud)

memory iphone xcode swift

4
推荐指数
1
解决办法
6752
查看次数

我需要一个沿椭圆等量运动的方程

我正在编写类似行星绕太阳运动的程序,为了移动行星,我正在使用一个函数

CGPointMake(object.center.x + 1, sqrt(75*75*150*150 - 75*75*(object.center.x - 300)*(object.center.x - 300))/150 + 150)
Run Code Online (Sandbox Code Playgroud)

使用椭圆方程,其中 a = 150,b = 75,p = 300,q = 150,但是当物体接近 x = 450 左右时,它的速度会上升,我想这是因为 pitagora,因为它通过的路径是 c = sqrt( (x-x0)^2*(y-y0)^2)

我注意到我的 c 总是在 0.5 左右,但是当它到达 x 域的末尾时它上升到 0.8 所以我需要一个程序或数学解决方案来使对象以相同的速度围绕椭圆曲线移动

谢谢!

math objective-c swift

1
推荐指数
1
解决办法
1893
查看次数

如何阻止iOS应用程序向上滚动

我在Scroll View中创建了一个内容视图,使用常见的Autolayout和UIScrollView教程添加了约束.但是当没有内容时,如何阻止我的Scrollview向上滚动,视图向下滑动到内容,但是我想禁用向上滚动,我搜索了答案,但没有找到,请帮忙.

scrollview ios autolayout

1
推荐指数
1
解决办法
167
查看次数

如何在Button内设置cornerRadius到UIImage?

我正在使用渐变创建图像,我希望按钮具有cornerRadius

button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        button.frame = CGRectMake(self.view.frame.size.width/2 - button.frame.size.width, 100, 250, 50)
        button.layer.cornerRadius = 3
        button.setTitle("ViewThree", forState: UIControlState.Normal)
        button.addTarget(self, action: "ViewControllerAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(getImageWithGradient(UIColor(netHex:0x2d72cf).CGColor, bottom: UIColor(netHex:0x2d72cf).CGColor, size: CGSize(width: button.bounds.width, height: button.bounds.height), frame: button.bounds), forState: UIControlState.Normal)

func getImageWithGradient(top: CGColor, bottom: CGColor, size: CGSize, frame: CGRect) -> UIImage{
        var image = UIImage()
        UIGraphicsBeginImageContext(frame.size)
        var context = UIGraphicsGetCurrentContext()

        image.drawAtPoint(CGPointMake(0, 0))

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let locations:[CGFloat] = [0.0, 1.0]


        let gradient = CGGradientCreateWithColors(colorSpace,
            [top, bottom], locations)

        let startPoint = CGPointMake(frame.size.width / …
Run Code Online (Sandbox Code Playgroud)

image button cornerradius ios swift

0
推荐指数
1
解决办法
1527
查看次数

为什么从JSON中检索数据需要花费大量时间?

我的iPhone需要至少15-35秒才能完成这些循环.我主要在StackOverflow上学习了JSON,这就是人们这样做的方式.这些数组内部有3个元素,每个元素有一些文本和一个小图像

if let parseJSON = json{
    let succes = parseJSON["data"]
    let item = self.success["catalogue_products"] as! [[String: AnyObject]]

if item.isEmpty == false{
    for i in item {
        var categoryName = i["category_name"] as! String
        if self.category == nil{
            self.category = categoryName
            self.categories.append(self.category)
            self.categoryCount = 1
        }

        if self.category != categoryName{
            self.categoryCount += 1
            self.category = categoryName
            self.categories.append(self.category)
        }

        var deep = i["products"] as! [[String: AnyObject]]
        for i in deep{
            var product = ProductCatalogue()
            product.categoryName = categoryName
            product.id =  i["id"]
            println(product.id)
            product.name = …
Run Code Online (Sandbox Code Playgroud)

json swift

-2
推荐指数
1
解决办法
59
查看次数