UIScrollView touchesBegan

2019-01-04 13:46发布

So all I wanna do is play a sound when the user touches a UIScrollView. The UIScrollViewDelegate has that scrollViewWillBeginDragging: method but it only gets called on touchMoved. I want it to get called on touchBegan.

Tried touchesBegan:withEvent: but it doesn't receive any touch. Anyone has a clue?

8条回答
The star\"
2楼-- · 2019-01-04 14:31

You should add touchesBegan:withEvent: on your UIScrollView, not on the UIScrollViewDelegate. UIScrollView is a sub class of UIResponder, which has the touch events.

查看更多
Root(大扎)
3楼-- · 2019-01-04 14:32

I think you will have to subclass UIScrollView to be able to do this.

touchesBegan:withEvent: is only sent to subclasses of UIView. You are problably implementing touchesBegan:withEvent: in your controller, right? If so, it won't work from there...

Alternatively, if you put a subclass of UIView (that you wrote) in your UIScrollView, you can also catch the touchesBegan event from there (but only when the user touches that particular subview). UIScrollView passes on touches to its subviews by default (see touchesShouldBegin:withEvent:inContentView: in UIScrollView).

查看更多
登录 后发表回答