简体   繁体   中英

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) 

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM