2016-05-05 4 views
0

У меня есть кнопка, расположенная внизу представления, и я пытаюсь оживить ее программно добавленные ограничения, чтобы перейти от середины к самому правильному экрана (в основном до тех пор, пока кнопка больше не будет видна). Продолжительность должна составлять до 2 минут, и пользователь должен иметь возможность нажимать кнопку во время анимации.Анимация ограничений UIButton от центра зрения вправо

  • Анимация показывает, но анимация завершается в течение половины секунды.

Это код, у меня есть на данный момент

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") 
} 

ответ

2

Попробуйте это:

animatedConstraint.constant = finalConstantValue 
     andOtherConstraint.constant = finalConstantValue 
UIView.animateWithDuration(duration, animations: { 
     self.view.layoutIfNeeded() 

    }) 
0

Попробуйте это:

view.layoutIfNeeded() 
UIView.animateWithDuration(duration) { [unowned self] in 
    constraint.constant = value 
    self.view.setNeedsLayout() 
}