你好,我有我想向上和向下移动(最多10个像素,向下10个像素),使我的形象似乎徘徊的图像。 我怎样才能做到这一点用简单的动画,非常感谢!
Answer 1:
你可以使用的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"];
由于这是使用的Core Animation,您将需要QuartzCore.framework并添加#import <QuartzCore/QuartzCore.h>
到你的代码。
Answer 2:
为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")
Answer 3:
试试这个:
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
];
Answer 4:
对于这样的任务,这将是最好的地方在一个子图像UIView
,然后动态显示子帧。 本教程可以给你一些想法: http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial
文章来源: Animate UIImage in UIImageView Up & Down (Like it's hovering) Loop