簡體   English   中英

UIView animateWithDuration太快了

[英]UIView animateWithDuration is too fast

我已經使用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

    })
}

也許你應該嘗試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. 而不是將更改放在動畫塊內,在動畫之前進行更改。

  2. 然后,在動畫中,只調用superView.layoutIfNeeded()

它對我有用 ,參考: 如何設置約束變化的動畫?

在我的情況下,問題是代碼動畫中的其他地方被禁用:

[UIView setAnimationsEnabled:false];

將此更改為true可解決此問題:

[UIView setAnimationsEnabled:true];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM