Ragen Dazs
Ragen Dazs

Reputation: 2170

UIScrollView subclass trigger event

I've tried for some days understand Xcode Subclasses and Categories - and after all I found one event that are fired.

- (void)setContentOffset:(CGPoint)contentOffset {
    NSLog(@"foo");
}

And for more confusion, after read Apple iOS Documentation I get this stuff:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
    NSLog(@"bar");
}

First event are fired, but from Apple documentation are not. Why?!

But in the first case, although he was fired the UIScrollView loses their scroll/drag'n' bounce behavior. I think it's because after overrride setContentOffset I would need to call the parent method to keep the default behavior of the UIScrollView. But I'm already exhausted from test obsolete Xcode approaches.

Than why second code are not fired and how call parent overridden method?

Thanks in advance.

Upvotes: 0

Views: 267

Answers (1)

Furkan Mustafa
Furkan Mustafa

Reputation: 794

To call the super (:parent) here

- (void)setContentOffset:(CGPoint)contentOffset {
    NSLog(@"foo  New Offset x: %.0f y: %.0f", contentOffset.x, contentOffset.y);
    [super setContentOffset:contentOffset];
}

And, for the second one; That is not a delegate method (:event), this is a method provided to developer actually, to initiate scrolling to a specific offset with/without animation. You probably do not need to override this.

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;

Even more; even the first one is not an event, that's a message sent to scrollview to change the offset, but you can get in between and do your thing using that as an event trigger, and call super again to let it do it's work.

If you want to get real events on scrollView, you need to set up a delegate as documented here; https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/UIScrollViewDelegate

And I also agree with Wain on sharing this link, https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Delegation.html

Upvotes: 1

Related Questions