Swift:CABasicAnimation问题

时间:2016-01-13 05:58:38

标签: ios swift calayer cabasicanimation

您在下面看到的代码会创建一个CALayer(矩形形状),并在用户按住屏幕longPressGestureRecognizer时从左向右动画。当他们抬起手指时,CALayer停止动画,它被推入阵列,当他们再次按住屏幕时,会创建另一个CALayer。您可以直接在新项目中复制和粘贴代码:

 //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),而是所有CALayer的长度。所以它只是弹出视图然后开始动画。我一直想弄清楚为什么要几个小时,有人可以帮忙!?

更新:我认为其加倍的原因是因为layer.presentationLayer()?.bounds.width没有被重置为0所以它总是被添加到。

1 个答案:

答案 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

    }