gpt4 book ai didi

iphone - 检测到长按时未调用 touchesEnded

转载 作者:行者123 更新时间:2023-11-29 13:06:08 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个 UIView 并排有两个 UITableView。位于这些之上的是一个 UIView(全部在 Storyboard中完成)。

enter image description here

它被设置为通过附加到顶部 UIView、DnD 覆盖 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,然后是长按,即使不是长按也是如此。 touchesEnded 不会被命中以取消先前的执行请求。 touchesCancelled 也不会被击中。

这里有什么明显的错误吗?

最佳答案

你需要在你的 Controller 上添加UILongPressGestureRecognizer

这里我在UIButton上提供了UILongPressGestureRecognizer作为例子

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];

关于iphone - 检测到长按时未调用 touchesEnded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18593268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com