如何将CADisplayLink实现为for循环

时间:2015-03-02 14:03:40

标签: ios swift for-in-loop cadisplaylink animatewithduration

所以我使用for in循环生成一堆点对象,然后使用UIView.animationWithDuration在屏幕上为它们设置动画。但是,我想用CADisplayLinks替换animationWithDurations,以便用户能够与点进行交互(使它们可以点击)。有人可以告诉我这是如何完成的吗?在此先感谢!!

@IBOutlet weak var timerScore: UILabel!


// Set up timer varriables
var count = 0
var timer:NSTimer = NSTimer()

// Declare dot images
let RedDot = UIImage(named: "Red Dot") as UIImage?
let BlueDot = UIImage(named: "Blue Dot") as UIImage?
let YellowDot = UIImage(named: "Yellow Dot") as UIImage?
let GreenDot = UIImage(named: "Green Dot") as UIImage?





override func viewDidLoad() {
    timerScore.text = String(count)
    super.viewDidLoad()



    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)

    self.view.userInteractionEnabled = true



    // Declare delay counter
    var delayCounter:Int = 100000
    var durationCounter:Double = 0

    // loop for 1000 times
    for loopNumber in 0...100 {

        // set up some constants for the animations
        let dotDuration:Double = 4 - durationCounter
        let redDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
        let blueDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
        let yellowDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
        let greenDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000

        let options = UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.LayoutSubviews

        //set up some constants for the dots
        let redSize:CGFloat = 54
        let redYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        let blueSize:CGFloat = 54
        let blueYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        let yellowSize:CGFloat = 54
        let yellowYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        let greenSize:CGFloat = 54
        let greenYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        // create the dots and add them to the view
        let redDot = UIButton.buttonWithType(UIButtonType.System) as UIButton
        redDot.setBackgroundImage(RedDot, forState: UIControlState.Normal)
        redDot.frame = CGRectMake(0-redSize, redYPosition, redSize, redSize)
        redDot.addTarget(self, action: "redDotTapped:", forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(redDot)


        let blueDot = UIButton.buttonWithType(UIButtonType.System) as UIButton
        blueDot.setBackgroundImage(BlueDot, forState: UIControlState.Normal)
        blueDot.frame = CGRectMake(0-blueSize, blueYPosition, blueSize, blueSize)
        blueDot.addTarget(self, action: "blueDotTapped:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(blueDot)


        let yellowDot = UIButton.buttonWithType(UIButtonType.System) as UIButton
        yellowDot.setBackgroundImage(YellowDot, forState: UIControlState.Normal)
        yellowDot.frame = CGRectMake(0-yellowSize, yellowYPosition, yellowSize, yellowSize)
        yellowDot.addTarget(self, action: "yellowDotTapped", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(yellowDot)


        let greenDot = UIButton.buttonWithType(UIButtonType.System) as UIButton
        greenDot.setBackgroundImage(GreenDot, forState: UIControlState.Normal)
        greenDot.frame = CGRectMake(0-greenSize, greenYPosition, greenSize, greenSize)
        greenDot.addTarget(self, action: "greenDotTapped", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(greenDot)






        // -----------WANT TO REPLACE THESE ANIMATIE WITH DURATIONS WITH CADISPLAYLINKS!!!-------------------

        UIView.animateWithDuration(dotDuration , delay: redDelay, options: options, animations: {

            redDot.frame = CGRectMake(675, redYPosition, redSize, redSize)

            }, completion: nil)



        UIView.animateWithDuration(dotDuration , delay: blueDelay, options: options, animations: {

            blueDot.frame = CGRectMake(675, blueYPosition, blueSize, blueSize)

            }, completion: { animationFinished in blueDot.removeFromSuperview() })



        UIView.animateWithDuration(dotDuration , delay: yellowDelay, options: options, animations: {

            yellowDot.frame = CGRectMake(675, yellowYPosition, yellowSize, yellowSize)

            }, completion: { animationFinished in yellowDot.removeFromSuperview() })



        UIView.animateWithDuration(dotDuration , delay: greenDelay, options: options, animations: {

            greenDot.frame = CGRectMake(675, greenYPosition, greenSize, greenSize)

            }, completion: { animationFinished in greenDot.removeFromSuperview() })










        durationCounter+=0.05
        delayCounter+=50000
    }





}

1 个答案:

答案 0 :(得分:0)

您只想添加一个CADisplayLink

var displayLink: CADisplayLink!
override func viewDidLoad() {
  // ...
  for loopNumber in 0...100 {
    // ...
  }
  displayLink = CADisplayLink(target: self, selector: Selector("moveDots"))
  displayLink.frameInterval = frameInterval
  displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
}

func moveDots() {

  for dot in self.subviews {
    // calculate its new position based on your
    // time delta (displaylink.timestamp) and move it.
  }
}