UIView 块动画将视图从左到右和从后到左移动

发布于 2024-12-23 10:40:37 字数 330 浏览 2 评论 0原文

我不想对形状为箭头的视图进行动画处理,我希望它从左到右、从左到右等进行动画处理。

下面的代码不起作用,我猜我需要一条路径,但是不知道如何在 UIView 块动画中做到这一点。

   [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
    controller.view.frame = frame1;
    controller.view.frame = frame2;

} completion:^(BOOL finished) {

}];

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 animateWithDuration:.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
    controller.view.frame = frame1;
    controller.view.frame = frame2;

} completion:^(BOOL finished) {

}];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-12-30 10:40:37

您可以使用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];

它将重复从右到左,从左到右,......顺利地移动。

You can use UIViewAnimationOptionAutoreverse option, try the code below:

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.

岁月流歌 2024-12-30 10:40:37

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

问题是动画是在运行循环结束时计算的,因此忽略了frame1,视图只是移动到frame2。如果将 frame1 的设置移到块之外,那么它应该可以正常工作。如果您希望动画向后和向前播放,则需要使用 UIViewAnimationOptionAutoreverse 使其autoreverse

Assuming that frame1 is the starting position and frame2 is the end position before repeat.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文