Search code examples
animationuiviewcalayercgpath

How to check that my method is called from inside '[UIView animateWithDuration:...]'?


I have a method that sets up UIView properties. And I call it directly in some cases and from UIView animateWithDuration: block. In this method I change among other things path of view's layer, so it works incorrectly with animation (path must be animated explicitly).

How can I determine that my method is called from inside [UIView animateWithDuration:...]?

[UIView animateWithDuration:duration animations:^{ [myClass myMethod]; }];
...
- (void)myMethod
{
    if (__view_is_animating__) // how to check here?
    {...}
}

At current moment I set custom viewIsAnimationFlag before [UIView animateWithDuration:...] and check it in myMethod but it's rudely.


Solution

  • Thanks to David Rönnqvist at objc.io:

    "the view returns NSNull whenever the layer asks for an action (animation handler for any animatable property), except when the property change happened inside of an animation block"

    NSLog(@"we are inside animation block: %@",
        [myView actionForLayer:myView.layer forKey:@"position"] ? @"YES" : @"NO");