所以我试图通过使用 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
}
}