【问题标题】:Pinch To Zoom Effect on UIImageView inside scrollView?在滚动视图内的 UIImageView 上捏缩放效果?
【发布时间】:2013-05-28 11:40:24
【问题描述】:

我正在使用情节提要 (iOS 6.0) 为我的应用创建照片库查看器。这就是我的 imageViewController 在情节提要中的设置方式:

我已确保在 imageView 和 scrollView 上启用 userInteraction 和多次触摸。我想要做的是,在捏我想放大 imageView(最大比例 3)并能够平移。这是我目前拥有的,但是,即使检测到捏合手势,比例也不会改变。

- (IBAction)imagePinched:(id)sender {

if (pinchRecognizer.state == UIGestureRecognizerStateEnded || pinchRecognizer.state == UIGestureRecognizerStateChanged) {

    NSLog(@"gesture.scale = %f", pinchRecognizer.scale);

    CGFloat currentScale = self.fullScreenView.frame.size.width / self.fullScreenView.bounds.size.width;
    CGFloat newScale = currentScale * pinchRecognizer.scale;

    if (newScale < 1) {
        newScale = 1;
    }
    if (newScale > 3) {
        newScale = 3;
    }

    CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.fullScreenView.transform = transform;
        pinchRecognizer.scale = 1;
    }

}

大多数在线问题和教程都涉及以编程方式创建视图并执行此操作,但代码越少越好(在我看来)。让它与故事板一起使用的最佳方法是什么?提前谢谢!!!


更新:

这是我完整的 .m 文件代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Assign an image to this controller's imageView
    fullScreenView.image = [UIImage imageNamed:imageString];

    //Allows single and double tap to work
    [singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
}

- (IBAction)imageTapped:(id)sender {

    NSLog(@"Image Tapped.");

    //On tap, fade out viewController like the twitter.app
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)imageDoubleTapped:(id)sender {

    NSLog(@"Image Double Tapped.");

    //On double tap zoom into imageView to fill in the screen.
    [fullScreenView setContentMode:UIViewContentModeScaleAspectFill];
}

- (IBAction)imagePinched:(id)sender {

    if (pinchRecognizer.state == UIGestureRecognizerStateEnded || pinchRecognizer.state == UIGestureRecognizerStateChanged) {

        NSLog(@"gesture.scale = %f", pinchRecognizer.scale);

        CGFloat currentScale = self.fullScreenView.frame.size.width / self.fullScreenView.bounds.size.width;
        CGFloat newScale = currentScale * pinchRecognizer.scale;

        if (newScale < 1) {
            newScale = 1;
        }
        if (newScale > 3) {
            newScale = 3;
        }

        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.fullScreenView.transform = transform;
        pinchRecognizer.scale = 1;
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.fullScreenView;
}


-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

  • 我用你的问题解决了我的疑问。谢谢

标签: ios objective-c uiscrollview uiimageview pinchzoom


【解决方案1】:

我认为 Apple 文档中有更好的解决方案

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return self.imageView;

}
- (void)viewDidLoad {

    [super viewDidLoad];

    self.scrollView.minimumZoomScale=0.5;

    self.scrollView.maximumZoomScale=6.0;

    self.scrollView.contentSize=CGSizeMake(1280, 960);

    self.scrollView.delegate=self;

}

Check Apple Documentation

【讨论】:

    【解决方案2】:

    第一步是确保您的视图实现了正确的委托。例如在 .m 文件中

    @interface myRootViewController () <.., UIGestureRecognizerDelegate, UIScrollViewDelegate, ...>
    

    从文档中,确保你已经实现了这个:

    UIScrollView 类可以有一个必须采用 UIScrollViewDelegate 协议的委托。要使缩放和平移工作,代理必须同时实现 viewForZoomingInScrollView:scrollViewDidEndZooming:withView:atScale:;另外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同。

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
      return self.fullScreenView;
    }
    
    
    -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
    {}
    

    scrollViewDidEndZooming 方法现在可以保持为空。如果您已经这样做了,或者仍然无法正常工作,请发布更多代码,以便更具体地提供帮助。

    【讨论】:

    • 我将 Delegates 添加到 ImageViewController 并将这两个方法添加到我的代码中,但它似乎仍然没有触发缩放效果。我用更多代码更新了我的代码。感谢您的耐心和帮助!
    • 了解当手势识别器触发时这些 scrollView 方法是否被调用会很有帮助
    【解决方案3】:

    您需要按照上面 Wadi 的建议进行操作,但还要将 setMinimumZoomScale 设置为与 setMaximumZoomScale 不同。默认情况下,它们都是 1.0f。

    之后,UIImageView 应该是可捏合的

    【讨论】:

      【解决方案4】:

      我创建了一个完整的演示应用程序(本质上类似于 Facebook 的默认照片库),它演示了如何缩放嵌套在具有 AutoLayout 和情节提要的滚动视图中的图像视图。在此处查看我的项目:http://rexstjohn.com/facebook-like-ios-photo-modal-gallery-swipe-gestures/

      它还包括通过 MKNetworkKit 的异步照片加载和基于手势的照片库滑动。享受吧,我希望它可以节省每个人尝试解决这个问题的时间,因为这有点烦人。

      【讨论】:

        【解决方案5】:

        Here 是 Apple 建议您做您想做的事情的方式。我使用了 Apple 提供的代码,它就像一个魅力!如果你想优化内存管理,可以使用iCarousel(由nicklockwood制作),它对dealloc未使用的视图有缓存管理!

        【讨论】:

        • 谢谢 lucaslt89。一旦开发人员站点重新打开(当前收到“维护中”消息),我肯定会看看。是不是 ScrollView Suite 示例代码?
        • 当然,您可以从这里下载 WWDC 2010 的所有示例:link
        • 能否请您提供年份和会议编号。您发布的链接无效。
        • 本次会议是 WWDC 2010 的“使用滚动视图设计应用程序”,这里是 WWDC 2010 的所有视频的link
        【解决方案6】:

        我创建了一个类来处理 UIImageView 的缩放,类似于 Instagram。可能是你正在寻找的,否则你可以看看我是如何实现它的。 https://github.com/twomedia/TMImageZoom

        【讨论】:

          猜你喜欢
          • 2023-04-11
          • 2014-01-07
          • 2020-10-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-12
          • 2019-11-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多