How to animate a UIView going off the screen to the right, and then reappearing back from the left

旧时模样 提交于 2020-01-13 13:06:15

问题


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.

Update:

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?


回答1:


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];
}];

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.




回答2:


you can use this code in case :

  1. view will go out off screen to Left and Get In From Right.
  2. view will go out off screen to Right and Get In From Left.

code sniped:

- (void)animateViewWithTransformation:(MyEnumAnimationTransformation)animationTransformation withDuration:(CGFloat)duration {
    CGFloat goOutOffScreenToX, cameInToScreenFromX;
    switch (animationTransformation) {
        case goOutToLeftGetInFromRight:
            goOutOffScreenToX = -1 * [UIScreen mainScreen].applicationFrame.size.width;
            cameInToScreenFromX = [UIScreen mainScreen].applicationFrame.size.width;
            break;
        case goOutToRightGetInFromLeft:
            goOutOffScreenToX = [UIScreen mainScreen].applicationFrame.size.width;
            cameInToScreenFromX = -1 * [UIScreen mainScreen].applicationFrame.size.width;
            break;
        default:
            break;
    }

    CGRect originViewFrame = self.frame;
    [UIView animateWithDuration:duration
                     animations:^{[self setX:goOutOffScreenToX];}
                     completion:^(BOOL finished)
                                { 
                                    [self setX:cameInToScreenFromX];
                                    [UIView animateWithDuration:duration
                                                     animations:^{[self setX:originViewFrame.origin.x];}
                                                     completion:nil];
                                }];
}


来源:https://stackoverflow.com/questions/8640685/how-to-animate-a-uiview-going-off-the-screen-to-the-right-and-then-reappearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!