如何正确观察我的scrollView子类的contentOffset属性? [英] How do I properly observe the contentOffset property of my scrollView subclass?
问题描述
在我的iOS应用中,我正在观察对scrollView子类的contentOffset属性的更改.我的观察员处理程序如下所示:
In my iOS app I am observing changes to the contentOffset property of my scrollView subclass. My observer handler looks like this:
- (void)observeContentOffsetHandler:(id)aContentOffset {
NSLog(@"%@", aContentOffset);
}
为简单起见,我任意选择方法的参数作为 id .
I chose the parameter to the method arbitrarily as an id for simplicity.
我的NSLog'ging看起来像这样:
My NSLog'ging looks like this:
-[MyScrollView observeContentOffsetHandler:] [Line 111] NSPoint: {296, 375}
-[MyScrollView observeContentOffsetHandler:] [Line 111] NSPoint: {296, 389}
-[MyScrollView observeContentOffsetHandler:] [Line 111] NSPoint: {295, 401}
-[MyScrollView observeContentOffsetHandler:] [Line 111] NSPoint: {291, 415}
我需要使用x和y值,但我不知道如何获得它们.我试过将ID投射到CGPoint,不.我试过将参数更改为CGPoint,不.
I need to use the x and y values but I have no idea how to get at them. I've tried casting the id to a CGPoint, nope. I've tried changing the param to a CGPoint, nope.
它变得更深. @mgold不高兴.这是我设置观察值的方法:
It gets deeper. @mgold no joy. Here is how I set up observation:
self.contentOffsetObserver = [[[Observer alloc] initWithTarget:self action:@selector(observeContentOffsetHandler:)] autorelease];
[self.myScrollViewSubclass addObserver:self.contentOffsetObserver forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
观察者是我用来简化观察的便捷类.注意观察者回调 observeContentOffsetHandler:.当我从当前方法更改此方法的签名时:
Observer is a handy class I use to make observation easy. Note the observer callback observeContentOffsetHandler:. When I change the signature of this method from its current:
- (void)observeContentOffsetHandler:(id)aContentOffset
@mgold对CGPoint的建议:
to @mgold's suggestion of CGPoint:
- (void)observeContentOffsetHandler:(CGPoint)aContentOffset
这是不正确的,因为NSLog显示aContentOffset的全零:
It is incorrect as NSLog shows with all zeros for aContentOffset:
-[MyScrollController observeContentOffsetHandler:] [Line 74] aContentOffset 0 0
-[MyScrollController observeContentOffsetHandler:] [Line 74] aContentOffset 0 0
-[MyScrollController observeContentOffsetHandler:] [Line 74] aContentOffset 0 0
-[MyScrollController observeContentOffsetHandler:] [Line 74] aContentOffset 0 0
不知道我在这里做什么.
Not sure what my move here is.
推荐答案
知道了.该方法的正确签名为:
Got it. The method correct signature is:
- (void)observeContentOffsetHandler:(NSValue *)aContentOffset
然后检索CGPoint很简单:
Retrieval of the CGPoint is then trivial:
CGPoint pt = [aContentOffset CGPointValue];
干杯,
道格
这篇关于如何正确观察我的scrollView子类的contentOffset属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!