【问题标题】:UIScrollView setZoomScale not working?UIScrollView setZoomScale 不工作?
【发布时间】:2010-04-13 11:53:24
【问题描述】:

我正在努力使用我的 UIScrollview 来放大底层 UIImageView。在我的视图控制器中,我设置了

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
 return myImageView;
}

在 viewDidLoad 方法中,我尝试将 zoomScale 设置为 2,如下所示(注意 UIImageView 和 Image 是在 Interface Builder 中设置的):

- (void)viewDidLoad {
 [super viewDidLoad];

 myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
 myScrollView.contentOffset = CGPointMake(941.0, 990.0);
 myScrollView.minimumZoomScale = 0.1;
 myScrollView.maximumZoomScale = 10.0;
 myScrollView.zoomScale = 0.7;
 myScrollView.clipsToBounds = YES;
 myScrollView.delegate = self;

 NSLog(@"zoomScale: %.1f, minZoolScale: %.3f", myScrollView.zoomScale, myScrollView.minimumZoomScale);
}

我尝试了一些变体,但 NSLog 始终显示缩放比例为 1.0。

有什么想法让我把这个搞砸了吗?

【问题讨论】:

    标签: objective-c cocoa-touch uikit


    【解决方案1】:

    我终于让它工作了。导致问题的原因是委托调用在最后。我现在把它移了上去......我们开始吧。

    新代码如下所示:

    - (void)viewDidLoad {
     [super viewDidLoad];
    
     myScrollView.delegate = self;
     myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
     myScrollView.contentOffset = CGPointMake(941.0, 990.0);
     myScrollView.minimumZoomScale = 0.1;
     myScrollView.maximumZoomScale = 10.0;
     myScrollView.zoomScale = 0.7;
     myScrollView.clipsToBounds = YES;
    }
    

    【讨论】:

    • 看起来这是由操作系统调用 -viewForZoomingInScrollView 引起的。如果您的委托为 nil,则不会进行此调用,因此禁用视图缩放。
    • 非常感谢!!这是真的。但为什么会发生这种情况?这不能是代表的事。
    【解决方案2】:

    这是我做的另一个例子。这个使用的是资源文件夹中包含的图像。与您拥有的相比,这个将 UIImageView 作为子视图添加到视图中,然后将缩放更改为整个视图。

    -(void)viewDidLoad{
    
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"random.jpg"];
    
    imageView = [[UIImageView alloc] initWithImage:image];
    
    [self.view addSubview:imageView];
    
    [(UIScrollView *) self.view setContentSize:[image size]];
    
    [(UIScrollView *) self.view setMaximumZoomScale:2.0];
    
    [(UIScrollView *) self.view setMinimumZoomScale:0.5];
    
    }
    

    【讨论】:

    • 想不出为什么会造成任何麻烦,但在我绝望中还是尝试了。结果不变
    【解决方案3】:

    我知道这已经很晚了,但问题是您的代码在设置委托之前调用了 zoomScale。你是对的,那里的其他东西不需要委托,但 zoomScale 需要委托,因为它必须能够在缩放完成时回调。至少我认为它是这样工作的。

    【讨论】:

    • 欢迎来到 SO @turboc!这条评论无疑是有帮助的,但为了将来参考,请在相应帖子下方的“cmets”部分添加类似这样的评论(在这种情况下,是已被接受为答案的帖子)。
    【解决方案4】:

    我的代码一定很疯狂,因为我使用的规模与教程和其他人正在做的完全相反。对我来说,minScale = 1 表示图像已完全缩小并适合包含它的UIImageView

    这是我的代码:

        [self.imageView setImage:image];
        // Makes the content size the same size as the imageView size.
        // Since the image size and the scroll view size should be the same, the scroll view shouldn't scroll, only bounce.
        self.scrollView.contentSize = self.imageView.frame.size;
    
        // despite what tutorials say, the scale actually goes from one (image sized to fit screen) to max (image at actual resolution)
        CGRect scrollViewFrame = self.scrollView.frame;
        CGFloat minScale = 1;
        // max is calculated by finding the max ratio factor of the image size to the scroll view size (which will change based on the device)
        CGFloat scaleWidth = image.size.width / scrollViewFrame.size.width;
        CGFloat scaleHeight = image.size.height / scrollViewFrame.size.height;
        self.scrollView.maximumZoomScale = MAX(scaleWidth, scaleHeight);
        self.scrollView.minimumZoomScale = minScale;
        // ensure we are zoomed out fully
        self.scrollView.zoomScale = minScale;
    

    这按我的预期工作。当我将图像加载到UIImageView 时,它会完全缩小。然后我可以放大并平移图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多