ios - 如何获得缩放,平移和旋转以在 ScrollView 中工作

标签 ios objective-c cocoa-touch uiscrollview uiimageview

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

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

屏幕旋转时,我希望行为保持不变。

我已经进行了很多设置,但无法获得完美的性能。

我目前所拥有的:

UIScrollView作为其子元素的UIImageView

- (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的功能是什么?

最佳答案

我只是在GitHub上放置了一个仓库来做到这一点。它在Swift中,但是应该可以

PhotoSlideShow-Swift

关于ios - 如何获得缩放,平移和旋转以在 ScrollView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21237842/

相关文章:

ios - 我们可以在 WKImageView 中使用 GIF 吗?

ios - 将数组数据模型转换为字典(swift)

iphone - 用户界面自动化 : Failed to authorize rights with status: -60007

iphone - setNeedsDisplay并不总是调用drawRect

ios - iOS:如何指向当前UITableViewCell上的当前控件

ios - 当应用程序被删除时,存储在 NSUserDefaults 中的值会发生什么变化?

ios - 设置框架后 UITextView 内容偏移发生变化

ios - 适用于 iOS 的 Drum Sim 构建示例

cocoa-touch - 检查 UIView 是否被触摸?

ios - 如何在 Visual Studio for Mac 上更改 iOS 模拟器