放大二维 UICollectionView [英] Zoom on a two dimensional UICollectionView

本文介绍了放大二维 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个水平和垂直的 UICollectionView.它有不同的 UICollectionViewCells.一切都布局正确.现在我正在尝试使其可缩放.UICollectionViewCells 也已正确调整大小.每次 UIPinchGesture 发生时,我都会在 UICollectionViewLayout 中设置 itemSize 依赖于 scale.

I created a UICollectionView which is horizontal and vertically. It has different UICollectionViewCells. Everything is layouted correctly. Now I am trying to make it zoomable. The UICollectionViewCells are resized correctly too. Every time the UIPinchGesture occures, I set the itemSize inside the UICollectionViewLayout dependend on the scale.

TestLayout *layout =  (TestLayout *) self.collectionViewLayout;
CGSize newItemSize = CGSizeMake(_sizeItem.width * gesture.scale, 
                                _sizeItem.height * gesture.scale);
[layout setItemSize:newItemSize];

在这里您可以看到我在 CustomLayout 中调用的方法 setItemSize.

Here you can see the method setItemSize I am calling inside my CustomLayout.

- (void)setItemSize:(CGSize)itemSize
{
    if (CGSizeEqualToSize(self.itemSize, itemSize)) return;

    _itemSize = itemSize;

//    [self prepareLayout];
    [self invalidateLayout];
}

我的问题是知道,所有项目 resize 到右下角,我不知道如何准确地关注我的 UIPinchGesture 所在的元素.

My problem is know, all items resize to the right bottom and I don't know how to focus exactly on the element my UIPinchGesture was on.

每次 gesture 发生这样的情况时,我都尝试更改 contentOffset:

I tried to change the contentOffset every time the gesture occures like this:

CGPoint posInView = [gesture locationInView:self];
CGPoint pointPinchTouch = CGPointMake(posInView.x - self.contentOffset.x,
                                      posInView.y - self.contentOffset.y);
CGPoint newOffset = CGPointMake(self.contentOffset.x * (gesture.scale * 2),
                                self.contentOffset.y * (gesture.scale * 2));
[self setContentOffset:newOffset animated:NO];

但我从未设法留在 CGPoint 我的 UIPinchGesture 被执行.

But I never managed to stay on the CGPoint my UIPinchGesture was executed.

另外当scrolling对整个UICollectionView,我的contentOffset还是{0,0}的时候scroll 没有结束.所以开始捏我总是在左上角结束.因为 UICollectionView 似乎没有被设计为同时使用水平和垂直,这就是为什么我也不能使用 UIScrollView<的 delegate 方法/code> 用于缩放.也许有人可以告诉我如何解决这个问题.

Furthermore when scrolling on the whole UICollectionView, my contentOffset is still {0,0} when the scroll didn't end. So start pinching I always end up in the top left corner. Because the UICollectionView seems not to be designed to be used horizontal and vertical at the same time, thats why I also can't use the delegate methods of UIScrollView for zooming. Maybe somebody can tell me how to solve this problem.

推荐答案

让我看看我是否理解问题所在.您正试图通过更改项目大小来放大特定单元格,但所有单元格的大小都在改变,而不是被挤压的那个?

Let me see if I understand the problem. You are trying to zoom in on a specific cell by changing the item size, but the size of ALL the cells are changing instead of the one that was pinched?

如果我的理解是正确的,那么我可以提供帮助.更改 FlowLayout.itemSize 会更改集合视图中所有单元格的大小.如果要设置特定单元格的项目大小,则应覆盖 UICollectionViewDelegateFlowLayout 的 sizeForItemAtIndexPath.在 sizeForItemAtIndexPath 方法中,您可以检查 itemAtIndexPath 与被捏合​​的单元格并仅为该单元格返回不同的大小.

If my understanding is correct, then I can help. Changing FlowLayout.itemSize changes the size for all cells in the collection view. If you want to set the item size of specific cells, you should override UICollectionViewDelegateFlowLayout's sizeForItemAtIndexPath. In the sizeForItemAtIndexPath method, you can check the itemAtIndexPath against the cell that was pinched and return a different size just for that cell.

您问题的另一部分(关于滚动和偏移)对我来说并不是很清楚.你是对的,集合视图应该水平或垂直使用,但不能同时使用.也许这就是导致问题的原因?

The other part of your question (about the scrolling and offset) is not really clear to me. You are right that collection views are supposed to be used either horizontally or vertically but not both. Perhaps that is what causes the problem?

这篇关于放大二维 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆