gpt4 book ai didi

ios - Zoomscale UIScrollView 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:05 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com