gpt4 book ai didi

ios - swift : CABasicAnimation Issue

转载 作者:行者123 更新时间:2023-11-30 13:47:16 26 4
gpt4 key购买 nike

您在下面看到的代码创建了一个 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 的位置。效果看起来像是一个连续的条,但实际上它是一堆彼此相邻放置的 CALayer。这看起来不错。

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

更新:我相信其加倍的原因是因为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

}

关于ios - swift : CABasicAnimation Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com