UIButton touch is delayed when in a UIScrollView

There is a small problem in my application.

I essentially have a series from UIButtons added as subviews to UIScrollView , which is part of nib. Each time I press 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 goes out and displays.

I assume this is because the UIScrollView needs to determine if the touch is a scroll or a touch that is meant to be watched.

In any case, I'm a little unsure how to proceed. I just want the button to be selected as soon as I clicked it.

Any help is appreciated!

Edit:

I tried setting delaysContentTouches to NO, but scrolling becomes almost impossible, since most of my scrollView is populated with UIButtons .

+41
ios uibutton uiscrollview touchescancelled
Sep 04 '10 at 13:51
source share
7 answers

Ok, I solved this by subclassing UIScrollView and overriding touchesShouldCancelInContentView

Now my UIButton , which was marked as 99, is highlighted correctly, and the scroll scroll scrolls!

myCustomScrollView.h:

 @interface myCustomScrollView : UIScrollView { } @end 

and myCustomScrollView.m:

 @implementation myCustomScrollView - (BOOL)touchesShouldCancelInContentView:(UIView *)view { NSLog(@"touchesShouldCancelInContentView"); if (view.tag == 99) return NO; else return YES; } 
+39
Sep 04 '10 at 16:24
source share
— -

Jeff's solution doesn’t work for me, but it is similar: http://www.charlesharley.com/2013/programming/uibutton-in-uitableviewcell-has-no-highlight-state/

Not sure if this was the case with Jeff's answer, but in addition to overriding -touchesShouldCancelInContentView: in your scroll subclass, you still need to set delaysContentTouches to NO . Finally, instead of returning NO for your buttons, you need to return YES . Here is an example from the link above.

 - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.delaysContentTouches = NO; } return self; } - (BOOL)touchesShouldCancelInContentView:(UIView *)view { if ([view isKindOfClass:UIButton.class]) { return YES; } return [super touchesShouldCancelInContentView:view]; } 

Quick version.

 override func touchesShouldCancelInContentView(view: UIView!) -> Bool { if view is UIButton { return true } return super.touchesShouldCancelInContentView(view) } 
+35
Oct 29 '13 at 11:10
source share

Try setting the UIScrollView delaysContentTouches to NO.

+24
04 Sep '10 at 14:22
source share

In Swift 3:

 import UIKit class ScrollViewWithButtons: UIScrollView { override init(frame: CGRect) { super.init(frame: frame) myInit() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) myInit() } private func myInit() { self.delaysContentTouches = false } override func touchesShouldCancel(in view: UIView) -> Bool { if view is UIButton { return true } return super.touchesShouldCancel(in: view) } } 

Then you can use this ScrollViewWithButtons in IB or in code.

+1
Jul 21 '17 at 17:16
source share

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

I have many UIButtons within a UIScrollView . When the UIButton button is pressed, a new UIViewController provided for the user. If the button is pressed and held long enough, the button will show its pressed state. My client complained that if you click too fast, the state of the pressed one is not displayed.

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

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

This is the load schedule for the next UIViewController in the next event loop. Time resolution for UIButton to redraw. UIButton now shows its lowered state before loading the next UIViewController .

0
Jul 23 2018-12-12T00:
source share

Storyboard Solution: Select a scroll view, open the "Attributes Inspector" and clear the "Holds Content" checkbox.

enter image description here

0
Jan 17 '17 at 9:54 on
source share

Swift 3:

 scrollView.delaysContentTouches = false 
0
May 27 '17 at 14:29
source share



All Articles