我使用UICollectionView
滚动方向holizontal,pagingEnable = true创建PhotoViewer。我创建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];
在创建单元格时工作,当单元格重用不起作用时
答案 0 :(得分: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: