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")
不是将更改放在动画块内,而是在动画之前进行更改。
然后,在动画中,只调用superView.layoutIfNeeded()
对我有用,参考:How do I animate constraint changes?
在我的例子中,问题是代码动画中的其他地方被禁用了:
[UIView setAnimationsEnabled:false];
将其更改为 true 解决了问题:
[UIView setAnimationsEnabled:true];
我已经使用 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")
不是将更改放在动画块内,而是在动画之前进行更改。
然后,在动画中,只调用superView.layoutIfNeeded()
对我有用,参考:How do I animate constraint changes?
在我的例子中,问题是代码动画中的其他地方被禁用了:
[UIView setAnimationsEnabled:false];
将其更改为 true 解决了问题:
[UIView setAnimationsEnabled:true];