【发布时间】:2014-02-09 20:23:22
【问题描述】:
所以我想显示一个用户可以缩放、平移和旋转的图像。
我希望图像适合屏幕(即保持纵横比,整个图像在屏幕上可见,并且图像的 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 的作用/是什么?
【问题讨论】:
标签: ios objective-c cocoa-touch uiscrollview uiimageview