问题描述
我正在尝试在UICollectionViewCell
内添加UIScrollView
.这个想法是,您可以使用捏来缩放UIScrollView
(以及其中的图像),但是scrollview似乎无法处理任何手势.我猜他们被UICollectionView
抓住了.
I'm trying to add a UIScrollView
inside of a UICollectionViewCell
. The idea is that you can use pinch to zoom the UIScrollView
(and with it, the image within), but the scrollview doesn't seem to handle any gesture. I'm guessing they are being caught by the UICollectionView
.
我已经将UIScrollView
的委托设置为UICollectionViewCell
,但是没有一个委托方法被调用.
I've set the delegate of the UIScrollView
to be the UICollectionViewCell
, but none of the delegate methods are being called.
我已经用代码制作了一个github回购协议(尽可能简化了).即使只有几行代码,我也看不到自己做错了什么.
I've made a github repo with the code (simplified as much as I could). Even though it's just a few lines of code, I cannot see what I did wrong.
找到答案后,我将修复程序添加到上述仓库中,希望其他人也对它有所帮助:)
After the answer was found, I added the fixes to the above-mentioned repo, hope others find it helpful too :)
https://github.com/krummler/gallery-pinchzoom-example
推荐答案
您可能想要尝试操作UIGestureRecognizer,以实现此目的.在GalleryViewController
:
You might want to try manipulating the UIGestureRecognizers in order to do that. In the GalleryViewController
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
GalleryImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"galleryImageCell" forIndexPath:indexPath];
ImageContext *imageContext = [self.images objectAtIndex:indexPath.row];
cell.imageContext = imageContext;
[self.collectionView addGestureRecognizer:cell.scrollView.pinchGestureRecognizer];
[self.collectionView addGestureRecognizer:cell.scrollView.panGestureRecognizer];
return cell;
}
来自 Apple在UIView上的文档:
因此,您还需要确保在不再显示该单元格时将其删除.
So you'll also want to make sure to remove them when the cell is not showing anymore.
- (void)collectionView:(UICollectionView *)collectionView
didEndDisplayingCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the cell instance and ...
[self.collectionView removeGestureRecognizer:cell.scrollView.pinchGestureRecognizer];
[self.collectionView removeGestureRecognizer:cell.scrollView.panGestureRecognizer];
}
由于您不修改UIGestureRecognizer的委托,仅修改其范围,它仍将仅控制该单元格的滚动视图的缩放.
Since you're not modifying the UIGestureRecognizer's delegate, only its scope, it will still control the zooming of just that cell's scrollview.
编辑:
根据OP的建议,我在上述示例中添加了panGestureRecognizer
.缩放本身完全由pinchGestureRecognizer
处理,但是的确如此,在大多数情况下,将图像缩放到仅能看到其一部分的点之后,您将需要平移以将可见部分四处移动.也就是说,这是正确缩放体验的一部分.
I added the panGestureRecognizer
to the above examples, following a suggestion from the OP that it was needed. The zooming itself is completely handled by the pinchGestureRecognizer
, but it's true that in most cases, after zooming an image to a point where only a subset of it is visible, you'll want to pan to move the visible portion around. That is, it's part of a proper zooming experience.
这篇关于如何在UICollectionViewCell内缩放UIScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!