图片浏览器(高仿微信)

自己根据微信朋友圈的图片浏览器特点,重新编写了一个高仿微信的图片浏览器源代码控件。

图片浏览器(高仿微信)_第1张图片
F964E8EB-10B8-4DE4-B405-A76A7D39CA61.png

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 

@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)];
}

你可能感兴趣的:(图片浏览器(高仿微信))