AntonSack AntonSack - 1 year ago 99
iOS Question

UIView.animateWithDuration: animation always stops at original position

I added an

in the middle of my view (no constraints set).

With this code I want to move this
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: { += 100
}, completion: nil)

Answer Source

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

Change position value as you need.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download