我正在使用 UICollectionView
生成图片库。我在 UICollectionView
单元格中使用了 UIImage
来加载图像。我需要通过长按(而不是单击)选择 UICollectionView
Cell。
- (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];
}
最佳答案
swift 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")
}
}
关于iphone - IOS:长按选择UICollectionView Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224384/