UIView.animateWithDuration: animation always stops at original position

I added an ImageView in the middle of my view (no constraints set).

With this code I want to move this ImageView from its original position to a new one.

Unfortunately, it moves from the new to its original position - why?

let opts = UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(1, delay: 0, options: opts, animations: {
     self.ivSun.center.x += 100
}, completion: nil) 

Answers


Add this after completion of your animation.

self.ivSun.layer.position = self.ivSun.layer.presentationLayer().position

I have code that implemented in my game.

        CATransaction.begin()
        CATransaction.setCompletionBlock { () -> Void in
                    self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position
            }
        }
        var animation = CABasicAnimation(keyPath: "position")
        animation.duration = ballMoveTime
        animation.fromValue = NSValue(CGPoint: viewBall.layer.presentationLayer().position)
        animation.toValue = NSValue(CGPoint: CGPointMake(self.borderRight, self.borderMiddle))
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        viewBall.layer.addAnimation(animation, forKey: "transform")
        CATransaction.commit()

Change position value as you need.


Try this one.

func animateImage()
{
    UIView.animateWithDuration(3.0, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.imageView.center = CGPointMake(self.imageView.center.x+10, self.imageView.center.y+10)
    }, completion: nil)
}

Without knowing more about the context and related code it's difficult to give an answer on "why" it's snapping back to its original position, but as an alternative you could set the completion handler with whatever you'd like as the final position/coordinate.

Generally I use constraints on my widgets/views, and I animate their values (versus animating the widgets' frame, for example) ... with excellent results.


This is due to autolayout. Constraints need to be changed and updated in the the animation as seen below:

- (IBAction)moveAction:(id)sender {
    self.spaceConstraint.constant += 220;

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

var animation = CABasicAnimation(keyPath: "position")
animation.duration = ballMoveTime
animation.fromValue = NSValue(CGPoint: viewBall.layer.presentationLayer().position)
animation.toValue = NSValue(CGPoint: CGPointMake(self.borderRight, self.borderMiddle))
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
viewBall.layer.addAnimation(animation, forKey: "transform")
CATransaction.commit()

Need Your Help

QT and window.open

c++ qt window.open qtwebkit qwebpage

I am developing a browser, it will have support for tabs.

Browser using Java not Javascript as DOM manipulation language

java javascript browser

Is/was there a browser that used Java instead of Javascript as it's client-side programming language?