如何为UIView设置动画,使其从屏幕右侧移出,然后从左侧重新出现 [英] How to animate a UIView going off the screen to the right, and then reappearing back from the left

查看:436
本文介绍了如何为UIView设置动画,使其从屏幕右侧移出,然后从左侧重新出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含在应用程序主视图中的视图(屏幕中间的正方形)。如果您向左滑动视图,我编写了以下代码,将视图从屏幕右移:

I have a view that is contained in the main view of my application (a square in the middle of a screen). I've written the following code that slides the view off the screen to the right, if you swipe it to the left:

- (IBAction)swipeLeft:(id)sender
{
    CGRect initCardViewFrame = self.cardView.frame;
    CGRect movedCardToRightViewFrame = CGRectMake(initCardViewFrame.origin.x + 1000, initCardViewFrame.origin.y, initCardViewFrame.size.width, initCardViewFrame.size.height);

    [UIView animateWithDuration:0.7
                     animations:^{self.cardView.frame = movedCardToRightViewFrame;}
                     completion:nil];
}

这很好,但是我想扩展代码,以便一旦正方形从屏幕的右侧消失,它从左侧向右回到中间。我不太确定如何在窗口左侧左侧重新绘制它,然后将其滑回中间。我只是尝试重做帧,但是动画仅显示动画块中的最后一帧运动。我假设我需要将视图从屏幕上移开,然后在屏幕左手的外部重绘它,然后在中间滑动它。当然,除非有更直观的方法。

This works great, however I would like to extend the code such that once the square goes off the right side of the screen, it comes right back from the left side back to the middle. I'm not really sure how to "re-draw" it on the left outside of the window, and then slide it back in the middle. I've tried just redoing the frames, but the animations only show the last frame movement in the animation block. I'm assuming that I need to push the view off the screen, then redraw it on the outside of the left hand of the screen, then slide it in the middle. Unless there is a more intuitive way to do it of course.

更新:

我已经回答了这个问题下面的问题,但我不禁想到有一种更好的方法,而无需在彼此之间嵌套UIView动画。这是解决此问题的唯一方法吗?

I've answered this question below, but I can't help but think there is a better way to do this without nesting UIView animations inside each other. Is this the only way to tackle this problem?

推荐答案

我最终在 completion参数内添加了以下代码:

I ended up adding the following code inside the "completion" parameter:

completion:^(BOOL finished)
{ 
    self.cardView.frame = moveCardToLeftViewFrame; 

    [UIView animateWithDuration:0.4 
                     animations:^{self.cardView.frame = initCardViewFrame;}
                     completion:nil];
}];

注意* moveCardToLeftViewFrame是一个CGRect,它将视图放置在可见窗口的左侧。因此,在向右滑动后,它被放置在屏幕的左侧,然后向后滑动到中间。

Note* moveCardToLeftViewFrame is a CGRect that places the view to the left of the visible window. So after it slides to the right, it is placed outside of screen to the left, then slides back into the middle.

这篇关于如何为UIView设置动画,使其从屏幕右侧移出,然后从左侧重新出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆