ifish/Ifish/views/Photo/LYPhotoShowView.m

268 lines
7.0 KiB
Objective-C
Executable File

//
// LYPhotoShowView.m
// Droplets
//
// Created by 米明 on 15/12/29.
// Copyright © 2015年 米明. All rights reserved.
//
#import "LYPhotoShowView.h"
#import "LYPhoto.h"
#import "LYHelper.h"
#import "UIImageView+WebCache.h"
#define kPlace ([UIScreen mainScreen].bounds.size.width - 4 * kSizePhoto)/5
#define SCREENSIZE [UIScreen mainScreen].bounds.size
#define kSizePhoto self.frame.size.height
#define kFirstSpace SCREENSIZE.width
#define LABEL_TITLE_FONT [UIFont systemFontOfSize:12.f]
@interface LYPhotoShowView ()
{
CGRect _frame;
}
@property(nonatomic,strong)NSMutableArray * imgViews;
@property(nonatomic,strong)NSMutableArray * imageNames;
@property(nonatomic,strong)NSArray *linewitdhArr;
@end
@implementation LYPhotoShowView
- (instancetype)init{
self = [super init];
if(self){
[self setUp];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self setUp];
}
return self;
}
- (UIScrollView *)scrollView{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(kFirstSpace, 0, self.frame.size.width, self.frame.size.height*3/2 + 3)];
_scrollView.alpha = 0;
_scrollView.contentOffset = CGPointMake(0, 0);
_scrollView.contentSize = CGSizeMake(self.frame.size.height*_images.count + kPlace +
kSizePhoto +(_images.count-1)*kPlace, self.frame.size.height);
//隐藏滚动视图的水平指示条
_scrollView.showsHorizontalScrollIndicator=NO;
_scrollView.backgroundColor = SECTHEADER_VIEWCOLOUR;
//隐藏滚动视图的垂直指示条
_scrollView.showsVerticalScrollIndicator=NO;
[self addSubview:_scrollView];
}
return _scrollView;
}
- (void)setUp{
_imgViews = [NSMutableArray array];
_imageNames = [NSMutableArray array];
}
- (void)clear{
// for (UIView * v in _imgViews) {
// [v removeFromSuperview];
// }
//
// for (UILabel *l in _imageNames) {
//
// [l removeFromSuperview];
//
// }
[_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[_scrollView removeFromSuperview];
_scrollView = nil;
[_imgViews removeAllObjects];
[_imageNames removeAllObjects];
if (!_images||_images.count<=0) {
self.frame=CGRectZero;
}
}
- (void)setImages:(NSArray *)images nameLabels:(NSArray *)nameLabels {
_images = images;
_nameLabels = nameLabels;
_linewitdhArr=[self getlabelWidthsWith:nameLabels];;
[self creatImageViews];
[self setLineView];
}
- (void)creatImageViews{
[self clear];
__weak typeof(self) wself = self;
[_images enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//imgView
CGRect frame = CGRectMake( kPlace + idx*(kPlace + kSizePhoto) , 0, kSizePhoto, kSizePhoto );
LYPhoto * photo = [[LYPhoto alloc]initWithFrame:frame];
[wself.scrollView addSubview:photo];
//nameLabel
CGRect nameFrame=CGRectMake( idx * (kPlace + kSizePhoto) + (kPlace +kSizePhoto) - kSizePhoto , kSizePhoto, kSizePhoto, kSizePhoto/2 );
UILabel * nameLabel =[[UILabel alloc] initWithFrame:nameFrame];
nameLabel.textColor=SECTIONVIEW_LABELCOLER;
nameLabel.text=_nameLabels[idx];
nameLabel.textAlignment=NSTextAlignmentCenter;
nameLabel.font=[UIFont systemFontOfSize:12];
[wself.scrollView addSubview:nameLabel];
[wself.imageNames addObject:nameLabel];
photo.tag = idx;
photo.backgroundColor = SECTHEADER_VIEWCOLOUR;
[wself.imgViews addObject:photo];
[photo addTarget:self action:@selector(photoAction:)];
if ([obj isKindOfClass:[NSString class]]) {//地址
NSString * imgUrl = (NSString *)obj;
if ([LYHelper isUrl:imgUrl]) {//是图片链接
[photo sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"icon1"]];
//自行下载图片
}else {//名字
photo.image = [UIImage imageNamed:imgUrl];
}
}else if ([obj isKindOfClass:[UIImage class]]){//image对象
UIImage * image = (UIImage *)obj;
photo.image = image;
}
}];
}
-(void)setPhotoActionCallback:(LYPhotoActionBlock)photoActionCallback{
_photoActionCallback = nil;
_photoActionCallback = [photoActionCallback copy];
}
- (void)photoAction:(id)sender{
if (self.photoActionCallback) {
self.photoActionCallback(((LYPhoto*)sender).tag);
}
NSInteger index= ((LYPhoto*)sender).tag ;
[self showLineWidthLabelWith:[_linewitdhArr[index] floatValue] andIndex:index];
}
- (void)show{
//距离
CGRect frame = self.scrollView.frame;
frame.origin.x = 0;
__weak typeof(self) wself = self;
[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
wself.scrollView.frame = frame;
wself.scrollView.alpha = 1;
} completion:^(BOOL finished) {
}];
}
- (void)dismiss{
CGRect frame = self.scrollView.frame;
frame.origin.x = kFirstSpace;
__weak typeof(self) wself = self;
[UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
wself.scrollView.frame = frame;
wself.scrollView.alpha = 0;
} completion:^(BOOL finished) {
[wself removeFromSuperview];
}];
}
-(NSArray*)getlabelWidthsWith:(NSArray*)nameLabels{
NSMutableArray *labelWidthsArr=[@[] mutableCopy];
for (NSString *name in nameLabels) {
CGSize size = [name boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LABEL_TITLE_FONT} context:nil].size;
NSNumber *width = [NSNumber numberWithFloat:size.width + 20.f];
[labelWidthsArr addObject:width];
}
return labelWidthsArr;
}
-(void)showLineWidthLabelWith:(CGFloat) width andIndex:(NSInteger) Index{
CGRect frame = CGRectMake( Index * (kPlace + kSizePhoto) + (kPlace +kSizePhoto) - kSizePhoto ,kSizePhoto*3/2,kSizePhoto,3.0f);
[UIView animateWithDuration:.2f animations:^{
_line.frame=frame;
}];
}
//初始line
-(void)setLineView{
CGRect frame = CGRectMake( kPlace ,kSizePhoto*3/2,kSizePhoto,3.0f);
_line = [[UIView alloc] initWithFrame:frame];
_line.backgroundColor = [UIColor orangeColor];
[_scrollView addSubview:_line];
}
@end