UIButton touch is delayed when in UIScrollView
Ok I've solved this by subclassing UIScrollView
and overriding touchesShouldCancelInContentView
Now my UIButton
that was tagged as 99 highlights properly and my scrollview is scrolling!
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;
}
UIScrollview delaysContentTouches issue
OK I have resolved by implementing below method :
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
NSLog(@"touchesShouldCancelInContentView");
if ([view isKindOfClass:[UIButton class]])
return NO;
else
return YES;
}
How do I pass delayed scroll gestures from a UIButton inside of a UIScrollView?
I found the answer to this here: UIScrollview with UIButtons - how to recreate springboard?
Essentially, I had to extend UIScrollView and override touchesShouldCancelInContentView, having it always return YES.
iOS - Delayed Touch Down event for UIButton in UITableViewCell
This is caused by the UIScrollView
property delaysContentTouches
.
It used to be sufficient to just set that property to NO
for the UITableView
itself, but that will only work for subviews of the table that are not encased in another UIScrollView
.
UITableViewCells
contain an internal scroll view in iOS 7 so you will need to change the value of this property on the cell level for all cells with buttons in them.
Here is what you need to do:
1.in viewDidLoad
or somewhere similar once your UITableView
has been initialized, put this in:
self.tableView.delaysContentTouches = NO;
2.for iOS 7 support, in the initialization method for your UITableViewCell
(initWithStyle:reuseIdentifier:
or initWithCoder:
for NIBs), put this in at the end:
for (UIView *currentView in self.subviews)
{
if([currentView isKindOfClass:[UIScrollView class]])
{
((UIScrollView *)currentView).delaysContentTouches = NO;
break;
}
}
This is unfortunately not a 100% permanent solution as Apple can change the view hierarchy inside cells again in the future (perhaps moving the scroll view another layer down or something which would require you to nest another loop in there), but until they surface the class or at least the property to developers somehow, this is the best we've got.
Can't touch UIButton in UIScrollView
Try this sample Code:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(@"Button Click");
}
@end
iOS UIScrollView cancel UIButton touch on scroll
You can override this method of UIScrollView
.
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}
And in your UIButton, you should add different method for different control events.
- show highlight effect for
UIControlEventTouchDown
. - trigger button for
UIControlEventTouchUpInside | UIControlEventTouchUpOutside
; - clear highlight effect for
UIControlEventTouchCancel
.
UIScrollView with UIButton doesn't scroll when touch down on button
There's no automatic way of doing that. You have to manually handle the touch event, and then determine what the user wants to do, if he moves the finger X pixels up or down, then he wants to scroll, and if he releases the touch inside the button, then he probably wants to activate that action.
Regards!
UIScrollView with UIButtons - Scroll View won't scroll when touch began on button
I ended up creating a subclass of UIScrollView and set it's cancelContentTouches value to TRUE and my problem was solved
Related Topics
Uitextview Cursor Below Frame When Changing Frame
Crashlytics Is Not Sending Crash Report from Iphone
Communicating Between iOS and Android with Bluetooth Le
How to Add a Container View Programmatically
How to Make iPhone Vibrate Using Swift
How Do We Create a Bigger Center Uitabbar Item
Uitextview Link Detection in iOS 7
Swiftui Withanimation Completion Callback
Warning: Uicollectionviewflowlayout Has Cached Frame Mismatch for Index Path 'Abc'
How to Disable No Inverse Relationship Warning for Coredata in Xcode 4.2
Method Load() Defines Objective-C Class Method 'Load', Which Is Not Permitted by Swift 1.2
How to Remove an iOS App from the App Store
How to Add 2 Buttons into the Uinavigationbar on the Right Side Without Ib