ios - Zoomscale UIScrollView 不工作

标签 ios uiscrollview

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

最佳答案

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

关于ios - Zoomscale UIScrollView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117027/

相关文章:

ios - 设置 UILongPressGesture 的最大持续时间?

iphone - 写入钥匙串(keychain)失败

ios - 使用 null 值调用 CoreBlueTooth 委托(delegate) didWriteValueForCharacteristic

iOS 如何让 MapKit 显示自定义室内地图?

xcode - iOS内存调试

ios - 嵌套的 UIScrollView 在包含的页面上启用了 pagingEnabled 在页面更改后不会收到第一次底部滑动

ios - 无法设置 TableView 单元格的动态高度?

ios - 启用语音时如何从自定义 View 上的点击位置获取 CGPoint?

iphone - ScrollView 不起作用 IOS 7

ios - UITabBarController 中的奇怪界面错误 UIScrollView。别人能复制吗?