在我的应用程序中,我有一个 UIView 并排有两个 UITableViews。位于这些之上的是 UIView(全部在 Storyboard 中完成)。
它被设置为通过附加到顶部 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;
}
}
}
}
所以现在,如果我触摸任何地方,touchesBegan 会被触发,然后 longPress,即使它不是长按。touchesEnded 不会被击中以取消先前的执行请求。touchesCancelled 也没有被击中。
有什么明显的我做错了吗?