How to properly observe the contentOffset property of my scrollView subclass?

In my iOS application, I observe changes in the contentOffset property of my scrollView subclass. My observer handler looks like this:

- (void)observeContentOffsetHandler:(id)aContentOffset { NSLog(@"%@", aContentOffset); } 

I chose the parameter for the method arbitrarily as id for simplicity.

My NSLog'ging is as follows:

 -[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} 

I need to use the x and y values, but I have no idea how to get to them. I tried pouring the identifier in CGPoint, no. I tried changing the parameter to CGPoint, nope.

UPDATE

Deeper and deeper. @mgold no joy. This is how I set up the observation:

 self.contentOffsetObserver = [[[Observer alloc] initWithTarget:self action:@selector(observeContentOffsetHandler:)] autorelease]; [self.myScrollViewSubclass addObserver:self.contentOffsetObserver forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL]; 

The observer is a convenient class that I use to simplify observation. Note the observer callback observContentOffsetHandler:. When I change the signature of this method to the current one:

 - (void)observeContentOffsetHandler:(id)aContentOffset 

in the @mgold CGPoint suggestion:

 - (void)observeContentOffsetHandler:(CGPoint)aContentOffset 

This is not true, 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 if my move is here.

+6
source share
3 answers

Got it. Correct method signature:

 - (void)observeContentOffsetHandler:(NSValue *)aContentOffset 

CGPoint extraction is then trivial:

 CGPoint pt = [aContentOffset CGPointValue]; 

Cheers
Doug

+6
source

Since you have a subclass of UIScrollView, you have access to layoutSubViews
It is called every time the value of contentOffset changes.

This is the โ€œright wayโ€ to get changes as they occur. Do not use KVO. Yes contentOffset is CGPoint .... if you havenโ€™t talked about NSScrollView ..... but even then the basic idea remains the same.

Override layoutSubviews .... remember super call

OR

register your ViewController as a scrollView delegate and implement scrollView: didScroll

+2
source

contentoffset really CGPoint , which is a C structure with CGFloat x and y . So simple

 aContentOffset.x aContentOffset.y 

Since you are a subclass of UIScrollView , you also have a contentoffset property, simply saying.

0
source

Source: https://habr.com/ru/post/905108/


All Articles