So I am trying to create 10 buttons by using a for in loop and make all those 10 buttons move down using a CADisplayLink. The problem is that my CADisplayLink only moves one of the buttons down and I want it to move all 10 of the buttons. Please Help! Thanks in advance!
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
}
}