自己根据微信朋友圈的图片浏览器特点,重新编写了一个高仿微信的图片浏览器源代码控件。
1,结构:
TBPictureBrowserView,继承UIView,主控件
TBImageView,重写UIImageView,子控件
2,实现思路
1,TBPictureBrowserView中添加UISrollView,并将图片添加至UISrollView
for (NSInteger i = 0; i < self.totalPage; i++) {
TBImageView *imageView = [[TBImageView alloc] init];
imageView.frame = CGRectMake(i * self.width, 0, self.width, self.height);
[self.contentSC addSubview:imageView];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.delegate = self;
// 设置占位符
if ([self.delegate respondsToSelector:@selector(placeHolderImage:index:)]) {
imageView.image = [self.delegate placeHolderImage:self index:i];
}
}
2,代理提供2中方法,分别用于小图,高清图
@protocol TBPictureBrowserViewDelegate <NSObject>
@required
/**
* 小图,站位图
*/
- (UIImage *)placeHolderImage:(TBPictureBrowserView *)browser index:(NSInteger)index;
@optional
/**
* 高清图的url
*/
- (NSString *)hightQualityImageUrl:(TBPictureBrowserView *)browser index:(NSInteger)index;
@end
3,每一个TBImageView子控件添加UIPinchGestureRecognizer手势,用于拉伸
- (void)pinchGesture:(UIPinchGestureRecognizer *)gesture {
CGFloat temp = self.tbScale + (gesture.scale - 1);
[self setImageViewScale:temp];
gesture.scale = 1.0;
}
4, 根据缩放比例展示图片
- (void)setImageViewScale:(CGFloat)scale{
self.tbScale = scale;
[self setupScrollView];
self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * scale, self.scrollView.bounds.size.height * scale);
UIImageView *tempImageView = self.scrollView.subviews.firstObject;
tempImageView.size = self.scrollView.contentSize;
tempImageView.center = CGPointMake(self.scrollView.contentSize.width * 0.5, self.scrollView.contentSize.height * 0.5);
CGFloat Y = 0.5 * (self.scrollView.contentSize.height - self.scrollView.height);
CGFloat X = 0.5 * (self.scrollView.contentSize.width - self.scrollView.width);
[self.scrollView setContentOffset:CGPointMake(X, Y)];
}