简体   繁体   中英

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)

  • I have tried including the animation method call in dispatch_async(dispatch_get_main_queue(), ^{ ... }); but animation still not working.
  • I have tried pushing the observed changes in queue and delaying the animation method for a second using dispatch_after to run it only using the last element in the queue to avoid repeated calls but didn't work.
  • KVO is working perfectly (adding the observer is already handled), but the code in the animation block is executed without animation.

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM