UIButton touch is delayed when in UIScrollView

2019-01-08 05:28发布

I'm running into a small issue in my app.

I essentially have a series of UIButtons added as subviews in a UIScrollView which is part of a nib. Every time I tap on a button there is a noticeable delay before the button is highlighted. I essentially have to hold it for about half a second before the button dims and appears selected.

I'm assuming this is because the UIScrollView needs to determine if the touch is a scroll or if it's a touch that is meant for a subview.

Anyways, I'm a little unsure on how to proceed. I simply want the button to appear selected as soon as I tap it.

Any help is appreciated!

Edit:

I've tried setting delaysContentTouches to NO but scrolling becomes almost impossible since a majority of my scrollView is filled with UIButtons.

7条回答
甜甜的少女心
2楼-- · 2019-01-08 05:56

None of the existing solutions worked for me. Maybe my situation is more unique.

I have many UIButtons within a UIScrollView. When a UIButton is pressed a new UIViewController is presented to the user. If a button is pressed and held long enough, the button will show its depressed state. My client was complaining that if you tap too quickly, no depressed state is shown.

My solution: Inside the UIButtons' tap method, where I load the new UIViewController and present it on screen, I use

[self performSelector:@selector(loadNextScreenWithOptions:) 
           withObject:options 
           afterDelay:0.]

This schedules the loading of the next UIViewController on the next event loop. Allowing time for the UIButton to redraw. The UIButton now shows its depressed state before loading the next UIViewController.

查看更多
登录 后发表回答