【问题标题】:How to use a for-in-loop to make multiple objects in a CADisplayLink如何使用 for-in-loop 在 CADisplayLink 中创建多个对象
【发布时间】:2015-05-03 04:31:12
【问题描述】:

所以我尝试使用 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
}

}

【问题讨论】:

    标签: ios iphone swift for-in-loop cadisplaylink


    【解决方案1】:

    您只获得了对您在 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
    }
    

    【讨论】:

    • 我该怎么写?
    • 你有它工作吗?如果是,请将答案标记为正确,如果不是,我很乐意提供进一步的帮助。
    • 我看不懂,你能告诉我整件事情放在一起会是什么样子吗?
    • 更新了结果代码并提供了文档链接
    • 它在按钮[index] = UIButton.buttonWithType(UIButtonType.System) as UIButton 处崩溃
    猜你喜欢
    • 2011-04-12
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多