0

を使用し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
}
4

1 に答える 1

0

遅延を伴うアニメーションを行うUIView.animateWithDurationには、 の代わりに function を使用できます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

        })

    }
}
于 2015-03-04T01:51:33.800 に答える