缩放 UIScrollView 不起作用

Posted

技术标签:

【中文标题】缩放 UIScrollView 不起作用【英文标题】:Zoomscale UIScrollView not working 【发布时间】:2014-06-09 09:09:47 【问题描述】:

我使用UICollectionView 水平滚动方向创建 PhotoViewer,pagingEnable = true。我创建 UICollectionViewCell 包含 UIScrollView 以缩放 UIImage。 但是第一个创建 UICollectionViewCell zoomScale 工作,UICollectionCell 重用zoomScale 不工作。

我的代码:

@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]; 在创建单元格时工作,当单元格重用不起作用时

【问题讨论】:

【参考方案1】:

此行来自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:

所以你还应该实现方法 scrollViewDidEndZooming:withView:atScale:

【讨论】:

以上是关于缩放 UIScrollView 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

ios - UIScrollView中的iOS编程缩放不起作用

iOS7 自动布局与 Tabbar 结合 UIScrollView 和可缩放 UIImageView 不起作用

为啥这个 UIScrollView 自动布局不起作用?

UIScrollView -setContentInset 不起作用

UIScrollView 不起作用

UIScrollView 水平滚动不起作用