Solve the Click Event and UIScrollView event of uiscrollview

Source: Internet
Author: User

Solve the Click Event and UIScrollView event of uiscrollview

There are currently two methods

The first method is to use Category to extend the UIScrollView object and add a touch event. (It is not recommended that the extension be inconvenient in the future.) The Code is as follows:

@implementation UIScrollView (ExtendTouch)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [[self nextResponder] touchesBegan:touches withEvent:event];    [super touchesBegan:touches withEvent:event];    UITouch *touch = [touches anyObject];    CGFloat startx = [touch locationInView:self].x;    NSLog(@"%f",startx);}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    [[self nextResponder] touchesMoved:touches withEvent:event];    [super touchesMoved:touches withEvent:event];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    [[self nextResponder] touchesEnded:touches withEvent:event];    [super touchesEnded:touches withEvent:event];}

Second, add a gesture (recommended and easy to maintain)

// Add the point-and-click gesture listener UITapGestureRecognizer * tapGesture = [[delealloc] initWithTarget: self action: @ selector (tapUiscrollView :)]; // set the gesture attribute tapGesture. delegate = self; tapGesture. numberOfTapsRequired = 1; // set the number of clicks. The default value is 1. Note that you rarely double-click tapGesture in iOS. numberOfTouchesRequired = 1; // The hand index of the point by [self. scrllview addGestureRecognizer: tapGesture];

  

@interface ViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>

  

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.