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

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

问题描述

我创建了一个水平和垂直的UICollectionView.它具有不同的UICollectionViewCells.一切都正确布局.现在,我尝试使其成为zoomable. UICollectionViewCells也将正确调整大小.每次出现UIPinchGesture时,我都将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.

此外,当整个UICollectionView上的scrolling时,当scroll没有结束时,我的contentOffset仍然是{0,0}.因此,开始捏捏我总是会出现在左上角. 因为UICollectionView似乎不能同时设计为水平和垂直使用,所以这就是为什么我也不能将UIScrollViewdelegate方法用于zooming的原因. 也许有人可以告诉我如何解决这个问题.

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天全站免登陆