【问题标题】:How to implement a CADisplayLink into a for in loop如何在 for in 循环中实现 CADisplayLink
【发布时间】:2015-03-02 14:03:39
【问题描述】:

因此,我使用 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
    }





}

【问题讨论】:

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


    【解决方案1】:

    您只想添加一个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_passed 的一个非常简单的计算。
    • 我对 CADisplayLinks 完全陌生,所以我很抱歉
    猜你喜欢
    • 2021-12-20
    • 2014-02-15
    • 2021-10-28
    • 1970-01-01
    • 2012-04-16
    • 2021-09-26
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多