Amr Lotfy
Amr Lotfy

Reputation: 2997

iOS: Animation Not working in observeValueForKeyPath

I used KVO to observe changes in a frame and set another frame accordingly

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:KeyPathForFrameBeingObserved]) {
         [UIView animateWithDuration:1 animations:^{
            CGRect frame = [self playWithFrame:viewBeingMonitored.frame];
            self.viewToAnimate.frame = frame;
         }];
    }
}

Code in the animation block is executed but animation is not working, I suspected that repeated calls to animation method could cause this but using log messages I found that animation is not working even for a single call, can anyone explain that ?

iPad 2/(iOS8.4)

Upvotes: 0

Views: 285

Answers (2)

Purple Giant
Purple Giant

Reputation: 1

This is an older question but I thought I would answer in case someone else is reading this. You will need to call setFrame to set the frame of the animation instead of the simple assignment.

CGRect frame = [self playWithFrame:viewBeingMonitored.frame]; [self.viewToAnimate setFrame:frame];

Upvotes: 0

Alexander Perechnev
Alexander Perechnev

Reputation: 2837

It seems like you're trying to observe the property of an UIKit object. Keep in mind that:

... the classes of the UIKit framework generally do not support KVO ...

Source: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html

So, you have to check if your observeValueForKeyPath:ofObject:change:context: is calling in fact.

Upvotes: 1

Related Questions