ios UIView 块动画从左到右和从后到左移动视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8675859/
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
UIView block animation move view left to right and back to left
提问by the Reverend
I wan't to animate a view that's shaped as an arrow, and I want it to animate from left to right, left to right and so on..
我不想动画一个箭头形状的视图,我希望它从左到右,从左到右等等动画。
This code below is not working, I'm guessing I need a path, but don't know how to do that in a UIView block animation.
下面的这段代码不起作用,我猜我需要一个路径,但不知道如何在 UIView 块动画中做到这一点。
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
controller.view.frame = frame1;
controller.view.frame = frame2;
} completion:^(BOOL finished) {
}];
回答by Kjuly
You can use UIViewAnimationOptionAutoreverse
option, try the code below:
您可以使用UIViewAnimationOptionAutoreverse
选项,请尝试以下代码:
UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f, 200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView];
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^{
[testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
}
completion:nil];
[testView release];
It'll repeat move from right to left, left to right, ... smoothly.
它会重复从右到左,从左到右,...顺利移动。
回答by Paul.s
Assuming that frame1
is the starting position and frame2
is the end position before repeat.
假设这frame1
是开始位置,frame2
是重复之前的结束位置。
The problem is that the animation is calculated at the end of the runloop therefore the frame1
is ignored and the view is simply moved to frame2
. If you move the setting of frame1
outside of the block then it should work fine. If you want the animation to play back and forward you will need to make it autoreverse
with UIViewAnimationOptionAutoreverse
问题是动画是在 runloop 结束时计算的,因此frame1
被忽略并且视图只是移动到frame2
。如果您移动frame1
块外部的设置,那么它应该可以正常工作。如果您希望动画回放和前进,则需要autoreverse
使用UIViewAnimationOptionAutoreverse