Swift:CABasicAnimation 问题

Swift : CABasicAnimation Issue

您在下面看到的代码创建了一个 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 做的,并让它成为下一个创建的 CALayer 的 "x" 点。效果看起来像是一个连续的条,但实际上是一堆并排放置的 CALayer。好像还可以。

但是,问题是当创建下一个 CALayer 时,它看起来好像宽度设置不是 0(我需要它是 0),而是所有 CALayer 的长度。所以它只是弹出到视图中,然后开始动画。几个小时以来,我一直在努力找出原因,有人可以帮忙吗!?

UPDATE:我认为它加倍的原因是因为 layer.presentationLayer()?.bounds.width 没有被重置为 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

    }