简体   繁体   English

放大UICollectionView中单个单元格的内容

[英]Zoom into the content of a single cell in a UICollectionView

I am trying to achieve the same behaviour as in the Photos app when one image is open in full screen; 当一个图像全屏打开时,我试图实现与照片应用程序中相同的行为; ie, I want to be able to scroll between my different images and also to pinch to zoom in any of them. 即,我希望能够在我的不同图像之间滚动,也可以捏缩放其中任何一个。 As in the Photos app, after I have zoomed into a particular image, I would like to be able to scroll between the different images of my gallery. 与照片应用程序一样,在我放大特定图像后,我希望能够在我的图库的不同图像之间滚动。

My situation is as follows: I have a UICollectionView , a custom UICollectionViewFlowLayout where the itemSize is set to be the size of the screen, and a custom UICollectionViewCell that has an UIImageView . 我的情况如下:我有一个UICollectionView ,一个自定义的UICollectionViewFlowLayout ,其中itemSize被设置为屏幕的大小,以及一个具有UIImageView的自定义UICollectionViewCell That works just fine scrolling through the different photos, the problem is scrolling into them. 这可以很好地滚动浏览不同的照片,问题是滚动到它们。

The first approach I tried is the one depicted in the WWDC 2012 session : 'Advanced Collection Views and Building Custom Layouts'. 我尝试的第一种方法是WWDC 2012会话中描述的方法:“高级集合视图和构建自定义布局”。 The effect one gets with this approach is the cell scaling properly but then it will overlap with the cells next to it creating an undesirable effect. 使用这种方法获得的效果是细胞正确缩放,但随后它将与其旁边的细胞重叠,从而产生不良影响。

The second approach is like the one suggested in WWDC 2010 session : 'Designing Apps with Scroll Views'. 第二种方法类似于WWDC 2010会议中建议的方法:“使用滚动视图设计应用程序”。 However, I am not entirely sure this approach would work having a UICollectionView instead of a UIScrollView + subviews. 但是,我不完全确定这种方法可以使用UICollectionView而不是UIScrollView +子视图。 I set my view controller as the UIScrollViewDelegate and override viewForZoomingInScrollView: where I return the UIImageView of the visible cell at that given point. 我将我的视图控制器设置为UIScrollViewDelegate并覆盖viewForZoomingInScrollView:其中我返回给定点的可见单元格的UIImageView。 Nothing happens if I do that. 如果我这样做,什么都不会发生。 I have also tried returning the cell and in that case the whole layout of the UICollectionView is messed up. 我也试过返回单元格,在这种情况下,UICollectionView的整个布局搞砸了。 I have taken a look at the sample project Apple put together ( PhotoScroller ) that successfully implements what I want, but in there they just have a UIImageView as a subview of the UIScrollView . 我已经看了Apple放在一起的示例项目( PhotoScroller ),它成功实现了我想要的东西,但在那里他们只有一个UIImageView作为UIScrollView的子视图。

I think that in order to implement the Photos app behaviour with a UICollectionView , I need to do that by going with the first approach and modifying the layout attributes of the other cells in my collection view and not only the one being zoomed in. 我认为,为了使用UICollectionView实现Photos应用程序行为,我需要通过第一种方法并修改集合视图中其他单元格的布局属性而不仅仅是放大的布局属性来实现。

That sounds like a quite low level solution to me and I was wondering if somebody is aware of a better approach to this problem. 这听起来像是一个非常低级别的解决方案,我想知道是否有人知道更好的方法来解决这个问题。

After a few more days investigating this, I came up with a solution where I had to make some compromises but it is acceptable for my requirements. 经过几天的调查,我想出了一个解决方案,我必须做出一些妥协但是我的要求是可以接受的。

What I ended up doing is to have a UIScrollView as the top-most view of my custom UICollectionViewCell and adding the UIImageView as a subview of that UIScrollView . 我最终做的是将UIScrollView作为我的自定义UICollectionViewCell的最顶层视图,并添加UIImageView作为该UIScrollView的子视图。 The first problem I need to solve when I added that is that UIScrollViews do not play well with AutoLayout as explained here . 第一个问题,我需要解决的时候我加那就是UIScrollViews不作为解释与自动版式玩好这里 So, if you want your image to be full screen both in portrait and landscape, remember to use the appropriate autoresizing masks. 因此,如果您希望图像在纵向和横向上都是全屏的,请记住使用适当的自动调整遮罩。 By doing that and returning my UIImageView in viewForZoomingInScrollView: I got the zooming effect I was looking for. 通过这样做并在viewForZoomingInScrollView中返回我的UIImageView:我得到了我正在寻找的缩放效果。

The compromise I had to do though was that I do not allow the user to scroll between different images if there is any zoom applied (ie, zoom scale is different than 1.0). 我必须做的妥协是,如果应用任何缩放(即缩放比例不同于1.0),我不允许用户在不同图像之间滚动。 Apart from that, it works fine. 除此之外,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM