0

I am using a CADisplayLink to animate 10 different buttons but the buttons are all clumped together. So my question is how can I implement a delay to make each button animate at a different time so they are not all clumped together.

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
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Garret Kaye
  • 2,412
  • 4
  • 21
  • 45
  • 2
    Why don't you use ordinary view animation? It's simpler and you get a built-in delay parameter. – matt Mar 04 '15 at 01:44

1 Answers1

0

To do animation with delay, you can just use UIView.animateWithDuration function instead of a CADisplayLink. For example

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

        })

    }
}
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • Thats what I had originally did but now you can not tap the buttons that are being animated without a CADisplayLink – Garret Kaye Mar 04 '15 at 03:06
  • 1
    Tapping on animating buttons is tricky, but possible. Have you posted a question about that? The problem is you need to do your own hit testing since the button thinks it's already at its final location. – Rob Napier Mar 04 '15 at 03:22
  • You can check this answer to enable user interaction on an animating UIButton. http://stackoverflow.com/questions/8346100/uibutton-cant-be-touched-while-animated-with-uiview-animatewithduration – rakeshbs Mar 04 '15 at 03:24