UIButton highlight and uibutton highlight on UITableView or UIScrollVIew

Source: Internet
Author: User

UIButton highlight and uibutton highlight on UITableView or UIScrollVIew
UIButton highlight on UITableView or UIScrollVIew

Address: http://www.jianshu.com/p/b4331f06bd34

Recently, when I was working on a project, I found that the UIButton click on UIScrollView was not highlighted, but the click event was indeed triggered, but this would create an illusion, it seems to the user that UIButton is not clicked. However, if you click it for a long time, it will be highlighted. So I found that this phenomenon should be caused by the length of time. Following this question, we will pursue the touch principle of UIScrollView. UIScrollView has a delaysContentTouches attribute.

Explanation of Apple's official documentation: default is YES. if NO, we immediately call-touchesShouldBegin: withEvent: inContentView:. this has no effect on presses

Obviously, the default value of delaysContentTouches is YES, that is, UIScrollView will receive a gesture delay of MS to determine whether the gesture can trigger the sliding event of UIScrollView; if the value is NO, UIScrollView immediately distributes the received gestures to the subview.

Of course, setting delaysContentTouches to "NO" is far from enough, because if you want to drag the UIScrollView and drop the view with Gesture Recognition for him at 7, it will not be moved. Therefore, we need to overload touchesShouldCancelInContentView. This method determines whether the gesture is canceled and the drag UIScrollView is triggered. When NO is returned, the drag gesture will be left on the UIScrollView; when YES is returned, it will be uploaded to the View.

Now, the solution is clear. We only need to set the delaysContentTouches attribute of all UIButton parent views belonging to UIScrollView to NO and override the touchesShouldCancelInContentView method. Paste the code below

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

This is the UIButton highlighting method of UIScrollView. However, UITableview is more complex
UITableView:

The view structure in iOS7 and iOS8 is different, and there are many views that we will never be able to access during coding, but we can find their subviews one by one through Debug. This is related to the depth of our problem.

IOS7: There are n + 1 UIScrollView in UITableView, one is UITableView itself, and the other n villages are between the contenviews of UITableViewCell. The class name is UITableVieCellScrollVIew, which has been removed from ios8.

IOS8: There are two uiscrollviews in UITableView. One is UITableView itself, and the other exists between UITableView and UITableViewCell. The class name is UITableViewWrapperView. Note that UITableViewWrapperView is not a UIScrollView in iOS7.

Post Code directly after understanding

@ Implementation ResponseTableView-(instancetype) initWithCoder: (NSCoder *) aDecoder {self = [super initWithCoder: aDecoder]; if (self) {self. delaysContentTouches = NO; // because UITableViewWrapperView does not belong to UIScrollView in iOS7 // iOS7 for (id obj in self. subviews) {if ([NSStringFromClass ([obj class]) isw.tostring: @ "UITableViewCellScrollView"]) {UIScrollView * scrollView = (UIScrollView *) obj; scrollView. delaysContentTouches = NO; break ;}// ios 8 for (id view in self. subviews) {if ([NSStringFromClass ([view class]) isw.tostring: @ "UITableViewWrapperView"]) {if ([view isKindOfClass: [UIScrollView class]) {UIScrollView * scrollView = (UIScrollView *) view; scrollView. delaysContentTouches = NO;} break; }}return self;}-(BOOL) touchesShouldCancelInContentView: (UIView *) view {if ([view isKindOfClass: [UIButton class]) {return YES;} return [super touchesShouldCancelInContentView: view];}

The above solution solves the problem of Button delay highlighting.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.