IPhone / iPad - Core Animation - CGAffineTransformMakeTranslation does not change the frame

I ran into a very annoying problem. Here's the context: I have a "rectangle" that is a sub-item of the main view. What I'm trying to do is just when I click on the button, I want the rectangle view to be translated along the x axis to disappear. Then I add a new subview and translate it to replace the old β€œrectangle” view. It works great, except that if I press the button again, the animation starts from the screen, for example, CGAffineTransformMakeTranslation will not change the frame of my new "rectangular" view. Here is the code:

UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0) [UIView animateWithDuration:0.5 animations:^{ [rectangleView setTransform:CGAffineTransformMakeTranslation(-1000, 0)]; } completion:^(BOOL finished) { [rectangleView removeFromSuperview]; UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)]; [otherView setBackgroundColor:[UIColor purpleColor]]; [otherView setTag:4]; [detailView addSubview:otherView]; [UIView animateWithDuration:0.5 animations:^{ [otherView setTransform:CGAffineTransformMakeTranslation(-1000, 0)]; } completion:^(BOOL finished) { [otherView release]; }]; }]; 
+4
source share
2 answers

After adding the second view, you have already set its transformation to CGAffineTransformMakeTranslation(-1000, 0) , and when you want to remove this view, you will install the exact same transformation, so it will have no effect. You have 2 options:

  • Apply the translation to a transformation that already has the form:

     CGAffineTransform newTransform = CGAffineTransformConcat(rectangleView.transform, CGAffineTransformMakeTranslation(-1000, 0)); [rectangleView setTransform:newTransform]; 
  • Instead of applying transformations, directly work with the position of the view (for example, through its center property)

     UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0) CGAffineTransform tf = CGAffineTransformMakeTranslation(-1000, 0); [UIView animateWithDuration:0.5 animations:^{ [rectangleView setCenter: CGPointApplyAffineTransform(rectangleView.center, tf)]; } completion:^(BOOL finished) { [rectangleView removeFromSuperview]; UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)]; [otherView setBackgroundColor:[UIColor purpleColor]]; [otherView setTag:4]; [detailView addSubview:otherView]; [UIView animateWithDuration:0.5 animations:^{ [otherView setCenter: CGPointApplyAffineTransform(otherView.center, tf)]; } completion:^(BOOL finished) { [otherView release]; }]; }]; 
+3
source

Try revitalizing the center property instead of using the affine transform. The transformations are not additive, so the second animation (when your recently added detailed view is then moved off the screen) does not actually change the view, since it already has this translation (-1000.0) applied to it.

+1
source

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


All Articles