开发者

UIScrollView setZoomScale not working?

开发者 https://www.devze.com 2022-12-26 10:17 出处:网络
I am struggeling with my UIScrollview to get it to zoom-in the underlying UIImageView. In my view controller I set

I am struggeling with my UIScrollview to get it to zoom-in the underlying UIImageView. In my view controller I set

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

In the viewDidLoad method I try to set the zoomScale to 2 as follows (note the UIImageView and Image is set in 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);
}

I tried a few variations of this, but the NSLog always shows a zoomScale of 1.0.

Any 开发者_如何学Pythonideas where I screw this one up?


I finally got this to work. what caused the problem was the delegate call being at the end. I now moved it up and .... here we go.

New code looks like this:

- (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;
}


Here is another example I made. This one is using an image that is included in the resource folder. Compared to the one you have this one adds the UIImageView to the view as a subview and then changes the zoom to the whole view.

-(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];

}


I know this is quite late as answers go, but the problem is that your code calls zoomScale before it sets the delegate. You are right the other things in there don't require the delegate, but zoomScale does because it has to be able to call back when the zoom is complete. At least that's how I think it works.


My code must be completely crazy because the scale that I use is completely opposite to what tutorials and others are doing. For me, minScale = 1 which indicates that the image is fully zoomed out and fits the UIImageView that contains it.

Here's my code:

    [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;

This works as I expect. When I load the image into the UIImageView, it is fully zoomed out. I can then zoom in and then I can pan the image.

0

精彩评论

暂无评论...
验证码 换一张
取 消