图像数组中的所有手势
All Gestures in Array of Image
我已经说了 10 张图像,我必须在滑动或滚动时更改这些图像,并使用双击在特定位置缩放,当图像缩放时平移会起作用,否则不会,应该保持并排捏合手势。
为此,我应用了逻辑,但滑动和平移手势之间存在冲突,所以我放弃了滑动的概念,我使用滚动视图并将图像视图放在里面但是当一个图像被缩放并且我水平滚动时下一个图像也会被缩放.
虽然我设置了在 scrollview 上获得相同大小的图像的条件,但它确实滚动了但无法正常工作。
我使用了 collectionview 和 inside cell 我放置了 scrollview 然后是图像并应用了所有的手势但是不起作用。
尝试使用滚动视图委托。不要使用平移手势,只需使用双击缩放和滑动手势。效果很好。
根据 krishnanunni 的回答 link 来自:“".I modified Bit And it works wellArrangement Same as from Sample
in gallerycell.m
@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 {
CGFloat newScale = MINIMUM_SCALE;
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.imgView.transform = transform;
self.scrView.contentSize = self.imgView.frame.size;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.delegate = self;
[self.scrView addGestureRecognizer:doubleTap];
[doubleTap setNumberOfTapsRequired:2];
ZoomCheck = YES;
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomImage:)];
pinch.delegate = self;
self.imgView.gestureRecognizers = @[pinch];
self.imgView.userInteractionEnabled = YES;
self.scrView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imgView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
}
- (void)centerScrollViewContents {
// This method centers the scroll view contents also used on did zoom
CGSize boundsSize = self.scrView.bounds.size;
CGRect contentsFrame = self.imgView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imgView.frame = contentsFrame;
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture {
NSLog(@"TAPCALLED");
CGPoint location = [gesture locationInView:self.imgView];
NSLog(@"x %f y %f",location.x, location.y);
CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
NSLog(@"%.f",currentScale);
CGPoint pointInView = [gesture locationInView:self.imgView];
if(ZoomCheck){
// Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
CGFloat newZoomScale = self.scrView.zoomScale * 3.0f;
newZoomScale = MIN(newZoomScale, self.scrView.maximumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.scrView.bounds.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = pointInView.x - (w / 2.0f);
CGFloat y = pointInView.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrView zoomToRect:rectToZoomTo animated:YES];
ZoomCheck=NO;
}
else{
CGFloat newZoomScale = self.scrView.zoomScale / 3.0f;
newZoomScale = MIN(newZoomScale, self.scrView.minimumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.scrView.bounds.size;
CGFloat w = scrollViewSize.width * newZoomScale;
CGFloat h = scrollViewSize.height * newZoomScale;
CGFloat x = pointInView.x - (w * 2.0f);
CGFloat y = pointInView.y - (h * 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrView zoomToRect:rectToZoomTo animated:YES];
ZoomCheck=YES;
}
}
//-----------------------------------------------------------------------
#pragma mark - Custom Methods
- (void)zoomImage:(UIPinchGestureRecognizer *)gesture {
if (UIGestureRecognizerStateBegan == gesture.state ||
UIGestureRecognizerStateChanged == gesture.state) {
// Use the x or y scale, they should be the same for typical zooming (non-skewing)
float currentScale = [[gesture.view.layer valueForKeyPath:@"transform.scale.x"] floatValue];
// Variables to adjust the max/min values of zoom
float minScale = 1.0;
float maxScale = 3.0;
float zoomSpeed = .5;
float deltaScale = gesture.scale;
// You need to translate the zoom to 0 (origin) so that you
// can multiply a speed factor and then translate back to "zoomSpace" around 1
deltaScale = ((deltaScale - 1) * zoomSpeed) + 1;
// Limit to min/max size (i.e maxScale = 2, current scale = 2, 2/2 = 1.0)
// A deltaScale is ~0.99 for decreasing or ~1.01 for increasing
// A deltaScale of 1.0 will maintain the zoom size
deltaScale = MIN(deltaScale, maxScale / currentScale);
deltaScale = MAX(deltaScale, minScale / currentScale);
CGAffineTransform zoomTransform = CGAffineTransformScale(gesture.view.transform, deltaScale, deltaScale);
gesture.view.transform = zoomTransform;
// Reset to 1 for scale delta's
// Note: not 0, or we won't see a size: 0 * width = 0
gesture.scale = 1;
}
}
@end
注意:Zoomcheck 是一个布尔值,用于检查之前是否双击和缩放。
添加 UICollectionView 并在单元格内添加 scrollview 然后imageview.the排列如上link所示
我已经说了 10 张图像,我必须在滑动或滚动时更改这些图像,并使用双击在特定位置缩放,当图像缩放时平移会起作用,否则不会,应该保持并排捏合手势。
为此,我应用了逻辑,但滑动和平移手势之间存在冲突,所以我放弃了滑动的概念,我使用滚动视图并将图像视图放在里面但是当一个图像被缩放并且我水平滚动时下一个图像也会被缩放.
虽然我设置了在 scrollview 上获得相同大小的图像的条件,但它确实滚动了但无法正常工作。
我使用了 collectionview 和 inside cell 我放置了 scrollview 然后是图像并应用了所有的手势但是不起作用。
尝试使用滚动视图委托。不要使用平移手势,只需使用双击缩放和滑动手势。效果很好。
根据 krishnanunni 的回答 link 来自:“
in gallerycell.m
@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 {
CGFloat newScale = MINIMUM_SCALE;
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.imgView.transform = transform;
self.scrView.contentSize = self.imgView.frame.size;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.delegate = self;
[self.scrView addGestureRecognizer:doubleTap];
[doubleTap setNumberOfTapsRequired:2];
ZoomCheck = YES;
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomImage:)];
pinch.delegate = self;
self.imgView.gestureRecognizers = @[pinch];
self.imgView.userInteractionEnabled = YES;
self.scrView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imgView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
}
- (void)centerScrollViewContents {
// This method centers the scroll view contents also used on did zoom
CGSize boundsSize = self.scrView.bounds.size;
CGRect contentsFrame = self.imgView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imgView.frame = contentsFrame;
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture {
NSLog(@"TAPCALLED");
CGPoint location = [gesture locationInView:self.imgView];
NSLog(@"x %f y %f",location.x, location.y);
CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
NSLog(@"%.f",currentScale);
CGPoint pointInView = [gesture locationInView:self.imgView];
if(ZoomCheck){
// Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
CGFloat newZoomScale = self.scrView.zoomScale * 3.0f;
newZoomScale = MIN(newZoomScale, self.scrView.maximumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.scrView.bounds.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = pointInView.x - (w / 2.0f);
CGFloat y = pointInView.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrView zoomToRect:rectToZoomTo animated:YES];
ZoomCheck=NO;
}
else{
CGFloat newZoomScale = self.scrView.zoomScale / 3.0f;
newZoomScale = MIN(newZoomScale, self.scrView.minimumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.scrView.bounds.size;
CGFloat w = scrollViewSize.width * newZoomScale;
CGFloat h = scrollViewSize.height * newZoomScale;
CGFloat x = pointInView.x - (w * 2.0f);
CGFloat y = pointInView.y - (h * 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrView zoomToRect:rectToZoomTo animated:YES];
ZoomCheck=YES;
}
}
//-----------------------------------------------------------------------
#pragma mark - Custom Methods
- (void)zoomImage:(UIPinchGestureRecognizer *)gesture {
if (UIGestureRecognizerStateBegan == gesture.state ||
UIGestureRecognizerStateChanged == gesture.state) {
// Use the x or y scale, they should be the same for typical zooming (non-skewing)
float currentScale = [[gesture.view.layer valueForKeyPath:@"transform.scale.x"] floatValue];
// Variables to adjust the max/min values of zoom
float minScale = 1.0;
float maxScale = 3.0;
float zoomSpeed = .5;
float deltaScale = gesture.scale;
// You need to translate the zoom to 0 (origin) so that you
// can multiply a speed factor and then translate back to "zoomSpace" around 1
deltaScale = ((deltaScale - 1) * zoomSpeed) + 1;
// Limit to min/max size (i.e maxScale = 2, current scale = 2, 2/2 = 1.0)
// A deltaScale is ~0.99 for decreasing or ~1.01 for increasing
// A deltaScale of 1.0 will maintain the zoom size
deltaScale = MIN(deltaScale, maxScale / currentScale);
deltaScale = MAX(deltaScale, minScale / currentScale);
CGAffineTransform zoomTransform = CGAffineTransformScale(gesture.view.transform, deltaScale, deltaScale);
gesture.view.transform = zoomTransform;
// Reset to 1 for scale delta's
// Note: not 0, or we won't see a size: 0 * width = 0
gesture.scale = 1;
}
}
@end
注意:Zoomcheck 是一个布尔值,用于检查之前是否双击和缩放。
添加 UICollectionView 并在单元格内添加 scrollview 然后imageview.the排列如上link所示