我正在使用CADisplayLink
为10个不同的按钮设置动画,但这些按钮都聚集在一起。所以我的问题是如何实现延迟以使每个按钮在不同的时间生成动画,这样它们就不会全部聚集在一起。
var buttons:[UIButton] = Array()
override func viewDidLoad() {
super.viewDidLoad()
var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
for index in 0...10 - 1{
buttons.append(UIButton.buttonWithType(.System) as UIButton)
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
}
答案 0 :(得分:0)
要制作延迟动画,您只需使用UIView.animateWithDuration
功能而不是CADisplayLink
。例如
override func viewDidLoad() {
for index in 0...10 - 1{
buttons.append(UIButton.buttonWithType(.System) as UIButton)
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])
// Change this line to change the delay between the button animations.
let delay : NSTimeInterval = 1.0 * NSTimeInterval(index)
UIView.animateWithDuration(2.0, delay: delay, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
var buttonFrame = self.buttons[index].frame
buttonFrame.origin.y = 500
self.buttons[index].frame = buttonFrame
}, completion: { (finished) -> Void in
})
}
}