2015-03-02 3 views
0

Итак, я создаю кучу объектов-точек, используя цикл for for, и я имитирую их по экрану с помощью UIView.animationWithDuration; однако я хочу заменить animationWithDurations на CADisplayLinks, чтобы пользователь мог взаимодействовать с точками (сделать их доступными). Может кто-нибудь, пожалуйста, покажите мне, как это делается? Заранее спасибо!!Как реализовать CADisplayLink в цикле for for

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





} 

ответ

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. 
    } 
} 
+0

Удалить ли я UIView.animationWithDurations? –

+0

Да. Функция 'moveDots' будет вычислять и применять анимации. –

+0

Итак, как вы реализуете его в цикле точно? –

 Смежные вопросы

  • Нет связанных вопросов^_^