当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - UIPanGestureRecognizer inside UIScrollView

问题描述:

I have the following problem with a UIPanGestureRecognizer inside a UIScrollView:

UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];

sv.contentSize = CGSizeMake(200, 100 *100);

for (int i = 0; i < 100; i++) {

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, i * 100, 200, 100)];

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTile:)];

[panGesture setDelegate:self];

[panGesture setEnabled:FALSE];

[newView addGestureRecognizer:panGesture];

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

[longPressRecognizer setDelegate:self];

[newView addGestureRecognizer:longPressRecognizer];

[sv addSubview:newView];

}

The complete scroll view is filled with small tiles, each of them implements a pan gesture in order to make them draggable. The problem is that - doing so - prevents the scroll view from scrolling. Dragging of the tiles instead works fine. When I disable the tiles pan gestures, scrolling works perfectly. The tiles pan gesture somewhat hides the scroll views own pan gesture. My idea was to disable the tiles pan gesture from the beginning. The gesture is enabled once the user does a long press on the tiles. The problem is that the user has to lift the finger and then touch the tile again to drag it. When the drag is finished, I enable the long press and disable the pan gesture again. So longPress: looks as follows:

- (void)longPress:(UILongPressGestureRecognizer *) gestureRecognizer {

for (UIGestureRecognizer *r in gestureRecognizer.view.gestureRecognizers) {

if ([r isKindOfClass:[UIPanGestureRecognizer class]]) {

[r setEnabled:TRUE];

}

}

//pan gesture should take over here...

}

Is there any possibility to glue the long press and the pan gestures together so that the user doesn't have to lift the finger? Or maybe another solution?

分享给朋友:
您可能感兴趣的文章:
随机阅读: