因此,我想显示一个用户可以缩放,平移和旋转的图像。
我希望图像适合屏幕(即保持宽高比,整个图像在屏幕上可见,并且图像的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/