Nhu Nguyen
Nhu Nguyen

Reputation: 874

Zoomscale UIScrollView not working

I create PhotoViewer by using UICollectionView scroll direction holizontal, pagingEnable = true. I create UICollectionViewCell contains UIScrollView to zooming UIImage.
But the first create UICollectionViewCell zoomScale working, UICollectionCell reuse zoomScale not working.

My code:

@interface ImageCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) NSString *stringURL;
@property (nonatomic, strong) UIImage *img;

- (void)resize;

@end

@interface ImageCell()<UIScrollViewDelegate>

@end

@implementation ImageCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)resize {
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:[NSURL URLWithString:self.stringURL]
                     options:0
                    progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         // progression tracking code

     }
     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
         if (image) {

             if(self.imgView == nil) {
                 self.imgView = [[UIImageView alloc] initWithFrame:self.frame];
                 [self.scrollView addSubview:self.imgView];
             }

             NSLog(@"%@", NSStringFromCGSize(image.size));
             self.img = image;

             self.imgView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
             self.imgView.image = image;

             self.scrollView.contentSize = image.size;
             CGRect scrollViewFrame = self.scrollView.frame;
             CGFloat scaleWidth = scrollViewFrame.size.width / image.size.width;
             CGFloat scaleHeight = scrollViewFrame.size.height / image.size.height;
             CGFloat minScale = MIN(scaleWidth, scaleHeight);

             self.scrollView.minimumZoomScale = minScale;
             self.scrollView.maximumZoomScale = 1;
             [self.scrollView setZoomScale:minScale];

             [self centerScrollViewContents];
         }
     }];
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imgView;
}

[self.scrollView setZoomScale:minScale]; working when cell is create, when cell reuse not working

Upvotes: 3

Views: 1725

Answers (1)

Bhoomi Jagani
Bhoomi Jagani

Reputation: 2423

This line from the UIScrollView class reference

 The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:

So you should also implement method scrollViewDidEndZooming:withView:atScale:

Upvotes: 1

Related Questions