首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift : CABasicAnimation发行

Swift : CABasicAnimation发行
EN

Stack Overflow用户
提问于 2016-01-13 05:58:38
回答 1查看 1.8K关注 0票数 0

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

代码语言:javascript
复制
 //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“点。效果看起来就像一个连续的酒吧,但实际上它的一堆CALayers放在彼此旁边。这看起来挺好的。

但是,问题是当创建下一个CALayer时,它看起来好像宽度不是设置为0(我需要它是0),而是所有CALayers的长度。所以它突然出现在视野中,然后开始动画。我已经想了好几个小时了,有人能帮忙吗?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-13 06:49:23

使用此更新代码:

代码语言:javascript
复制
        //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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34759415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档