Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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 UIView animateWithDuration太快_Ios_Swift_Uiview_Swift2_Animatewithduration - Fatal编程技术网

Ios UIView animateWithDuration太快

Ios UIView animateWithDuration太快,ios,swift,uiview,swift2,animatewithduration,Ios,Swift,Uiview,Swift2,Animatewithduration,我使用了UIViewanimateWithDuration来增加下面代码中的shapeLayer.frame.size.height,但不管动画的持续时间如何,它都非常快。我发现有几篇帖子建议使用一些我已经做过的延迟时间,但是它仍然非常快,忽略了我设置的5秒持续时间 private let minimalHeight: CGFloat = 50.0 private let shapeLayer = CAShapeLayer() override func loadView() { su

我使用了
UIView
animateWithDuration
来增加下面代码中的
shapeLayer.frame.size.height
,但不管动画的持续时间如何,它都非常快。我发现有几篇帖子建议使用一些我已经做过的延迟时间,但是它仍然非常快,忽略了我设置的5秒持续时间

private let minimalHeight: CGFloat = 50.0  
private let shapeLayer = CAShapeLayer()

override func loadView() {  
super.loadView()

shapeLayer.frame = CGRect(x: 0.0, y: 0.0, width: view.bounds.width, height: minimalHeight)
shapeLayer.backgroundColor = UIColor(red: 57/255.0, green: 67/255.0, blue: 89/255.0, alpha: 1.0).CGColor
view.layer.addSublayer(shapeLayer)

  }

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}


override func viewDidAppear(animated: Bool) {
   delay(3)    {

    UIView.animateWithDuration(5.0) {
        self.shapeLayer.frame.size.height += 400.0
        }
    }
如何使动画在5秒内完成?

试试:

override func viewDidAppear(_ animated: Bool) {
    //You should not edit directly the frame here, or the change will be committed ASAP. Frame does not act like constraints.
    // create the new frame
    var newFrame = self.shapeLayer.frame
    newFrame.size.height += 400.0

    UIView.animate(withDuration: 5.0, delay: 3.0, options: .curveEaseOut, animations: {
        //assign the new frame in the animation block
        self.shapeLayer.frame = newFrame
    }, completion: { finished in

    })
}

也许你应该试试
cabasicanition

    let fromValue = view2.layer.bounds.height
    let toValue = view2.layer.bounds.height + 50
    CATransaction.setDisableActions(true) //Not necessary
    view2.layer.bounds.size.height = toValue
    let positionAnimation = CABasicAnimation(keyPath:"bounds.size.height")
    positionAnimation.fromValue = fromValue
    positionAnimation.toValue = toValue
    positionAnimation.duration = 1
    view2.layer.addAnimation(positionAnimation, forKey: "bounds")
  • 而不是将更改放在动画块中,而是在动画之前进行更改

  • 然后,在动画中,仅调用superView.layoutIfNeeded()


  • 它适用于我,参考:

    在我的情况下,问题是代码中的其他地方动画被禁用:

    [UIView SetAnimationEnabled:false]

    将此更改为true解决了以下问题:


    [UIView SetAnimationEnabled:true]

    我尝试了上面的代码,但动画仍然非常快动画非常快或没有动画?基本上,在不到一秒钟的时间内,它从原始位置移动到预期位置。这就是为什么我说动画非常快,也许解决方案就在这里。链接中的第二种方法是我一开始所做的。谢谢,它是有效的,但我真的想知道为什么
    animateWithDuration
    不起作用。因为您使用的是适用于UIView的动画方法。您正在尝试制作一个较低级别组件的CALayer witch动画。如果您能帮我看一下这个问题,我将非常感激,因为我还没有在这方面取得任何进展