放大整个UICollectionView [英] zoom entire UICollectionView

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

问题描述

我有一个iPad App,使用的是UICollectionView,每个UICollectionViewCell仅包含一个UIImage. 目前,我每页显示9张UIImage(3行* 3列),我有几页.

I have an iPad App where I'm using a UICollectionView and each UICollectionViewCell contains just a single UIImage. Currently I'm displaying per 9 UIImages (3 rows * 3 columns) per page, I have several pages.

我想使用Pinch Gesture缩放整个UICollectionView,以增加/减少每页显示的行/列的数量,最好是在Pinch手势过程中使用漂亮的缩放动画!

I would like to use Pinch Gesture to zoom on the entire UICollectionView to increase/decrease the number of row/columns displayed per page and the best would be to have beautiful zoom animation during the Pinch gesture!

当前,我已在UICollectionView上添加了捏手势.我捕获了Pinch Gesture事件,以使用比例因子来计算行数/列数,如果它已更改,则使用以下命令更新完整的UICollectionView:

Currently, I have added a Pinch Gesture on my UICollectionView. I catch the Pinch Gesture event to compute the number of rows/columns using the scale factor, if it has changed then I update the full UICollectionView using:

[_theCollectionView performBatchUpdates:^{
     [_theCollectionView deleteSections:[NSIndexSet indexSetWithIndex:0]];
     [_theCollectionView insertSections:[NSIndexSet indexSetWithIndex:0]];
 } completion:nil];

它可以工作,但是过渡期间我没有流畅的动画.

It works but I don't have smooth animation during the transition.

有什么主意吗? UICollectionView继承自UIScrollView,是否有可能重新使用UIScrollView捏手势功能以达到我的目标?

Any idea? UICollectionView inherits from UIScrollView, is there a possibility to re-use the UIScrollView Pinch gesture feature to reach my goal?

推荐答案

我假设您使用的是默认的UICollectionViewDelegateFlowLayout,对吗?然后,请确保您对委托方法做出了相应的响应,并且在发生捏合手势时,只需使布局无效即可.

I'm assuming you're using the default UICollectionViewDelegateFlowLayout, right? Then make sure you respond accordingly to the delegate methods, and when the pinch gesture occurs, simply invalidate the layout.

例如,如果要在捏紧时调整每个项目的大小:

For example, if I want to adjust the size of every item, while pinching:

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic,assign) CGFloat scale;
@property (nonatomic,weak)   IBOutlet UICollectionView *collectionView;

@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scale = 1.0;

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(didReceivePinchGesture:)];
    [self.collectionView addGestureRecognizer:gesture];

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50*self.scale, 50*self.scale);
}

- (void)didReceivePinchGesture:(UIPinchGestureRecognizer*)gesture
{
    static CGFloat scaleStart;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        scaleStart = self.scale;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        self.scale = scaleStart * gesture.scale;
        [self.collectionView.collectionViewLayout invalidateLayout];
    }
}

属性self.scale仅用于显示,您可以将相同的概念应用于任何其他属性,因为用户本人正在负担秤的计时,所以不需要beginUpdates/endUpdates.

The property self.scale is just for show, you can apply this same concept to any other attribute, this doesn't require a beginUpdates/endUpdates because the user himself is carrying the timing of the scale.

这是一个正在运行的项目,以防您想看到它的实际效果.

Here's a running project, in case you want to see it in action.

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

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