ios 如何使 UIView 动画序列重复和自动反转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14730460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make UIView animation sequence repeat and autoreverse
提问by B.S.
How to make this complex animation repeat and autoreverse? Is there any way to add options UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat to this animation sequence?
如何让这个复杂的动画重复和自动反转?有什么办法可以添加选项 UIViewAnimationOptionAutoreverse | UIViewAnimationOption 重复这个动画序列?
[UIView animateWithDuration:1.0f animations:^{
someView.frame = someFrame1;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f animations:^{
someView.frame = someFrame2;
} completion:nil];
}];
回答by Rob
To animation from point 1 to 2 to 3 to 2 to 1 and repeat, you can do use animateKeyframesWithDuration
in iOS 7 and later:
要动画从点 1 到 2 到 3 到 2 到 1 并重复,您可以animateKeyframesWithDuration
在 iOS 7 及更高版本中使用:
someView.frame = frame1;
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
someView.frame = frame2;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
someView.frame = frame3;
}];
} completion:nil];
If using auto-layout, you can animate the changing of the constraint constants:
如果使用自动布局,您可以动画约束常量的变化:
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
topConstraint.constant = 200;
leftConstraint.constant = 200;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
topConstraint.constant = 100;
leftConstraint.constant = 300;
[self.view layoutIfNeeded];
}];
} completion:nil];
Or, the approach with auto layout is to deactivate the constraints, and then you can animate using frame
values or what have you.
或者,使用自动布局的方法是停用约束,然后您可以使用frame
值或您拥有的内容制作动画。
In earlier versions of iOS, you can use CAKeyframeAnimation
, for example to animate along a path:
在较早版本的 iOS 中,您可以使用CAKeyframeAnimation
, 例如沿路径设置动画:
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100.0, 100.0)];
[path addLineToPoint:CGPointMake(200.0, 200.0)];
[path addLineToPoint:CGPointMake(100.0, 300.0)];
CAKeyframeAnimation *animatePosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animatePosition.path = [path CGPath];
animatePosition.duration = 1.0;
animatePosition.autoreverses = YES;
animatePosition.repeatCount = HUGE_VALF;
[self.someView.layer addAnimation:animatePosition forKey:@"position"];
You can do this with however many points you want. This also useful technique if you want to animate along a curved path (e.g. a circle or bezier curve).
您可以使用任意数量的点来执行此操作。如果您想沿曲线路径(例如圆形或贝塞尔曲线)设置动画,这也是一种有用的技术。
To just animate between two points, you can use animateWithDuration:delay:options:animations:completion:
, such as:
要在两点之间设置动画,您可以使用animateWithDuration:delay:options:animations:completion:
,例如:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
animations:^{
// do whatever animation you want, e.g.,
someView.frame = someFrame1;
}
completion:NULL];
This animates the movement of someView
from the starting frame to someFrame1
and back.
这为someView
从起始帧到someFrame1
和返回的移动设置动画。
By the way, using UIViewAnimationOptionCurveEaseInOut
in conjunction with UIViewAnimationOptionAutoreverse
and UIViewAnimationOptionRepeat
will give you a smoother effect as the animation reverses and repeats.
顺便说一下,当动画反转和重复时,UIViewAnimationOptionCurveEaseInOut
与UIViewAnimationOptionAutoreverse
和一起使用UIViewAnimationOptionRepeat
会给你一个更平滑的效果。
回答by Crashalot
Swift 4 for simple wiggle sequence:
Swift 4 用于简单的摆动序列:
func animateWiggle() {
// Set animation props
let scaleDelta = CGFloat(0.10)
// Set transforms
let wiggleOutHorizontally = CGAffineTransform(scaleX: 1.0 + scaleDelta, y: 1.0)
let wiggleOutVertically = CGAffineTransform(scaleX: 1.0, y: 1.0 + scaleDelta)
// Run animation sequence
UIView.animateKeyframes(withDuration: 1.0, delay: 0.0, options: [.autoreverse, .repeat], animations: {
// Animate wiggle horizontally
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5, animations: {
self.transform = wiggleOutHorizontally
})
// Animate wiggle vertically
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: {
self.transform = wiggleOutVertically
})
},
completion: nil)
}
回答by Johnny Rockex
UIImageView *pin = [UIImageView new];
[pin setFrame:CGRectMake(115, 160, 30, 60)];
[pin setBackgroundColor:[UIColor redColor]];
[holderView addSubview:pin];
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
// [pin setTransform:CGAffineTransformMakeTranslation(0, 20)];
[pin setFrame:CGRectMake(115, 200, 60, 100)];
}completion:^(BOOL finished){
}];