is possible to zoom in/out a UIImageView in custom cell of a UICollectionView only when pinch the cell's imageView?

女生的网名这么多〃 提交于 2019-12-03 10:20:54

问题


I have a CollectionView that have one custom cell.

I want to zoom in/out imageView in cell, so i add pinch gesture at CollectionView.m

when i add gesture to self.collectionView, like this:

[self.collectionView addGestureRecognizer:pinchGesture];

it works! my cell.imageView can zoom in/out, but when i pinch other place(not in cell's imageView, my cell.imageView still zoom in/out with the gesture, I want the cell.imageView zoom in/out just when user pinch the cell.imageView, so i try to add gesture use this code:

[cell.imageView addGestureRecognizer:pinchGesture];

but unfortunately it doesn't work. Nothing happen when i pinch the cell's imageView.

so my question is, is possible to zoom in/out the cell's imageView only when user pinch the cell's imageView? not other place.


回答1:


Collection view cell

//cell.h

#import <UIKit/UIKit.h>

@interface Cell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIImageView *imageview;

- (void)setup;

@end


//cell.m

#import "Cell.h"
#define MAXIMUM_SCALE 3.0
#define MINIMUM_SCALE    1.0

@interface Cell()<UIScrollViewDelegate>

@end

@implementation Cell

- (void)setup {

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomImage:)];
    self.imageview.gestureRecognizers = @[pinch];
    self.imageview.userInteractionEnabled = YES;
    self.scrollview.delegate = self;

}

//-----------------------------------------------------------------------

#pragma  mark  - Scrollview Delegate Method

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageview;
}

//-----------------------------------------------------------------------

#pragma mark - Custom Methods

- (void)zoomImage:(UIPinchGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded
    || gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"gesture.scale = %f", gesture.scale);

        CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
        CGFloat newScale = currentScale * gesture.scale;

        if (newScale < MINIMUM_SCALE) {
            newScale = MINIMUM_SCALE;
        }
        if (newScale > MAXIMUM_SCALE) {
            newScale = MAXIMUM_SCALE;
        }

        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.imageview.transform = transform;
        self.scrollview.contentSize = self.imageview.frame.size;

    }

}

@end

ViewController files

//ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end



//ViewController.m[![enter image description here][1]][1]


#import "ViewController.h"
#import "Cell.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray  *imageArray;

@end

@implementation ViewController

//-----------------------------------------------------------------------

#pragma mark - Collectionview Datasource Methods

//-----------------------------------------------------------------------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.imageArray.count;
}

//-----------------------------------------------------------------------

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell setup];
    cell.imageview.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]];
    return cell;
}

//-----------------------------------------------------------------------

#pragma mark - Lifecycle method

- (void)viewDidLoad {
    [super viewDidLoad];

    _imageArray = @[ @"1.jpg", @"1.jpg" ];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

}

//-----------------------------------------------------------------------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Note : You need to create an IBOutlet for scrollview, image view and collection view from storyboard.




回答2:


By default, Image view's property userInteractionEnabled is set to NO, which disable all gesture recognizers. Setting the property to YES should solve your problem




回答3:


I recommend you to add use scrollview inside cell and image view in scroll view and enable the zoom for the scroll view not for your entire collectionview

 scrollViewMain.maximumZoomScale = 5.0;
    scrollViewMain.minimumZoomScale = 1.0;
    scrollViewMain.clipsToBounds = NO;
    scrollViewMain.delegate = self;

i think this will help you



来源:https://stackoverflow.com/questions/33546395/is-possible-to-zoom-in-out-a-uiimageview-in-custom-cell-of-a-uicollectionview-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!