Artem Z.
Artem Z.

Reputation: 1243

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.

Upvotes: 0

Views: 818

Answers (1)

Vikas Rajput
Vikas Rajput

Reputation: 1874

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

Upvotes: 0

Related Questions