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>