Ios Zoomscale UIScrollView不工作
我使用Ios Zoomscale UIScrollView不工作,ios,uiscrollview,Ios,Uiscrollview,我使用UICollectionViewscroll direction holizontal,paginable=true创建PhotoViewer。我创建UICollectionViewCell包含UIScrollView以缩放UIImage 但是第一个创建UICollectionViewCellzoomScale工作,UICollectionCell重用zoomScale不工作 我的代码: @interface ImageCell : UICollectionViewCell @prope
UICollectionView
scroll direction holizontal,paginable=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;
}
@接口ImageCell:UICollectionViewCell
@属性(非原子,强)UIImageView*imgView;
@属性(非原子,强)IBUIScrollView*scrollView;
@属性(非原子,强)NSString*stringURL;
@属性(非原子,强)UIImage*img;
-(无效)调整大小;
@结束
@接口ImageCell()
@结束
@实现ImageCell
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
-(无效)调整大小{
SDWebImageManager*manager=[SDWebImageManager共享管理器];
[manager downloadWithURL:[NSURL URLWithString:self.stringURL]
选项:0
进度:^(NSInteger接收大小,NSInteger预期大小)
{
//进程跟踪码
}
已完成:^(UIImage*图像,N错误*错误,SDImageCacheType cacheType,布尔已完成){
如果(图像){
if(self.imgView==nil){
self.imgView=[[UIImageView alloc]initWithFrame:self.frame];
[self.scrollView addSubview:self.imgView];
}
NSLog(@“%@”,NSStringFromCGSize(image.size));
self.img=图像;
self.imgView.frame=CGRectMake(0,0,image.size.width,image.size.height);
self.imgView.image=图像;
self.scrollView.contentSize=image.size;
CGRect scrollViewFrame=self.scrollView.frame;
CGFloat scaleWidth=scrollViewFrame.size.width/image.size.width;
CGFloat SCALEHHEIGH=scrollViewFrame.size.height/image.size.height;
CGFloat minScale=MIN(标度宽度、标度高度);
self.scrollView.minimumZoomScale=minScale;
self.scrollView.maximumZoomScale=1;
[self.scrollView setZoomScale:minScale];
[self-centerScrollViewContents];
}
}];
}
-(UIView*)视图用于缩放CrollView:(UICrollView*)滚动视图
{
返回self.imgView;
}
[self.scrollView setZoomScale:minScale]代码>创建单元时工作,单元重用时不工作
因此,您还应该实现方法ScrollViewDiEndZooming:withView:atScale:
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: