6

I have a custom UIView that is added to as a subView of a UIScrollView, and I want that UIView automatically repositioned whenever the UIScrollView scrolls. In addition to observe the UIScrollView's contentOffset property(which works fine), I also need observes its dragging property:

[scrollView addObserver:self 
             forKeyPath:@"dragging" 
                options:NSKeyValueObservingOptionNew 
                context:NULL];

but in the observeValueForKeyPath:ofObject:change:context method, I didn't receive any NSNotification for the change of the dragging property, what's the problem here? Thanks.

updated

Since i'm gonna reuse this custom UIView in multiple UIViewControllers, I want the custom view itself handles the repositioning logic, rather than duplicate the repositioning logic in each UIViewController's UIScrollViewDelegate methods(like scrollViewDidScroll thing).

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Pwn
  • 3,387
  • 11
  • 38
  • 42

2 Answers2

4

UIKit is not KVO-compliant in general. The fact that some properties may generate change notifications isn't reliable - it's not documented and may change in future versions etc.

Since you need to reuse this reposition logic, why not separate it properly? Btw - you can place it in your custom view class itself.

Max O
  • 997
  • 8
  • 13
  • Thanks. I find another way around which does not involve observing the `dragging` property anymore. – Pwn Jul 13 '12 at 17:20
0

Or you set a delegate to you UIScrollView and implement scrollViewDidScroll:

iSofTom
  • 1,718
  • 11
  • 15
  • Since i'm gonna reuse this custom view in multiple view controllers, I want the custom view itself handles the repositioning logic, rather than duplicate the repositioning logic in each view controller's UIScrollViewDelegate methods(like scrollViewDidScroll thing). – Pwn Jul 13 '12 at 14:39
  • Try to override the layoutSubviews method in your class, this method is called when a view has to layout its subviews. – iSofTom Jul 13 '12 at 14:43