2016-01-13 152 views
0

您在下面看到的代碼會創建一個CALayer(矩形形狀),並在用戶按住屏幕longPressGestureRecognizer時從左向右進行動畫處理。當他們擡起手指時,CALayer停止動畫,將其推入數組中,當它們再次保持在屏幕上時,將創建另一個CALayer。您可以複製和粘貼代碼直接在新項目:Swift:CABasicAnimation Issue

//Global Variables 
var layer: CALayer? 
var holdGesture = UILongPressGestureRecognizer() 
let animation = CABasicAnimation(keyPath: "bounds.size.width") 
var layerHolder = [CALayer]() 
var endPoint : CGFloat = 0 


func setUpView(){ 

    self.view.addGestureRecognizer(holdGesture) 
    holdGesture.addTarget(self, action:"handleLongPress:") 

} 


func handleLongPress(sender : UILongPressGestureRecognizer){ 
    print("inside handlelong press") 

    let newLayer = CALayer() 

    if(sender.state == .Began) { 

     newLayer.frame = CGRect(x: 0, y: 0, width: 0, height: 10) 
     newLayer.backgroundColor = UIColor.redColor().CGColor 

     if(layerHolder.count >= 1){ 
      newLayer.frame.origin.x = endPoint 
     } 

     animation.fromValue = endPoint 
     animation.toValue = self.view.bounds.width * 2 
     animation.duration = 30 
     self.view.layer.addSublayer(newLayer) 

     print("Long Press Began") 
     newLayer.addAnimation(animation, forKey: "bounds.size.width") 

     layer = newLayer 
    } 


    else { 
     print("Long press ended") 

     if let layer = layer { 
      print("width is: \(layer.presentationLayer()?.bounds.width)") 
      print("Total Width is \(self.view.bounds.width * 2)") 
      pauseLayer(layer) 
      layerHolder.append(layer) 

      endPoint += (layer.presentationLayer()?.bounds.width)! 
      print("endPoint is: \(endPoint)") 
     } 

    } 


} 


func pauseLayer(layer : CALayer){ 
    let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) 
    layer.speed = 0.0 
    layer.timeOffset = pausedTime 

} 

所以我想要做的就是第一個CALayer的,我與layer.presentationLayer()?.bounds.width做的寬度和有是的「X」點下一個創建的CALayer。效果看起來像是一個連續的酒吧,但它真的是一堆被放置在彼此旁邊的CALayers。這似乎很好。

但是,問題是當下一個CALayer被創建時,它看起來好像寬度不是0(我需要它是0),而是所有CALayers的長度。所以它只是彈出視圖,然後開始動畫。我一直在努力弄清楚爲什麼幾個小時,有人可以幫忙!?

UPDATE:我相信爲什麼它的倍增原因是因爲layer.presentationLayer()?.bounds.width沒有被重置爲0,所以它總是被添加到。

回答

0

使用此更新的代碼:

 //Global Variables 
    var layer: CALayer? 
    var holdGesture = UILongPressGestureRecognizer() 
    let animation = CABasicAnimation(keyPath: "bounds.size.width") 
    var nextXOffset = CGFloat(0.0) 


    func setUpView(){ 

     self.view.addGestureRecognizer(holdGesture) 
     holdGesture.addTarget(self, action:"handleLongPress:") 

    } 

    func handleLongPress(sender : UILongPressGestureRecognizer){ 

     if(sender.state == .Began) { 

      let newLayer = CALayer() 
      newLayer.frame = CGRect(x: nextXOffset, y: 0, width: 0, height: 10) 
      newLayer.backgroundColor = UIColor.redColor().CGColor 

      animation.fromValue = 0 
      animation.toValue = self.view.bounds.width * 2 - nextXOffset 
      animation.duration = 5 
      self.view.layer.addSublayer(newLayer) 

      print("Long Press Began") 
      newLayer.addAnimation(animation, forKey: "bounds.size.width") 

      layer = newLayer 
     } 
     else { 
      print("Long press ended") 

      if let layer = layer { 

       pauseLayer(layer) 
       layer.frame = layer.presentationLayer()!.frame 
       nextXOffset = CGRectGetMaxX(layer.frame) 
//    layer.removeFromSuperlayer() 
      } 
     } 
    } 

    func pauseLayer(layer : CALayer){ 
     let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) 
     layer.speed = 0.0 
     layer.timeOffset = pausedTime 

    } 
+0

而且他也再次女士們先生們! – OriginalAlchemist

+0

嘿Arun,有沒有辦法檢查圖層的右側是否觸及了屏幕的右側。基本上,當酒吧已滿(從左到右),我需要停止動畫並調用一個函數。 – OriginalAlchemist

+0

if nextXOffset> = self.view.bounds.width * 2 –