Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 斯威夫特:动画卡拉耶_Ios_Swift_Animation_Calayer_Uilongpressgesturerecogni - Fatal编程技术网

Ios 斯威夫特:动画卡拉耶

Ios 斯威夫特:动画卡拉耶,ios,swift,animation,calayer,uilongpressgesturerecogni,Ios,Swift,Animation,Calayer,Uilongpressgesturerecogni,在下面的代码中,当用户按住屏幕时,我试图将CALayer从屏幕左侧设置为屏幕右侧的动画(longPressGestureRecognizer)。当用户抬起手指时,CALayer暂停 var l = CALayer() var holdGesture = UILongPressGestureRecognizer() let animation = CABasicAnimation(keyPath: "bounds.size.width") override func viewDidLoad()

在下面的代码中,当用户按住屏幕时,我试图将
CALayer
从屏幕左侧设置为屏幕右侧的动画(longPressGestureRecognizer)。当用户抬起手指时,
CALayer
暂停

var l = CALayer()
var holdGesture = UILongPressGestureRecognizer()
let animation = CABasicAnimation(keyPath: "bounds.size.width")

override func viewDidLoad() {
    super.viewDidLoad()
    setUpView()
}

func setUpView(){
    l.frame = CGRect(x: 0, y: 0, width: 0, height: 10)
    l.backgroundColor = UIColor.redColor().CGColor

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

func handleLongPress(sender : UILongPressGestureRecognizer){

    if(sender.state == .Began) { //User is holding down on screen
        print("Long Press Began")
        animation.fromValue = 0
        animation.toValue = self.view.bounds.maxX * 2
        animation.duration = 30
        self.view.layer.addSublayer(l)
        l.addAnimation(animation, forKey: "bounds.size.width")
    }
    else { //User lifted Finger
        print("Long press ended")
        print("l width: \(l.bounds.size.width)")
        pauseLayer(l)
    }
}

func pauseLayer(layer : CALayer){
    var pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
    layer.speed = 0.0
    layer.timeOffset = pausedTime
}
我有两个问题:

  • 当我在动画后打印
    CALayer
    的宽度时(当用户抬起手指时),其始终为0。我设置了宽度的动画,并将其展开,因此我不知道为什么它不给我
    CALayer
    的新宽度

  • 用户抬起手指,然后再次按住后,
    CALayer
    消失。我需要它保留在屏幕上,并创建另一个
    CALayer
    ,我不会以任何方式删除它,所以我不明白为什么它也会消失。我检查了该对象仍然存在的内存


  • 第2期更新:我相信要创建另一个
    CALayer
    ,我不能再添加层。我必须创建一个副本或创建一个可以添加图层的
    UIView
    。我仍然不明白为什么它会消失。

    基本上,当用户按下按钮时,您正在尝试调整图层的大小。以下是一个可用于调整给定图层大小的函数:

    如果要将原点固定到左侧,则需要首先设置图层的定位点:

    layer.anchorPoint = CGPointMake(0.0, 1);
    
    func resizeLayer(layer:CALayer, newSize: CGSize) {
    
        let oldBounds = layer.bounds;
        var newBounds = oldBounds;
        newBounds.size = size;
    
    
        //Ensure at the end of animation, you have proper bounds
        layer.bounds = newBounds
    
        let boundsAnimation = CABasicAnimation(keyPath: "bounds")
        positionAnimation.fromValue = NSValue(CGRect: oldBounds)
        positionAnimation.toValue = NSValue(CGRect: newBounds)
        positionAnimation.duration = 30
    } 
    
    在您的情况下,我不确定您将在哪里恢复暂停的图层。还要注意,每次用户点击时,都会在handleLongPress方法中添加一个新动画!这将产生不必要的影响。理想情况下,您只需要在第一次启动动画,然后再继续先前启动的暂停动画

    //Flag that holds if the animation already started..
    var animationStarted = false
    
    func handleLongPress(sender : UILongPressGestureRecognizer){
    
      //User is holding down on screen
      if(sender.state == .Began){
    
        if(animationStarted == false){
          let targetBounds =  CalculateTargetBounds() //Implement this to your need
          resizeLayer(layer, targetBounds)
          animationStarted = true
        }else {
          resumeLayer(layer)
        }
      }else {
        pauseLayer(layer)
      }
    }
    

    在设置动画之前,您应该将目标绑定到层。感谢您的回复!你到底是什么意思?你能回答这个问题吗?有人说我应该制作一个CALayer的副本@ShripadaI试着回答,看看-@originalalchemistry你太棒了!!看看我的评论@ShripadaThis真是太棒了!非常感谢。唯一的问题是,每次用户按下按钮时,我都要创建一个新层。下一层的原点将是上一层离开的地方,它看起来像一个连续的条,但它实际上是多个相邻放置的条。我这样做的原因是,我将它们存储到一个数组中,并可以在需要时删除它们。我需要跟踪每一次按下,以及控制条的长度,然后在需要时删除它(不过不要担心删除)。请帮助!!你确定这个代码是对的?您定义了一个函数
    resizeLayer
    ,该函数接受一个参数
    newSize
    ,但您没有在内部使用它。相反,您引用的是
    size
    ,我不确定这是从哪里来的。如果是因为您在UIView的子类中,那么即使如此,
    oldBounds
    newBounds
    始终是相等的,因为您是从旧设置新,然后从当前(即旧)设置新大小……还是我遗漏了什么?