#import <UIKit/UIKit.h>
@interface PickerView : UIView<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,strong) NSArray *dataAr;
@property (nonatomic,assign) NSInteger currentPageNum;
//创建实例
+ (PickerView *)initWithFrame:(CGRect)frame WithDataSurce:(NSArray *)dataAy SelectNum:(NSInteger)selectNum;
//展示界面
- (void)show;
//消失界面
- (void)dismiss;
@end
#import "PickerView.h"
@implementation PickerView
+ (PickerView *)initWithFrame:(CGRect)frame WithDataSurce:(NSArray *)dataAy SelectNum:(NSInteger)selectNum {
PickerView *pickerView = [[self alloc]initWithFrame:frame];
pickerView.dataAr = dataAy;
pickerView.currentPageNum = selectNum;
//根据参数初始化
[pickerView initsubViews];
return pickerView;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
//滚动视图
self.scrollView = [[UIScrollView alloc]init];
[self.scrollView setBackgroundColor:[UIColor clearColor]];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
[self addSubview:self.scrollView];
//分页控件
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((self.frame.size.width-100)/2.0,self.frame.size.height-22,100,18)];
[self.pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
[self.pageControl setPageIndicatorTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.6]];
[self addSubview:self.pageControl];
}
return self;
}
- (void)initsubViews {
self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
for (int i = 0; i < self.dataAr.count; i++) {
NSDictionary *dic = [self.dataAr objectAtIndex:i];
UIImage *image = [dic objectForKey:@"Image"];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height)];
//设置imgView的停靠模式,图片按原比例显示,而非拉伸填充
imgView.contentMode = UIViewContentModeScaleAspectFit;
UITapGestureRecognizer *dissTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToDismiss)];
[imgView addGestureRecognizer:dissTap];
imgView.userInteractionEnabled = YES;
[imgView setImage:image];
[self.scrollView addSubview:imgView];
}
self.scrollView.scrollEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.frame.size.width*self.dataAr.count, self.frame.size.height);
self.pageControl.numberOfPages = [self.dataAr count] > 1 ? [self.dataAr count] : 0 ;
self.pageControl.currentPage = self.currentPageNum;
//默认显示点击的图片
[self.scrollView scrollRectToVisible:CGRectMake(self.frame.size.width*self.currentPageNum,0,self.frame.size.width,self.frame.size.height) animated:NO];
}
#pragma mark - Animated Mthod
- (void)animatedIn {
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.alpha = 0;
[UIView animateWithDuration:.35 animations:^{
self.alpha = 1;
self.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)animatedOut {
[UIView animateWithDuration:.35 animations:^{
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[self removeFromSuperview];
}
}];
}
#pragma mark - show or hide self
- (void)show {
UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
[keywindow addSubview:self];
[self animatedIn];
}
- (void)dismiss {
[self animatedOut];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self animatedOut];
}
//点击大图,移除
- (void)tapToDismiss
{
[self animatedOut];
}
#pragma mark - scrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGPoint point = scrollView.contentOffset;
self.pageControl.currentPage = (NSInteger)point.x/self.scrollView.frame.size.width;
}
@end