【问题标题】:How to zoom a UIImageView within a UIScrollView?如何在 UIScrollView 中缩放 UIImageView?
【发布时间】:2015-01-24 23:32:22
【问题描述】:

我在 UIScrollView 中有一个 UIImageView,我可以垂直滚动,但不能水平滚动,因为我设置了内容大小。这正是我想要的。

但是,我似乎无法放大和缩小。我设置了最小和最大缩放比例,但它不起作用。

谁能告诉我为什么?

谢谢。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //self.scrollView.scrollEnabled = YES;

    CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.scrollView setContentSize:scrollableSize];

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
    tempImageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);

    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.minimumZoomScale = 1.0  ;
    self.scrollView.maximumZoomScale = tempImageView.image.size.width / self.scrollView.frame.size.width;
    self.scrollView.zoomScale = 1.0;
    self.scrollView.delegate = self;

    [self.scrollView addSubview:tempImageView];
}

【问题讨论】:

    标签: ios objective-c uiscrollview uiimageview


    【解决方案1】:

    您必须实现以下委托方法来进行缩放:

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

    并确保将self.imageView 添加到您的self.scrollView。 它应该是这样的:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
        [self.scrollView setContentSize:scrollableSize];
    
        self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
        self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
    
        self.scrollView.backgroundColor = [UIColor blackColor];
        self.scrollView.minimumZoomScale = 1.0  ;
        self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
        self.scrollView.delegate = self;
    
        [self.scrollView addSubview:self.imageView];
    }
    

    【讨论】:

    • 你说得对,我改了但还是不行:(
    • 感谢它完美运行。最后一个问题,self.scrollView.scrollEnabled = YES 的目的是什么,因为我将其注释掉并且它仍然可以正常滚动?
    • 如果你想动态禁用滚动,那是为了禁用滚动。您不需要它,因为您希望它滚动。 :)
    【解决方案2】:

    要使缩放起作用,您需要提供一个委托方法,该方法返回您希望缩放的视图:

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

    您还需要保留将tempImageView 分配给属性,以便您可以在此处引用它。如果您不想在属性中保留它,您将需要一些其他方式来识别视图,例如scrollView.subviews[0] 如果它是唯一的子视图。

    【讨论】:

      【解决方案3】:

      试试这个试试这个

      [scroll setUserInteractionEnabled:YES];

      而且你的最大缩放比例很奇怪。尝试使用 2,0 进行测试。

      【讨论】:

      • 好的。 contentSize 应该是 UIImageview 大小,而不是 ScrollView 大小。
      猜你喜欢
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2012-08-16
      • 2012-11-24
      • 2017-01-06
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多