2

In layoutSubviews i layout my child views into appropriate positions, and for some of them apply some transformation. Now i want to make on this subviews repeatable scale animation to draw attention. But, when i launch animation with [UIView animate....], with changing transform property - fired layoutSubviews method, which conflict with animation (it override transform and animation not played).

Is there any good way (without bool flags) to handle this behavior?

Example:

- (void)layoutSubviews {
    [super layoutSubviews];
    myChildView.transform = CGAffineTransformMake....;
}

- (void)animate {
    CGAffineTransform originalTransform = myChildView.transform;
    [UIView animateWithDuration:0.1f
                          delay:0.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^{
                         [UIView setAnimationRepeatCount:4];
                         //next line of code produce calling layoutSubviews
                         myChildView.transform = CGAffineTransformMakeScale(1.15f, 1.15f);
                     } completion:^(BOOL finished){
                         myChildView.transform = originalTransform; 
                         //after this also called layoutSubviews
                     }];
}
Mikhail
  • 4,271
  • 3
  • 27
  • 39

4 Answers4

2

Recently, I have encountered this issue. I override the method viewDidLayoutSubviews inside the view controller to control the animation.

- (void)viewDidLayoutSubviews {
     // This method will be called after subview's layoutSubviews.
     if (self.shouldAnimated) {
        // Do animation at here.
     }
}

However, this is just a dirty solution.

ShengHuaWu
  • 140
  • 8
1

I suggest you would do better to not use layoutSubviews. Instead create and position and transform your subviews either inside the init method or a custom method called from the init.

ader
  • 5,403
  • 1
  • 21
  • 26
1

I meet this problem also. And I add another view(called subView) as a subview, and add the myChildView to subView. This will not trigger the layoutSubviews of superview when I change the transform of myChildView. And why, you can see this UIView/CALayer: Transform triggers layoutSubviews in superview.

Community
  • 1
  • 1
-1

Is there any good way (without bool flags) to handle this behavior?

Yes. Rid off "layoutSubviews" method and set transform for children views in another method.

[self.parentView setUpChildrenViews];
...
[self.parentView animate];
Dmitry Vodianyk
  • 395
  • 3
  • 4