Animation Blocks Resets to Original Position After Updating Text

Animation Blocks resets to original position after updating text

This problem can be caused by having Auto Layout set on the UIView. Strictly speaking, if you're using Auto Layout, then you shouldn't animate the absolute position of objects -- you should animate their constraints instead.

Changing the label text once your animation is underway triggers a layout refresh, and iOS shuffles everything around to comply with the original view constraints. (I suspect this is a behavioural change from iOS7).

Quick fix: un-check Auto Layout on the View, and this should work as expected.

animation: stop reset position of an element (swift)

In comment of my question , @Hardik Shekhat told to use :

self.tfUser.translatesAutoresizingMaskIntoConstraints = true

it works for me !

Reset view to original position after animation

clearAnimation(); does not reset your animations, it just stops them and removes them from the animation queue. To undo your animations you need to actually undo them. So, for your code block you will need to call rl2.animate().translationX(0).translationY(0).setDuration(2000); to move the view back to its original position.

Text Field Returns to Original Location after Running Animation

If you have auto layout turned on, you can't manipulate the frame like that. You need a reference to a NSLayoutConstraint and update the constant.

like this:

myXConstraint.constant = originalX + 500;

Edit -- and then your animate block should look like this:

[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];

Reset label position before animation is applied (swift3)

Try this out:

 //To store the default value of the position of the label
var yPosLabel : CGFloat!
override func viewDidLoad() {
super.viewDidLoad()

yPosLabel = self.myLabel.frame.origin.y
// Do any additional setup after loading the view.
}

@IBAction func reset(_ sender: Any) {
//Just in case you are using AutoLayout
self.view. translatesAutoresizingMaskIntoConstraints = true
let newFrame = CGRect(x:self.label.frame.origin.x, y:yPosLabel,
width:self.label.frame.size.width, height:self.label.frame.size.height)
self.label.frame = newFrame
}

Hope this helps. Happy Coding.

iOS animation doesn't reset when replayed repeatedly

I can reproduce the behavior you describe. But if I call removeAllAnimations() on the layer of the view that is moving, the problem goes away.

@IBAction func didTapButton(_ sender: Any) {
animatedView.layer.removeAllAnimations()

centerYConstraint.constant = 0
view.layoutIfNeeded()

centerYConstraint.constant = 30
UIView.animate(withDuration: 2) {
self.view.layoutIfNeeded()
}
}

Note, I'm not removing the animation from the superview (because that still manifests the behavior you describe), but rather of the view that is moving.



Related Topics



Leave a reply



Submit