ios - 如何使用 for-in-loop 在 CADisplayLink 中创建多个对象

标签 ios iphone swift for-in-loop cadisplaylink

所以我尝试使用 for in 循环创建 10 个按钮,并使用 CADisplayLink 使所有这 10 个按钮向下移动。问题是我的 CADisplayLink 只向下移动了一个按钮,而我希望它移动所有 10 个按钮。请帮忙!提前致谢!

var button: UIButton!



override func viewDidLoad() {
    super.viewDidLoad()

    var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    for index in 0...10 {

        var xLocation:CGFloat = CGFloat(arc4random_uniform(300) + 30)

        button = UIButton.buttonWithType(UIButtonType.System) as UIButton

        button.frame = CGRectMake(xLocation, 10, 100, 100)
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

        }



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func handleDisplayLink(displayLink: CADisplayLink) {

    for index in 0...10 {

        var buttonFrame = button.frame
        buttonFrame.origin.y += 1
        button.frame = buttonFrame
        if button.frame.origin.y >= 500 {
            displayLink.invalidate()
        }
    }
}


func buttonAction(sender: UIButton) {
    sender.alpha = 0
}

最佳答案

您只获得了对您在 viewDidLoad 中创建的 10 个按钮之一的引用。使用类型为按钮 [UIButton] 的数组来存储所有 10 个按钮,然后在您的 CADisplayLink 回调期间循环遍历每个按钮。

您的声明将是:

    var buttons: [UIButton] = Array(count: 10, repeatedValue: UIButton.buttonWithType(.System) as! UIButton)

任何时候您在原始代码中引用按钮时,使用数组索引运算符在 for 循环的当前索引处引用该按钮:

buttons[index]

这里是 Swift 数组和标准库引用的概述:

所以提供的代码是:

var buttons: [UIButton] = Array(count: 10, repeatedValue: UIButton.buttonWithType(.System) as! UIButton)

override func viewDidLoad() {
    super.viewDidLoad()

    var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    for index in 0...10 {

        var xLocation:CGFloat = CGFloat(arc4random_uniform(300) + 30)

        buttons[index].frame = CGRectMake(xLocation, 10, 100, 100)
        buttons[index].setTitle("Test Button \(index)", forState: UIControlState.Normal)
        buttons[index].addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(buttons[index])

        }



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func handleDisplayLink(displayLink: CADisplayLink) {

    for index in 0...10 {

        var buttonFrame = buttons[index].frame
        buttonFrame.origin.y += 1
        buttons[index].frame = buttonFrame
        if buttons[index].frame.origin.y >= 500 {
            displayLink.invalidate()
        }
    }
}


func buttonAction(sender: UIButton) {
    sender.alpha = 0
}

关于ios - 如何使用 for-in-loop 在 CADisplayLink 中创建多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28816287/

相关文章:

ios - 在tableView上方添加一个UIView并在UIView隐藏时开始滚动

iphone - xcode 中有类似 treeview 的控件吗?如果可能的话给我举个例子

ios - 推送通知 : no sound

ios - 插入核心数据后 TableView 行未更新

ios - Collection View 单元格未出现在 Collection View 中?

ios - 使用 Swift 2 从 Firebase 为自定义单元获取快照时出现问题

ios - UIButton 宽度是 UIView 宽度的 50% iOS

iphone - 应用程序不在后台模式下运行

iphone - 作为获取谓词的日期范围 - Core Data 之间的 NSDate 谓词

jquery - 当我们看不到另一个 div 时如何修复一个 div?