gpt4 book ai didi

uitableview - 如何为 iPad 创建类似于 Safari 工具栏自定义的拖放功能

转载 作者:行者123 更新时间:2023-12-04 06:09:18 25 4
gpt4 key购买 nike

我有一个 UIViewController,上面有一个 UITable 和一个 UIView。我希望能够拾取 UIView 中显示的项目并将它们放到 UITableView 中的单元格上。我不得不恢复使用触摸事件而不是新的 UIGestureRecognisers 来拍摄被点击的 UIView 的快照以便将此快照拖到 UITableView 而不是触摸的 UIView。使用以下方法效果很好,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t =[touches anyObject];

UIGraphicsBeginImageContext(t.view.bounds.size);
[t.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.draggedView = [[UIImageView alloc] initWithImage:img];

CGPoint centre = [[touches anyObject] locationInView:self.view];
[self.draggedView setCenter:centre];
[self.draggedView setAlpha:0.5];
[self.view addSubview:self.draggedView];
}

但是,在 touchesEnded 事件中,当我尝试评估触摸在哪个 UIView 上结束时,当我放下它时,我总是得到一个 UIView 而不是 UITableView。任何想法都会非常受欢迎。

最佳答案

我想我已经使用最新的 GestureRecognisers 为这个问题提出了更好的解决方案。我在基本 TableView Controller 中使用以下 LongPress 手势识别器。

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
[gestureRecognizer addTarget:self action:@selector(longGestureAction:)];
}
return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

-(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{
UITableViewCell *cell= (UITableViewCell *)[gesture view];

switch ([gesture state]) {
case UIGestureRecognizerStateBegan:{
NSIndexPath *ip = [self.tableView indexPathForCell:cell];
[self.tableView setScrollEnabled:NO];
if(ip!=nil){
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureWillBegin:gesture forCell:cell];
UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self ];
//switch the view the gesture is associated with this will allow the dragged view to continue on where the cell leaves off from
[draggedView addGestureRecognizer:[[cell gestureRecognizers]objectAtIndex:0]];
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidBegin:gesture forCell:cell];
}
}
break;
case UIGestureRecognizerStateChanged:{
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidMove:gesture];
}
break;
case UIGestureRecognizerStateEnded:{
UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self];
if(draggedView==nil)
return;

//this does not seem like the best way to do this yet you really don't want to fire one after the other I don't think
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidEnd:gesture];
[self.dropableDelegate dragAndDropTableViewController:self droppedGesture:gesture];

[self.tableView setScrollEnabled:YES];
[self.tableView reloadData];
}
break;

// case UIGestureRecognizerStateCancelled:
// case UIGestureRecognizerStateFailed:
// case UIGestureRecognizerStatePossible:
// [self.dragAndDropDelegate dragAndDropTableViewController:self draggingGesture:gesture endedForItem:nil];
break;
default:
break;
}
}

在扩展此基类的 TableViews 中,我将以下内容添加到 cellForIndexPath TableViewDataSource 中的每个单元格,
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];

最后你需要做的就是像这样实现委托(delegate)方法,
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
[gestureRecognizer addTarget:self action:@selector(longGestureAction:)];
}
return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

-(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{
UITableViewCell *cell= (UITableViewCell *)[gesture view];

switch ([gesture state]) {
case UIGestureRecognizerStateBegan:{
NSIndexPath *ip = [self.tableView indexPathForCell:cell];
[self.tableView setScrollEnabled:NO];
if(ip!=nil){
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureWillBegin:gesture forCell:cell];
UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self ];
//switch the view the gesture is associated with this will allow the dragged view to continue on where the cell leaves off from
[draggedView addGestureRecognizer:[[cell gestureRecognizers]objectAtIndex:0]];
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidBegin:gesture forCell:cell];
}
}
break;
case UIGestureRecognizerStateChanged:{
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidMove:gesture];
}
break;
case UIGestureRecognizerStateEnded:{
UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self];
if(draggedView==nil)
return;

//this does not seem like the best way to do this yet you really don't want to fire one after the other I don't think
[self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidEnd:gesture];
[self.dropableDelegate dragAndDropTableViewController:self droppedGesture:gesture];

[self.tableView setScrollEnabled:YES];
[self.tableView reloadData];
}
break;

// case UIGestureRecognizerStateCancelled:
// case UIGestureRecognizerStateFailed:
// case UIGestureRecognizerStatePossible:
// [self.dragAndDropDelegate dragAndDropTableViewController:self draggingGesture:gesture endedForItem:nil];
break;
default:
break;
}
}

您可以找到此 here 的来源.我花了很长时间才做到这一点,所以我真的希望这可以节省其他人的时间。

关于uitableview - 如何为 iPad 创建类似于 Safari 工具栏自定义的拖放功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961712/

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