简体   繁体   English

UIView animateWithDuration太快了

[英]UIView animateWithDuration is too fast

I have used UIView animateWithDuration to increase the shapeLayer.frame.size.height in the code below, but regardless of the duration it animates very fast. 我已经使用UIView animateWithDuration来增加下面代码中的shapeLayer.frame.size.height ,但无论持续时间如何,它都会非常快。 A few posts that I found recommended using some delay time which I've done, but it still animates very quickly, ignoring the 5 seconds duration I set. 我发现的一些帖子推荐使用了我已经完成的一些延迟时间,但它仍然很快动画,忽略了我设置的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
        }
    }

How can I make the animation complete in 5 seconds? 如何在5秒内完成动画制作?

Try it : 试试吧 :

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

    })
}

Maybe you should try CABasicAnimation 也许你应该尝试CABasicAnimation

    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")
  1. Instead of putting the change inside the animation block, making the change before the animation. 而不是将更改放在动画块内,在动画之前进行更改。

  2. Then, in the animation, only call superView.layoutIfNeeded() 然后,在动画中,只调用superView.layoutIfNeeded()

It works for me, reference: How do I animate constraint changes? 它对我有用 ,参考: 如何设置约束变化的动画?

In my case the problem was that somewhere else in the code animations were disabled: 在我的情况下,问题是代码动画中的其他地方被禁用:

[UIView setAnimationsEnabled:false];

Changing this to true solved the issue: 将此更改为true可解决此问题:

[UIView setAnimationsEnabled:true];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM