[英]Setting up a Zooming UIScrollView with a UIImageView
我正在尝试设置一个UIScrollView子类,该子类在其中加载UIImageView和UIImage。 图像应遵循以下苹果文档(通过捏)进行缩放: https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html
我创建了UIScrollView的子类,该子类可以完成以下操作:
@interface SPMScrollView () {}
@property (nonatomic) float zoomedInScale;
@property (nonatomic) float naturalZoomScale;
@end
@implementation SPMScrollView
-(id)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"%s",__PRETTY_FUNCTION__);
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor blackColor];
super.delegate = self;
self.imageView = [[UIImageView alloc] initWithFrame:self.frame];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:self.imageView];
}
return self;
}
-(void)setupScales {
self.maximumZoomScale = 2.0f;
self.minimumZoomScale = 1.0f;
}
-(void)setImage:(UIImage *)image {
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
self.imageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
[self setupScales];
});
NSLog(@"The image frame is: %@",NSStringFromCGRect(self.imageView.frame));
}
#pragma mark - UIScrollViewDelegate methods
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
在加载时,View Controller只需完成以下操作:
_scrollView.contentSize=self.view.frame.size;
[_scrollView setImage:[UIImage imageNamed:@"WeCanDoIt"]];
我的问题是图像实际上没有正确地首先加载/加载。 甚至以为创建图像时会设置Image视图上的contentMode,图像最初显示如下。
任何人都可以协助为什么可能无法按预期进行吗?
这是完整的屏幕截图:
底部应该有一些空白(这是实际图像),但不在屏幕上。
问题仅在于IOS7。 问题是origin.y设置不正确,中心不正确。
经过一段时间的测试,我发现问题是由于创建了自动滚动插入而造成的:
if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
以上为我解决了问题
如果将UIImageView框架的比例与图像不同,则将contentMode设置为UIViewContentModeScaleAspectFit
,上部和下部应有一些黑色边框。 您可以设置UIViewContentModeScaleToFill
来查看UIImageView是否正确,但是这次图像不按比例拉伸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.