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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:04:34  来源:igfitidea点击:

UIView block animation move view left to right and back to left

ioscocoa-touchuiviewuiviewanimation

提问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 UIViewAnimationOptionAutoreverseoption, 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 frame1is the starting position and frame2is the end position before repeat.

假设这frame1是开始位置,frame2是重复之前的结束位置。

The problem is that the animation is calculated at the end of the runloop therefore the frame1is ignored and the view is simply moved to frame2. If you move the setting of frame1outside of the block then it should work fine. If you want the animation to play back and forward you will need to make it autoreversewith UIViewAnimationOptionAutoreverse

问题是动画是在 runloop 结束时计算的,因此frame1被忽略并且视图只是移动到frame2。如果您移动frame1块外部的设置,那么它应该可以正常工作。如果您希望动画回放和前进,则需要autoreverse使用UIViewAnimationOptionAutoreverse