1.将scrollView 添加父类
@interface Scr1 : UIScrollView
@property (nonatomic, copy) void(^isPointInsideHandle)(UIScrollView * scr);
@end
@implementation Scr1
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIButton *bt = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[bt addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor yellowColor];
[self addSubview:bt];
}
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isHit = [super pointInside:point withEvent:event];
if (isHit && self.isPointInsideHandle) {
self.isPointInsideHandle(self);
}
return isHit;
}
VC的代码 这里就将scro1的手势给了scro2。现象滑动scro1时候,scro2才会移动
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
Scr1 *scr1 = [[Scr1 alloc] initWithFrame:CGRectMake(0, 100, 200, 200)];
[self.view addSubview:scr1];
scr1.contentSize = CGSizeMake(500, 500);
scr1.backgroundColor = [UIColor redColor];
[scr1 setIsPointInsideHandle:^(UIScrollView * _Nonnull scr) {
UIPanGestureRecognizer * listPan = self.scr2.panGestureRecognizer;
// NSLog(@"self.scr2 == %@", self.scr2.gestureRecognizers);
// NSLog(@"scr == %@", scr.gestureRecognizers);
if ([scr.gestureRecognizers containsObject:listPan] == NO) {
[scr addGestureRecognizer:listPan];
}
}];
Scr2 *scr2 = [[Scr2 alloc] initWithFrame:CGRectMake(0, 400, 200, 200)];
[self.view addSubview:scr2];
scr2.contentSize = CGSizeMake(500, 500);
scr2.backgroundColor = [UIColor yellowColor];
self.scr2 = scr2;
[self.scr2 setIsPointInsideHandle:^(UIScrollView * _Nonnull scr) {
}];
}
当点击其他地方,或者scro1的点击事件非滑动事件,将事件返回。
在哪里才能真正判断事件是滑动还是点击呢。答案是: Window
@implementation MYTouchObserverWindow
//触摸时,所有的事件分发都在这里
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
for (UITouch * touch in event.allTouches) {
if (UITouchPhaseEnded == touch.phase || UITouchPhaseCancelled == touch.phase) {
//触摸离开屏幕
//allTouches通常只有一个
[[AAAA sharedInstance] bbbb];
return;
}
}
}
self.collectionArray 添加上面 self.scro1
下面代码就会将 scro1的滑动手势还给scro1.并且从scro2移除。
- (void)bbbb {
for (UICollectionView * colletionView in self.collectionArray) {
if ([colletionView.gestureRecognizers containsObject:colletionView.panGestureRecognizer] == NO) {
//手势添加给一个view,会自动从原来的view移除
[colletionView addGestureRecognizer:colletionView.panGestureRecognizer];
}
}
}