使用UIScrollView的时候,总是调用不了touchesBegan和touchesEnd,还有另外的两个方法,经过查找是因为UIScrollViewDelegate没有这个方法,不过我们可以自己来定义这个方法,来调用父类的方法:
touchScrollView.h
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#import <UIKit/UIKit.h>
@interface touchScrollView : UIScrollView
@end
|
touchScrollView.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#import "touchScrollView.h"
@implementation touchScrollView
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; }
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [super touchesBegan:touches withEvent:event]; if ( !self.dragging ) { [[self nextResponder] touchesBegan:touches withEvent:event]; } }
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [super touchesEnded:touches withEvent:event]; if ( !self.dragging ) { [[self nextResponder] touchesEnded:touches withEvent:event]; } }
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; if(!self.dragging) { [[self nextResponder] touchesCancelled:touches withEvent:event]; } }
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; if(!self.dragging) { [[self nextResponder] touchesMoved:touches withEvent:event]; } }
@end
|
使用方法可以这样
1
| @property (strong, nonatomic) IBOutlet touchScrollView *scrollView;
|