检测到长按时没有调用touchesEnded

时间:2013-09-03 13:12:35

标签: iphone ios objective-c uitableview uiview

在我的应用程序中,我有一个UIView并排有两个UITableViews。在这些之上分层是UIView(全部在故事板中完成)。

enter image description here

它被设置为通过附加到顶部UIView,DnD覆盖视图(使用一些开源代码https://github.com/josh2112/iPad-UITableView-DnD-Demo)来拖放表格之间的单元格。所有这一切都很好,直到表格有足够的单元格来保证滚动 - 我无法让它们滚动,因为每当我触摸一个单元格时,它就会弹出来进行拖动。

所以,我想尝试从长按而不是立即制作弹出功能。这就是我所拥有的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog( @"touchesBegan" );
    UITouch* touch = [touches anyObject];
    longPress = NO;

    [self performSelector:@selector(longPress:) withObject:touch afterDelay:2.0];
}

- (void)longPress:(UITouch *)touch{
BOOL inDragSource = NO;
longPress = YES;
// Was the touch over a drag source?
for( UIView<DragSource>* dragSource in dragSources ) {
    CGPoint point = [touch locationInView:dragSource];
    if( CGRectContainsPoint( dragSource.bounds, point )) {
        NSLog( @"Detected touch inside drag source!!!" );
        inDragSource = YES;

        // Ask the drag source for a draggable
        UIView* draggable = [dragSource popItemForDragFrom:point];
        if( draggable != nil ) {
            NSLog( @"Start drag on thing: %@", draggable );
            dragOperation = [[DragOperation alloc] initWithDraggable:draggable dragSource:dragSource initialPoint:draggable.center];

            // Unattach the draggable from its parent view and attach it to the overlay view, using the touch
            // location as the center point.
            [self addSubview:draggable];
            draggable.center = [self convertPoint:draggable.center fromView:dragSource];
            draggable.layer.masksToBounds = NO;
            draggable.layer.cornerRadius = 8;
            draggable.layer.shadowOffset = CGSizeMake( 7, 7 );
            draggable.layer.shadowRadius = 5;
            draggable.layer.shadowOpacity = 0.5;

            [UIView animateWithDuration:0.1f animations:^{
                draggable.center = [touch locationInView:self];
                draggable.transform = CGAffineTransformMakeScale( 1.2f, 1.2f );
            }];

            // If the drag source is also a drop target, tell it the draggable is hovering over it.
            if( [dragSource conformsToProtocol:@protocol(DropTarget)] ) {
                [self notifyDraggableEntered:(UIView<DropTarget>*)dragSource atPoint:point];
            }

            return;
        }
    }
}

}  


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if( dragOperation == nil ) return;

UITouch* touch = [touches anyObject];

if(!longPress){
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(longPress:) object:touch];
}
else{
    [self setHighlight:NO onDropTarget:dragOperation.currentDropTarget];

    // If we have a current drop target, drop the draggable there
    if( dragOperation.currentDropTarget != nil ) {
        NSLog( @"Transferring draggable to drop target!" );
        CGPoint point = [touch locationInView:dragOperation.currentDropTarget];

        CGPoint adjustedPoint = [dragOperation.currentDropTarget actualDropPointForLocation:point];
        [self animateDropOn:dragOperation.currentDropTarget atPoint:adjustedPoint withDuration:0.1f];
    }
    else {
        // If the original drag source is also a drop target, put the draggable back in its
        // original spot.
        if( [dragOperation.dragSource conformsToProtocol:@protocol(DropTarget)] ) {
            NSLog( @"Transferring draggable back to source" );
            [self animateDropOn:(UIView<DropTarget>*)dragOperation.dragSource atPoint:dragOperation.initialPoint withDuration:0.3f];
        }
        else {
            // Otherwise, just kill it?
            NSLog( @"Killing draggable, nobody wants it :-(" );
            [dragOperation.draggable removeFromSuperview];
            dragOperation = 0;
        }
    }
}
}

所以现在,如果我触摸任何地方,触摸开始被触发,然后是longPress,即使它不是长按。 touchesEnded不会被取消以取消之前的执行请求。 touchesCancelled也没有被击中。

有什么明显的我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

您需要在控制器上添加UILongPressGestureRecognizer

我在UILongPressGestureRecognizer提供了UIButton,例如

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnCaptureTapped:)];
longPress.minimumPressDuration = 1.30f; // here you can set it as per your requirement.
[self.MyButtonName addGestureRecognizer:longPress]; 

UILongPressGestureRecognizer

的方法调用
- (void)btnCaptureTapped:(UILongPressGestureRecognizer*)gesture
{
   // do your  stuff
}

即使在UIButton上添加点击也适合您。

[self.MyButtonName addTarget:self action:@selector(MyButtonNameTapped:) forControlEvents:UIControlEventTouchUpInside];
相关问题