// // KXAdAlertView.m // advert // // Created by 王伟 on 2018/7/23. // Copyright © 2018年 王伟. All rights reserved. // #import "KXAdAlertView.h" #import "KXAdAlertModel.h" #import "UIImageView+WebCache.h" #define BaseTag 100 #define KXScreenBounds [UIScreen mainScreen].bounds #define KXScreenWidth [UIScreen mainScreen].bounds.size.width #define KXScreenHeight [UIScreen mainScreen].bounds.size.height #define KXSCREEN_MUTI (KXScreenWidth/375.0) #define KXScrollWidth _scrollView.frame.size.width #define KXScrollHeight _scrollView.frame.size.height #define KXAlertDate @"KXAlertDate" @interface KXAdAlertView() { UIPageControl *pageControl; UIButton *cancelBtn; NSString *placeHolderImgStr; } @property(nonatomic,strong)UIScrollView *scrollView; @property(nonatomic,assign)NSInteger itemsCount; @property(nonatomic,strong)NSArray *adDataList; @property(nonatomic,assign)BOOL hiddenPageControl; @end @implementation KXAdAlertView +(KXAdAlertView *)showInView:(UIView *)view theDelegate:(id)delegate theADInfo: (NSArray *)dataList placeHolderImage: (NSString *)placeHolderStr{ //上次显示时间 年-月-日 NSString *lastDate = [[NSUserDefaults standardUserDefaults] stringForKey:KXAlertDate]; lastDate=nil; //当前时间 年-月-日 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate *datenow = [NSDate date]; NSString *nowDate = [formatter stringFromDate:datenow]; if ([lastDate isEqualToString:nowDate]) { //判断是否显示 return nil; } else { [[NSUserDefaults standardUserDefaults] setObject:nowDate forKey:KXAlertDate]; [[NSUserDefaults standardUserDefaults] synchronize]; } if (!dataList) { return nil; } KXAdAlertView *sqAlertView = [[KXAdAlertView alloc] initShowInView:view theDelegate:delegate theADInfo:dataList placeHolderImage:placeHolderStr]; return sqAlertView; } - (instancetype)initShowInView:(UIView *)view theDelegate:(id)delegate theADInfo:(NSArray *)dataList placeHolderImage: (NSString *)placeHolderStr{ self = [super init]; if (self) { self.frame = KXScreenBounds; self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5]; // self.backgroundColor = [UIColor clearColor]; placeHolderImgStr = placeHolderStr; self.delegate = delegate; self.hiddenPageControl = NO; self.adDataList = dataList; NSLog(@"self.frame-%@",NSStringFromCGRect(self.frame)); // [[UIApplication sharedApplication].keyWindow addSubview:self]; [view addSubview:self]; [self showAlertAnimation]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeFromCurrentView:)]; [self addGestureRecognizer:tapGesture]; } return self; } - (void)showAlertAnimation { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.fromValue = [NSNumber numberWithFloat:0]; animation.toValue = [NSNumber numberWithFloat:1]; animation.duration = 0.25; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; [self.layer addAnimation:animation forKey:@"opacity"]; } -(void)removeFromCurrentView:(UIGestureRecognizer *)gesture { UIView * subView = (UIView *)[self viewWithTag:99]; UIView * shadowView = self; if (CGRectContainsPoint(subView.frame, [gesture locationInView:shadowView])) {}else{ [self removeSelfFromSuperview]; } } - (void)removeSelfFromSuperview { [UIView animateWithDuration:0.2 animations:^{ self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } -(UIScrollView *)scrollView{ if (!_scrollView) { _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, KXScreenHeight/2-420*KXSCREEN_MUTI/2, KXScreenWidth, 420*KXSCREEN_MUTI)]; _scrollView.backgroundColor = [UIColor clearColor]; _scrollView.userInteractionEnabled = YES; _scrollView.contentSize = CGSizeMake(self.frame.size.width*_itemsCount, 420*KXSCREEN_MUTI); _scrollView.delegate = self; _scrollView.pagingEnabled = YES; _scrollView.bounces = NO; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; } return _scrollView; } -(void)setAdDataList:(NSArray *)adDataList{ _adDataList = adDataList; _itemsCount = adDataList.count; [self creatItemView]; } -(void)creatItemView{ if (_itemsCount == 0) { return; } if (_itemsCount == 1) { self.hiddenPageControl = YES; } [self addSubview:self.scrollView]; for ( int i = 0; i < _itemsCount; i++ ) { KXAdAlertModel *adModel = [_adDataList objectAtIndex:i]; KXItemView*item = [[KXItemView alloc]initWithFrame:CGRectMake(KXScreenWidth/2-300*KXSCREEN_MUTI/2+i*KXScrollWidth,0, 300*KXSCREEN_MUTI, 420*KXSCREEN_MUTI)]; item.userInteractionEnabled = YES; item.index = i; item.tag = BaseTag+item.index; [item.imageView sd_setImageWithURL:[NSURL URLWithString:adModel.img_url] placeholderImage:[UIImage imageNamed:@"icon_home_placeholder"]]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)]; [item addGestureRecognizer:singleTap]; [_scrollView addSubview:item]; } CGFloat safeBottom = [self getSafeBottom]; cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; cancelBtn.frame = CGRectMake(KXScreenWidth/2-22, KXScreenHeight-100-safeBottom, 44, 44); [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal]; [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:cancelBtn]; //初始化pageControl pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, KXScreenHeight-120-safeBottom, KXScreenWidth, 20)]; pageControl.numberOfPages = _itemsCount; pageControl.currentPage = 0; [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged]; pageControl.hidden = self.hiddenPageControl; [self addSubview:pageControl]; } -(void)tapContentImgView:(UITapGestureRecognizer *)gesture{ UIView *imageView = gesture.view; NSInteger itemTag = (long)imageView.tag-BaseTag; if ([self.delegate respondsToSelector:@selector(clickAlertViewAtIndex:)]){ [self.delegate clickAlertViewAtIndex:itemTag]; [self removeSelfFromSuperview]; } } -(void)pageValueChange:(UIPageControl*)page{ [UIView animateWithDuration:0.35 animations:^{ self->_scrollView.contentOffset = CGPointMake(page.currentPage*KXScreenWidth, 0); }]; } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSInteger index = scrollView.contentOffset.x/KXScreenWidth; pageControl.currentPage = index; } - (CGFloat)getSafeBottom{ if (@available(iOS 11.0, *)) { return [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom; } else { return 0; } } #pragma mark - 获取当前时间 - (NSString *)getNowTimeTimestampWithformatter:(NSString *)fmt{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:fmt]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"]; [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式 NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]]; return timeSp; } @end /*********D*********Y**********分**********割**********线************/ //自定义中间主界面 @implementation KXItemView -(id)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; [self setSubViews]; } return self; } -(void)setSubViews{ self.userInteractionEnabled = YES; self.layer.masksToBounds = YES; self.layer.cornerRadius = 5; self.layer.shadowOpacity = .2; self.layer.shadowOffset = CGSizeMake(0, 2.5); self.layer.shadowColor = [UIColor blackColor].CGColor; [self addSubview:self.imageView]; } -(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]initWithFrame:self.bounds]; _imageView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0]; _imageView.userInteractionEnabled = YES; _imageView.layer.masksToBounds = YES; } return _imageView; } @end