简体   繁体   中英

Zoom UICollectionView via pinch gesture

I have a UICollectionView with a lot of cells inside (about 5k+). I want to make pinch to zoom in/out. I've tried to invalidateLayout on every pinch. It's really slow. SO I want to use CGAffineTransformMakeScale but I don't know how to scroll after this. My code is:

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

if (gesture.state == UIGestureRecognizerStateBegan) {
    scaleStart = self.venueLayoutZoom;
}
else if (gesture.state == UIGestureRecognizerStateChanged) {
    CGAffineTransform transform = CGAffineTransformMakeScale(self.venueLayoutZoom, self.venueLayoutZoom);
    self.activeCollectionNode.view.transform = transform;
    self.activeCollectionNode.view.contentSize = CGSizeMake(318 * self.venueLayoutZoom, 500 * self.venueLayoutZoom);
}
}

But when is zoomed in I can't scroll left and right. Help me out.

use

@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];
  }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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