簡體   English   中英

如何將CADisplayLink實現為for循環

[英]How to implement a CADisplayLink into a for in loop

所以我使用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
    }





}

您只想添加一個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.
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM