【问题标题】:Setting up a Zooming UIScrollView with a UIImageView使用 UIImageView 设置缩放 UIScrollView
【发布时间】:2014-01-15 14:23:15
【问题描述】:

我正在尝试设置一个 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;
}

在加载视图控制器时,只需完成以下操作:

_scrollView.contentSize=self.view.frame.size;
[_scrollView setImage:[UIImage imageNamed:@"WeCanDoIt"]];

我遇到的问题是图像实际上并没有正确加载/加载。即使在 Image 视图上的 contentMode 是在创建时设置的,但最初的图像显示是这样的。

任何人都可以帮助解释为什么这可能无法按预期工作吗?

这是完整的截图:

底部应该有一些空白(这是实际图像)但不在屏幕上。

【问题讨论】:

    标签: ios ios7 uiscrollview uiimageview uiimage


    【解决方案1】:

    问题仅出在 IOS7 上。问题是没有正确设置 origin.y 并且中心不正确。

    经过一段时间的测试,我发现问题是由于创建了自动滚动插图:

    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    

    以上解决了我的问题

    【讨论】:

      【解决方案2】:

      你将 contentMode 设置为UIViewContentModeScaleAspectFit 如果 UIImageView 框架的比例与图像的比例不同,则上下应该有一些黑色边框。可以设置UIViewContentModeScaleToFill看UIImageView是否正确,但是这次图片不是按比例拉伸的。

      【讨论】:

      • 请看上面的编辑,我已经添加了一个完整的截图。是的,如果图像确实合适,我希望顶部/底部为黑色。但是图像实际上并没有完全显示在屏幕上,图像底部应该有更多的白色并适合屏幕。
      • 截图没有完全显示,但模拟器的结尾是图像结束的地方。
      • 你有没有尝试打印出 UIImageView 的框架?为什么不使用self.bounds(而不是self.frame)来初始化它?
      • 没什么区别,我认为问题是导航栏被隐藏但仍然在那里?以某种方式。
      • 你在推动视图控制器来显示视图吗?如果是这样,也许是 UINavigationbar
      猜你喜欢
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 2012-08-16
      • 2012-11-24
      • 2019-05-23
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多