Animate UIImage in UIImageView Up & Down (Like it's hovering) Loop

前端 未结 4 1335
情深已故
情深已故 2021-02-04 13:45

Hello I have an image that I\'d like to move up and down (Up 10 pixels and down 10 pixels) so that my image appears to be hovering. How can I do this with simple animation thank

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 14:18

    You could use Core Animation to animate the position of the views layer. If you configure the animation to be additive you won't have to bother calculating the new absolute position, just the 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 is using Core Animation you will need to add QuartzCore.framework and #import into your code.

提交回复
热议问题