Ios 如何将CADisplayLink实现到for in循环中

Ios 如何将CADisplayLink实现到for in循环中,ios,swift,for-in-loop,cadisplaylink,animatewithduration,Ios,Swift,For In Loop,Cadisplaylink,Animatewithduration,因此,我使用for-in循环生成一组点对象,并使用UIView.animationWithDuration在屏幕上为它们设置动画;但是,我想用CADISPLAYLINK替换带有持续时间的动画,以便用户能够与点进行交互(使其可点击)。有人能告诉我这是怎么做到的吗?提前谢谢 @IBOutlet weak var timerScore: UILabel! // Set up timer varriables var count = 0 var timer:NSTimer = NSTimer()

因此,我使用for-in循环生成一组点对象,并使用UIView.animationWithDuration在屏幕上为它们设置动画;但是,我想用CADISPLAYLINK替换带有持续时间的动画,以便用户能够与点进行交互(使其可点击)。有人能告诉我这是怎么做到的吗?提前谢谢

@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
    }





}

您只需添加一个
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.
  }
}

我是否要删除UIView.animationWithDurations?是的。你的
moveDots
函数将计算并应用动画。那么你如何在循环中准确地实现它呢?根据经过的时间量计算你的位置增量。因为你正在线性地制作w.r.t.时间的动画,所以这是一个非常简单的
current.x=(end.x-start.x)/time\u passed
的计算。我对CADisplayLinks完全陌生,所以我同意