在UIImageView向上和向下(就像它的悬停)循环中动画UIImage

Yea*_*000 9 animation core-animation uiimageview ios ios6

你好,我有一个图像,我想上下移动(向上10像素,向下10像素),以便我的图像看起来像是在盘旋.我怎么能用简单的动画做到这一点非常感谢!!!

Dav*_*ist 25

您可以使用Core Animation为视图图层的位置设置动画.如果将动画配置为additive您将不必费心计算新的绝对位置,只需更改(相对位置).

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"];
Run Code Online (Sandbox Code Playgroud)

由于这是使用Core Animation,您需要将QuartzCore.framework添加#import <QuartzCore/QuartzCore.h>到您的代码中.


pab*_*ros 8

对于Swift 3和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")
Run Code Online (Sandbox Code Playgroud)


小智 5

试试这个:

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
];
Run Code Online (Sandbox Code Playgroud)