我尝试使用 for-in-loop 生成一组 10 个按钮,然后使用 CADisplayLink 内部的 for-in-loop 为它们设置动画,但问题是只有一个按钮正在创建和动画。请帮忙!提前致谢!
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 - 1{
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 - 1{
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
}
}