图像阵列中的所有手势

时间:2017-02-03 07:50:33

标签: ios objective-c uigesturerecognizer

我已经说过10张图片,我必须在滑动或滚动时更改,然后使用双击来缩放特定位置,当图像变亮时,平移将会起作用,并且应该保持并排捏合手势。

为此,我应用逻辑,但是滑动和平移手势之间存在冲突,所以我放弃了滑动的概念,我使用scrollview并将imageview放在里面但是当一个图像被缩放并且我水平滚动时,下一个图像也会被缩放。 / p>

虽然我把条件设置为在scrollview上获得相同大小的图像但滚动但不会工作。

我使用了collectionview,在内部单元格中我放置了scrollview然后是图像并应用了所有的手势,但不会工作。

2 个答案:

答案 0 :(得分:0)

尝试使用滚动视图委托。不要使用平移手势只需使用双击缩放和滑动手势。它工作正常。

答案 1 :(得分:0)

根据krishnanunni的回答链接From:“is possible to zoom in/out a UIImageView in custom cell of a UICollectionView only when pinch the cell's imageView?”。我修改了Bit并且效果很好Arrangement 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是一个bool,用于检查它是否被双击并先前缩放。

添加UICollectionView并在单元格内添加scrollview,然后添加imageview。该排列显示在上面的链接

相关问题