ios - 从 View 中心向右动画 UIButton 的约束

标签 ios iphone xcode swift

我在 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/

相关文章:

objective-c - uitableview单元格突出显示

ios - 交付iOS的自动续订订阅

ios - 禁用自动隐藏字幕 ios Storyboard

iphone - 当我尝试从最近的应用程序栏终止我的应用程序时,我的模拟器崩溃了

iphone - 如果 NSString stringWithContentsOfFile 已被弃用,那么它的替代品是什么?

ios - UITableViewCell 阴影

xcode - 如何检索用户联系人并将其显示在 UITableView 中?

ios - Apple设备中的Google Analytics(分析)跟踪问题

ios - 我想在应用程序每 10 次快速打开时显示一条警报

objective-c - UIScrollView:更准确/精确的 contentOffset 值?