因此,我试图通过使用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个按钮中的一个。使用button[UIButton]类型的数组存储所有10个,然后在您的CADisplayLink回调期间循环遍历每个类型。
你的声明是:

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

当您在原始代码中引用某个按钮时,请使用array index操作符在for循环的当前索引处引用该按钮:
buttons[index]

Swift数组和标准库参考的概述如下:
Swift Language Guide: Collection Types
Swift Standard Library Reference: Arrays
所以提供的代码是:
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/

10-14 21:46
查看更多