How to animate a UIView going from the screen to the right and then reappear on the left

I have a view that is contained in the main view of my application (the square in the middle of the screen). I wrote the following code that shifts the view from 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 fine, however I would like to extend the code in such a way that as soon as the square leaves on the right side of the screen, it returns from the left side back to the middle. I'm not quite sure how to β€œredraw” it to the left outside the window, and then move it back in the middle. I tried just redoing the frames, but the animation shows only the last frame movement in the animation block. I assume that I need to get the view out of the screen, and then redraw it on the outside of the left side of the screen, and then shift it in the middle. Unless there is a more intuitive way to do this, of course.

Update:

I answered this question below, but I cannot help but think that there is a better way to do this without inserting UIView animations inside each other. Is this the only way to solve this problem?

+4
source share
2 answers

As a result, I added 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. Therefore, after it slides to the right, it is placed off the screen to the left, then slides back to the middle.

+4
source

you can use this code in case of:

  • will exit the screen left and right.
  • view will exit the screen on the right and 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]; }]; } 
0
source

Source: https://habr.com/ru/post/1388079/


All Articles