仅当捏住单元格的 imageView 时,才可以在 UICollectionView 的自定义单元格中缩放 in/out UIImageView?
is possible to zoom in/out a UIImageView in custom cell of a UICollectionView only when pinch the cell's imageView?
我有一个包含一个自定义单元格的 CollectionView。
我想在单元格中缩放 in/out imageView,所以我在 CollectionView.m
处添加了捏手势
当我向 self.collectionView 添加手势时,像这样:
[self.collectionView addGestureRecognizer:pinchGesture];
有效!我的 cell.imageView 可以缩放 in/out,但是当我捏其他地方(不在单元格的 imageView 中,我的 cell.imageView 仍然用手势缩放 in/out,我想要 cell.imageView 缩放 in/out 就在用户捏 cell.imageView 时,所以我尝试使用此代码添加手势:
[cell.imageView addGestureRecognizer:pinchGesture];
但不幸的是它不起作用。当我捏住单元格的 imageView 时没有任何反应。
所以我的问题是,只有当用户捏住单元格的图像视图时,才可以缩放 in/out 单元格的图像视图吗?不是其他地方。
默认情况下,图像视图的 属性 userInteractionEnabled
设置为 NO,这会禁用所有手势识别器。将 属性 设置为 YES 应该可以解决您的问题
我建议您在单元格中添加使用滚动视图,在滚动视图中添加图像视图
并为滚动视图启用缩放而不是为整个 collectionview
scrollViewMain.maximumZoomScale = 5.0;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.clipsToBounds = NO;
scrollViewMain.delegate = self;
我想这对你有帮助
集合视图单元格
//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.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。
我有一个包含一个自定义单元格的 CollectionView。
我想在单元格中缩放 in/out imageView,所以我在 CollectionView.m
处添加了捏手势当我向 self.collectionView 添加手势时,像这样:
[self.collectionView addGestureRecognizer:pinchGesture];
有效!我的 cell.imageView 可以缩放 in/out,但是当我捏其他地方(不在单元格的 imageView 中,我的 cell.imageView 仍然用手势缩放 in/out,我想要 cell.imageView 缩放 in/out 就在用户捏 cell.imageView 时,所以我尝试使用此代码添加手势:
[cell.imageView addGestureRecognizer:pinchGesture];
但不幸的是它不起作用。当我捏住单元格的 imageView 时没有任何反应。
所以我的问题是,只有当用户捏住单元格的图像视图时,才可以缩放 in/out 单元格的图像视图吗?不是其他地方。
默认情况下,图像视图的 属性 userInteractionEnabled
设置为 NO,这会禁用所有手势识别器。将 属性 设置为 YES 应该可以解决您的问题
我建议您在单元格中添加使用滚动视图,在滚动视图中添加图像视图 并为滚动视图启用缩放而不是为整个 collectionview
scrollViewMain.maximumZoomScale = 5.0;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.clipsToBounds = NO;
scrollViewMain.delegate = self;
我想这对你有帮助
//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.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。