UIScrollView setZoomScale not working?

I am struggling with my UIScrollview so that it increases the base UIImageView. In my opinion, I installed the controller

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

In the viewDidLoad method, I try to set zoomScale to 2 as follows (note that UIImageView and Image are 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 several variations of this, but NSLog always shows zoomScale 1.0.

Any ideas where I twist this?

+5
source share
4 answers

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

The new code is as follows:

- (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;
}
+19
source

, . , . , , 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];

}
+1

, , , , zoomScale, . , , zoomScale , , . , , .

+1

My code should be absolutely crazy, because the scale that I use is completely opposite to what the lessons and others do. For me minScale = 1, that indicates that the image is completely scaled down and matches UIImageViewthat contains it.

Here is 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;

It works as I expect. When I upload the image to UIImageView, it is fully scaled. Then I can zoom in and then I can pan the image.

0
source

All Articles