[英]UIView block animation move view left to right and back to left
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) {
}];
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. 它将重复从右向左,从左向右,...的移动。
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
. 问题在于动画是在运行循环的末尾计算的,因此将忽略
frame1
并将视图简单地移至frame2
。 If you move the setting of frame1
outside of the block then it should work fine. 如果将
frame1
的设置移出该块,则它应该可以正常工作。 If you want the animation to play back and forward you will need to make it autoreverse
with UIViewAnimationOptionAutoreverse
如果要播放动画并向前播放,则需要使用
UIViewAnimationOptionAutoreverse
将其自动autoreverse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.