我在 View 底部放置了一个按钮,我正在尝试为其以编程方式添加的约束设置动画以从屏幕的中间移动到屏幕的最右侧(基本上直到按钮不再可见)。持续时间最多需要 2 分钟,并且用户应该能够在动画播放时点击按钮。
- 动画显示,但动画在半秒内完成。
这是我目前的代码
func animateButton() {
let leftMarginConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.tapToSaveButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)
let rightMarginConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.tapToSaveButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
tapToSaveButton.addTarget(self, action: #selector(CheckOutViewController.buttonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.tapToSaveButton.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(tapToSaveButton)
self.view.addConstraints([leftMarginConstraint, rightMarginConstraint])
self.view.layoutIfNeeded()
self.constraintAnimation(120, delay: 1, animatedConstraint: leftMarginConstraint, andOtherConstraint: rightMarginConstraint, finalConstantValue: 500, options: [UIViewAnimationOptions.AllowUserInteraction, UIViewAnimationOptions.CurveEaseOut])
func constraintAnimation (duration:NSTimeInterval, delay:NSTimeInterval, animatedConstraint: NSLayoutConstraint, andOtherConstraint: NSLayoutConstraint, finalConstantValue: CGFloat, options:UIViewAnimationOptions) {
UIButton.animateWithDuration(duration, animations: {
animatedConstraint.constant = finalConstantValue
andOtherConstraint.constant = finalConstantValue
})
}
func buttonPressed(sender : UIButton)
{
print("Tapped")
}
最佳答案
试试这个:
animatedConstraint.constant = finalConstantValue
andOtherConstraint.constant = finalConstantValue
UIView.animateWithDuration(duration, animations: {
self.view.layoutIfNeeded()
})
关于ios - 从 View 中心向右动画 UIButton 的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37059557/