IOS:通过长按选择 UICollectionView 单元格
Posted
技术标签:
【中文标题】IOS:通过长按选择 UICollectionView 单元格【英文标题】:IOS: Selecting UICollectionView Cell by Long Press 【发布时间】:2013-08-14 06:11:23 【问题描述】:我正在使用UICollectionView
生成图片库。我在UICollectionView
单元格中使用UIImage
来加载图像。我需要通过长按(而不是单击)选择UICollectionView
单元格。
- (IBAction)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
UICollectionViewCell *cell=(UICollectionViewCell *)[gestureRecognizer view];
int index=cell.tag;
OverlayImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
OverlayImage.image = [UIImage imageNamed:@"Overlay@2x.png"];
[cell addSubview:OverlayImage];
【问题讨论】:
你可以使用UILongPressGestureRecognizer
【参考方案1】:
斯威夫特 4
it的更新答案
let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(longPressGR:)))
longPressGR.minimumPressDuration = 0.5
longPressGR.delaysTouchesBegan = true
self.collectionView.addGestureRecognizer(longPressGR)
@objc
func handleLongPress(longPressGR: UILongPressGestureRecognizer)
if longPressGR.state != .ended
return
let point = longPressGR.location(in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: point)
if let indexPath = indexPath
var cell = self.collectionView.cellForItem(at: indexPath)
print(indexPath.row)
else
print("Could not find index path")
【讨论】:
什么意思:“如果让 indexPath = indexPath ” 它解开indexPath,即self.collectionView.indexPathForItem(at: point)
【参考方案2】:
您可以使用LongPressGesture
UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 5;
[longpressGesture setDelegate:self];
[self.yourImage addGestureRecognizer:longpressGesture];
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer
NSLog(@"longPressHandler");
UIImageView *tempImage=(UIImageView*)[gestureRecognizer view];
【讨论】:
以上是关于IOS:通过长按选择 UICollectionView 单元格的主要内容,如果未能解决你的问题,请参考以下文章
H5开发的IOS App如何屏蔽长按选择文字弹出的气泡菜单?
即使禁用了文本选择,IOs Cordova 长按也会显示文本选择放大镜,如何删除?
BranchIO Universal Link iOS 数据仅在长按并选择打开时有效