【问题标题】:All Gestures in Array of Image图像数组中的所有手势
【发布时间】:2017-06-20 12:44:45
【问题描述】:

我已经说过 10 张图像,我必须在滑动或滚动时更改它们,并使用双击以在特定位置缩放,当图像缩放时平移将起作用,否则应保持并排捏合手势。

为此,我应用了逻辑,但滑动和平移手势之间存在冲突,因此我放弃了滑动的概念,我使用滚动视图并将 imageview 放置在其中,但是当一个图像被缩放并且我水平滚动时,下一个图像也会被缩放。

虽然我设置了条件以在滚动视图上获得相同大小的图像,但确实滚动但无法正常工作。

我使用了collectionview,在单元格内我放置了滚动视图,然后图像并应用了所有手势,但不起作用。

【问题讨论】:

  • 提示:设置委托并实现UIGestureRecognizer委托方法。
  • 试试 CollectionView。这是一个示例:stackoverflow.com/questions/33546395/…
  • 我试过那个代码,但它不起作用。也有问题提到..@krishnanunni

标签: ios objective-c uigesturerecognizer


【解决方案1】:

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

【讨论】:

  • 我知道,但我也不会平移。请帮帮我
  • 滚动视图代表也可以帮助您平移。使用 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return yourImageView; }
【解决方案2】:

根据 krishnanunni 的回答链接来自:“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 是一个布尔值,用于检查它是否被双击和缩放。

添加 UICollectionView 并在单元格内添加滚动视图,然后添加 imageview。排列如上链接所示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多