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
Setting a Rotation Point for Cgaffinetransformmakerotation Swift
iOS Notification Trigger: Fortnightly And/Or Quarterly
Ios- Check Whether Current Time Is Between Two Times or Not
How to Properly Add Child View Controller in iOS 8 with Swift
How to Create a Uiimage with Uibezierpath
iOS App Getting Throttled from Local Searches
Xcode Creates Wrong IPA Folder Structure
Measuring Time Accurately in Swift for Comparison Across Devices
Ios-8 and Later - Uitableview Inside an Uialertcontroller
Making Phone Calls on the Iphone
Rounding Uiimage and Adding a Border
Xcode:Why Launchoptions in Didfinishlaunchingwithoptions Always Nil
Instantiateviewcontrollerwithidentifier - Storyboard Id Set But Still Not Working
Carthage Build Failed Xcode 12 12A7209 Building
iOS - Custom Table Cell Not Full Width of Uitableview
How to Take Uiimage of Avcapturevideopreviewlayer Instead of Avcapturephotooutput Capture