【问题标题】:Drag and drop within UIScrollView在 UIScrollView 内拖放
【发布时间】:2013-07-04 09:27:04
【问题描述】:

我正在尝试设计一个视图,其中包含要订购并放入数组的项目列表。这些项目是动态生成的,因此冷的数量会超出屏幕底部。

出于这个原因,我的第一个视图是UIScrollview,它占据了整个设备屏幕 嵌套在这个标签下,我有一个标签,解释列表的用途以及如何与之交互,然后是 UITableView 与来自 http://b2cloud.com.au/how-to-guides/reordering-a-uitableviewcell-from-any-touch-point 的委托方法的拖放

我面临的问题是,虽然脚本在有 1 或 2 行时运行良好,但当 UIscrollview 的内容大小大于屏幕时,它似乎优先于拖放导致不可预测的行为.

有什么方法可以让点击表格优先只编辑单元格并允许用户通过在视图上的其他地方进行交互来滚动?

谢谢

更新

根据下面的评论,我设法得到:

- (void)viewDidLoad
{
    [super viewDidLoad];
//
//
//

UIPanGestureRecognizer *tapGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasPanned:)];
    [self.view addGestureRecognizer:tapGesture];
}
-(void)wasPanned:(UIPanGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:scrollView];
    UIView *isTable = [scrollView hitTest:point withEvent:nil];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if([[[isTable class] description] isEqualToString:@"UITableViewCellReorderControl"])
        {
            NSLog(@"Dragged from within table");
            [scrollView setScrollEnabled: NO];

        }
        else
        {
            [scrollView setScrollEnabled:YES];
        }
    }
    else{
        [scrollView setScrollEnabled:YES];
    }
}

现在,当滚动视图不足以开始滚动时,NSLogs 消息正常 但是,当滚动视图较长时,它仅在滚动视图尚未开始滚动时才识别手势

更新

我现在让控制台可以 100% 地识别表格中的触摸并禁用滚动。然而,禁用滚动也会停止拖放功能。有谁知道为什么?

附加代码:

tapGesture.delegate = self;

#pragma mark UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

【问题讨论】:

  • 嘿,老兄看过了,stackoverflow.com/questions/9715582/…
  • 我读过一些类似的文章,并且一致的是,每当与 tableview 交互时,我都需要取消或禁用滚动行为。我曾尝试设置 canCancelContentTouches = No 但主要问题是表格视图在滚动开始之前没有注册任何交互...我将尝试其他一些方法/侦听器并报告:)

标签: ios objective-c uiscrollview


【解决方案1】:

所以我的最终(但不是完美的)解决方案是这样做:

.h

<UIGestureRecognizerDelegate>

.m (viewDidLoad)

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wasTapped:)];
    [self.view addGestureRecognizer:tapGesture];
    tapGesture.delegate = self;

.m

-(void)wasTapped:(UIPanGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:scrollView];
    UIView *isTable = [scrollView hitTest:point withEvent:nil];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if([[[isTable class] description] isEqualToString:@"UITableViewCellReorderControl"])
        {
            NSLog(@"Dragged from within table");
            [scrollView setScrollEnabled: NO];

        }
        else
        {
            [scrollView setScrollEnabled:YES];
        }
    }
    else{
        [scrollView setScrollEnabled:YES];
    }
}

点击手势比平移手势效果更好,因为平移手势似乎将按住、拖动、停止、拖动识别为两种不同的手势。该功能现在可以正常工作,但是如果您在单元格移动动画开始之前移动手指,它将滚​​动。您还必须等待视图(我可以称之为过度滚动吗?)动画完全停止并且滚动条在它抓取单元格之前消失。

如果有人可以改进它,我将不胜感激:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多