observe scroll view's 'dragging' property but received no notification
Asked Answered
G

2

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).

Gehenna answered 13/7, 2012 at 14:28 Comment(0)
T
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.

Todd answered 13/7, 2012 at 14:43 Comment(1)
Thanks. I find another way around which does not involve observing the dragging property anymore.Gehenna
L
0

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

Laconic answered 13/7, 2012 at 14:30 Comment(2)
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).Gehenna
Try to override the layoutSubviews method in your class, this method is called when a view has to layout its subviews.Laconic

© 2022 - 2024 — McMap. All rights reserved.