Scrolling detection in a subclass of UIScrollView

Good morning,

I am creating a subclass of UIScrollView and now I want to know when the user scrolls my subclass. For this, I implemented it as follows:

ImageScroller.h

@interface UIImageScroller : UIScrollView <UIScrollViewDelegate> { 

ImageScroller.m (as part of @implementation)

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"did scroll"); } 

The problem is that the scrollViewDidScroll method does not seem to work. Is there any way to make it work?

I also tried to set a delegate for it, but it does not work.

  - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { self.directionalLockEnabled = YES; [self setDelegate:self]; } return self; } 

I added a ScrollView to my XIB file and set the class if it was on my ImageScroller. I also installed Fileowner, and I use UIScrollViewDelegate in the .h file of the ViewController, and also implement the scrollViewDidScroll method in the .m file.

When I set the delegate of my ImageScroller to the code of the .m file from the XIB, like [imageScroller setDelegate: imageScroller] scrollViewDidScroll runs in my ImageScroller subclass, but the one in my ViewController does not start, but I need both.

Any solutions for this?

Thanks for your answers in advance.

+6
source share
3 answers

I think you can try setting self.delegate = self; to receive events.

+4
source

I ran into the same problem by subclassing UIScrollView, but solving it that way. As mentioned above, you can set yourself (an instance of a subclass) as a delegate, but that is what I did.

In a subclass, I override the following methods

 //Override this to detect when the user is scrolling, this is also triggered during view //layout. Here you can check your deceleration rate and determine when the scrolling has //grinded to a halt. -(void)setContentOffset:(CGPoint)contentOffset //Override this to detect when the view has been resized (just a handy override) -(void)setFrame:(CGRect)frame; 

Unlike other UIScrollViewDelegate methods, the view manager should be responsible for handling these, in my opinion.

+13
source

If someone is looking for a Swift 3 answer, it's that simple:

 override var contentOffset: CGPoint { didSet { if contentOffset != oldValue { //same as scrollViewDidScroll } } } 
+1
source

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


All Articles