【问题标题】:How to get zooming, panning, and rotating to work for image in scrollview如何获得缩放、平移和旋转以适用于滚动视图中的图像
【发布时间】:2014-02-09 20:23:22
【问题描述】:

所以我想显示一个用户可以缩放、平移和旋转的图像。

我希望图像适合屏幕(即保持纵横比,整个图像在屏幕上可见,并且图像的 2 个相对边缘接触到屏幕边缘)。

当屏幕旋转时,我希望保持这种行为。

我玩过很多设置,但无法获得完美的行为。

我目前拥有的:

UIScrollViewUIImageView 作为其子级。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.imageView.image = [UIImage imageNamed:@"test2.jpg"];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.clipsToBounds = YES;
}

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    CGFloat width = self.scrollView.frame.size.width;
    CGFloat height = self.scrollView.frame.size.height;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        NSLog(@"Screen rotated to landscape orientation.");
        self.scrollView.frame = CGRectMake(0, 0, height, width);

        [self.scrollView setZoomScale:1 animated:YES];
    }
    else
    {
        NSLog(@"Screen rotated to portrait orientation.");
        self.scrollView.frame = CGRectMake(0, 0, width, height);

        [self.scrollView setZoomScale:1 animated:YES];
    }
}

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

当前代码的行为在纵向模式下对我来说非常有效。图像适合屏幕并居中。景观的行为并不像我想要的那样工作。

我完全不明白为什么[self.scrollView setZoomScale:1 animated:YES]; 有效。它似乎适用于任何尺寸的图像。也许我不太明白 zoomScale 的作用/是什么?

【问题讨论】:

    标签: ios objective-c cocoa-touch uiscrollview uiimageview


    【解决方案1】:

    我只是在 GitHub 上放了一个 repo 来做这件事。它在 Swift 中,但它应该这样做

    PhotoSlideShow-Swift

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2018-12-13
      • 2014-02-24
      • 1970-01-01
      • 2015-01-05
      相关资源
      最近更新 更多