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?
source share