UIImage animation in UIImageView up and down (how it hangs) Loop

Hello, I have an image that I would like to move up and down (up 10 pixels and down 10 pixels) so that my image freezes. How can I do this with simple animation, thanks a lot!

+7
source share
4 answers

You can use Core Animation to animate the position of the views layer. If you set the animation to additive , you don’t have to worry about calculating a new absolute position, just a change (relative position).

 CABasicAnimation *hover = [CABasicAnimation animationWithKeyPath:@"position"]; hover.additive = YES; // fromValue and toValue will be relative instead of absolute values hover.fromValue = [NSValue valueWithCGPoint:CGPointZero]; hover.toValue = [NSValue valueWithCGPoint:CGPointMake(0.0, -10.0)]; // y increases downwards on iOS hover.autoreverses = YES; // Animate back to normal afterwards hover.duration = 0.2; // The duration for one part of the animation (0.2 up and 0.2 down) hover.repeatCount = INFINITY; // The number of times the animation should repeat [myView.layer addAnimation:hover forKey:@"myHoverAnimation"]; 

Since this uses Core Animation, you need to add QuartzCore.framework and #import <QuartzCore/QuartzCore.h> to your code.

+23
source

For Swift 3 and Swift 4:

 let hover = CABasicAnimation(keyPath: "position") hover.isAdditive = true hover.fromValue = NSValue(cgPoint: CGPoint.zero) hover.toValue = NSValue(cgPoint: CGPoint(x: 0.0, y: 100.0)) hover.autoreverses = true hover.duration = 2 hover.repeatCount = Float.infinity myView.layer.add(hover, forKey: "myHoverAnimation") 
+7
source

Try the following:

 CGRect frm_up = imageView.frame; frm_up.origin.y -= 10; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeats animations:^{ imageView.frame = frm_up; } completion:NULL ]; 
+5
source

For such a task, it is better to place the image in the UIView UIView , and then animate this sublayer. This lesson may give you some idea: http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial

0
source

All Articles