在我的应用程序中,我有一个并排带有两个UITableViews的UIView。这些视图之上是UIView(均在Storyboard中完成)。
通过将其附加到顶部的UIView DnD Overlay View(使用一些开源代码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也不会被击中。
我在这里做错什么了吗?
最佳答案
您需要在控制器上添加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];