只有当捏住单元格的imageView时,才能在UICollectionView的自定义单元格中放大/缩小UIImageView? [英] is possible to zoom in/out a UIImageView in custom cell of a UICollectionView only when pinch the cell's imageView?
问题描述
我有一个CollectionView,它有一个自定义单元格。
I have a CollectionView that have one custom cell.
我想在单元格中放大/缩小imageView,所以我在CollectionView.m中添加了捏合手势
I want to zoom in/out imageView in cell, so i add pinch gesture at CollectionView.m
当我向self.collectionView添加手势时,如下所示:
when i add gesture to self.collectionView, like this:
[self.collectionView addGestureRecognizer:pinchGesture];
它有效!我的cell.imageView可以放大/缩小,但是当我捏住其他地方时(不在单元格的imageView中,我的cell.imageView仍然用手势放大/缩小,我希望cell.imageView在用户捏住时放大/缩小cell.imageView,所以我尝试添加手势使用此代码:
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];
但不幸的是它不起作用。当我捏住单元格的imageView时没有任何事情发生。
but unfortunately it doesn't work. Nothing happen when i pinch the cell's imageView.
所以我的问题是,只有当用户捏住单元格的imageView而不是其他地方时才可以放大/缩小单元格的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.
推荐答案
收藏视图单元格
//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文件
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
注意:你需要创建从故事板开始的滚动视图,图像视图和集合视图的IBOutlet。
Note : You need to create an IBOutlet for scrollview, image view and collection view from storyboard.
这篇关于只有当捏住单元格的imageView时,才能在UICollectionView的自定义单元格中放大/缩小UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!