0

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.
Community
  • 1
  • 1
Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
  • did you call addObserver forKeyPath in your object ? – LHIOUI Dec 26 '15 at 10:34
  • @Coyote Yes, I did, KVO is working perfectly but the result is not animated. – Amr Lotfy Dec 26 '15 at 16:23
  • Does this thread apply at all? http://stackoverflow.com/questions/26277109/uiview-animatewithduration-not-animating-in-ios8 – A O Jan 13 '16 at 13:23
  • Yeah the answerer in that question recommended modifying the auto layout constraints (the constant property), instead of modifying the frame (as you are doing in the code you posted) – A O Jan 13 '16 at 19:40
  • is **presentationLayer** the issue? http://stackoverflow.com/questions/22582192/ios-observing-change-in-frame-of-a-uiview-during-animation – Fattie Dec 29 '16 at 13:00

2 Answers2

1

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.

Alexander Perechnev
  • 2,797
  • 3
  • 21
  • 35
  • KVO already worked in my case, see http://stackoverflow.com/a/19687115/1356559 , the problem is that code in the animation blocks is executed but without animation. – Amr Lotfy Dec 26 '15 at 16:34
0

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