猎豆优选

KXAdAlertView.m 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // KXAdAlertView.m
  3. // advert
  4. //
  5. // Created by 王伟 on 2018/7/23.
  6. // Copyright © 2018年 王伟. All rights reserved.
  7. //
  8. #import "KXAdAlertView.h"
  9. #import "KXAdAlertModel.h"
  10. #import "UIImageView+WebCache.h"
  11. #define BaseTag 100
  12. #define KXScreenBounds [UIScreen mainScreen].bounds
  13. #define KXScreenWidth [UIScreen mainScreen].bounds.size.width
  14. #define KXScreenHeight [UIScreen mainScreen].bounds.size.height
  15. #define KXSCREEN_MUTI (KXScreenWidth/375.0)
  16. #define KXScrollWidth _scrollView.frame.size.width
  17. #define KXScrollHeight _scrollView.frame.size.height
  18. #define KXAlertDate @"KXAlertDate"
  19. @interface KXAdAlertView()<UIScrollViewDelegate>
  20. {
  21. UIPageControl *pageControl;
  22. UIButton *cancelBtn;
  23. NSString *placeHolderImgStr;
  24. }
  25. @property(nonatomic,strong)UIScrollView *scrollView;
  26. @property(nonatomic,assign)NSInteger itemsCount;
  27. @property(nonatomic,strong)NSArray *adDataList;
  28. @property(nonatomic,assign)BOOL hiddenPageControl;
  29. @end
  30. @implementation KXAdAlertView
  31. +(KXAdAlertView *)showInView:(UIView *)view theDelegate:(id)delegate theADInfo: (NSArray *)dataList placeHolderImage: (NSString *)placeHolderStr{
  32. //上次显示时间 年-月-日
  33. NSString *lastDate = [[NSUserDefaults standardUserDefaults] stringForKey:KXAlertDate];
  34. lastDate=nil;
  35. //当前时间 年-月-日
  36. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  37. [formatter setDateFormat:@"yyyy-MM-dd"];
  38. NSDate *datenow = [NSDate date];
  39. NSString *nowDate = [formatter stringFromDate:datenow];
  40. if ([lastDate isEqualToString:nowDate]) { //判断是否显示
  41. return nil;
  42. } else {
  43. [[NSUserDefaults standardUserDefaults] setObject:nowDate forKey:KXAlertDate];
  44. [[NSUserDefaults standardUserDefaults] synchronize];
  45. }
  46. if (!dataList) {
  47. return nil;
  48. }
  49. KXAdAlertView *sqAlertView = [[KXAdAlertView alloc] initShowInView:view theDelegate:delegate theADInfo:dataList placeHolderImage:placeHolderStr];
  50. return sqAlertView;
  51. }
  52. - (instancetype)initShowInView:(UIView *)view theDelegate:(id)delegate
  53. theADInfo:(NSArray *)dataList
  54. placeHolderImage: (NSString *)placeHolderStr{
  55. self = [super init];
  56. if (self) {
  57. self.frame = KXScreenBounds;
  58. self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5];
  59. // self.backgroundColor = [UIColor clearColor];
  60. placeHolderImgStr = placeHolderStr;
  61. self.delegate = delegate;
  62. self.hiddenPageControl = NO;
  63. self.adDataList = dataList;
  64. NSLog(@"self.frame-%@",NSStringFromCGRect(self.frame));
  65. // [[UIApplication sharedApplication].keyWindow addSubview:self];
  66. [view addSubview:self];
  67. [self showAlertAnimation];
  68. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeFromCurrentView:)];
  69. [self addGestureRecognizer:tapGesture];
  70. }
  71. return self;
  72. }
  73. - (void)showAlertAnimation
  74. {
  75. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  76. animation.fromValue = [NSNumber numberWithFloat:0];
  77. animation.toValue = [NSNumber numberWithFloat:1];
  78. animation.duration = 0.25;
  79. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  80. [self.layer addAnimation:animation forKey:@"opacity"];
  81. }
  82. -(void)removeFromCurrentView:(UIGestureRecognizer *)gesture
  83. {
  84. UIView * subView = (UIView *)[self viewWithTag:99];
  85. UIView * shadowView = self;
  86. if (CGRectContainsPoint(subView.frame, [gesture locationInView:shadowView]))
  87. {}else{
  88. [self removeSelfFromSuperview];
  89. }
  90. }
  91. - (void)removeSelfFromSuperview
  92. {
  93. [UIView animateWithDuration:0.2 animations:^{
  94. self.alpha = 0;
  95. } completion:^(BOOL finished) {
  96. [self removeFromSuperview];
  97. }];
  98. }
  99. -(UIScrollView *)scrollView{
  100. if (!_scrollView) {
  101. _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, KXScreenHeight/2-420*KXSCREEN_MUTI/2, KXScreenWidth, 420*KXSCREEN_MUTI)];
  102. _scrollView.backgroundColor = [UIColor clearColor];
  103. _scrollView.userInteractionEnabled = YES;
  104. _scrollView.contentSize = CGSizeMake(self.frame.size.width*_itemsCount, 420*KXSCREEN_MUTI);
  105. _scrollView.delegate = self;
  106. _scrollView.pagingEnabled = YES;
  107. _scrollView.bounces = NO;
  108. _scrollView.showsVerticalScrollIndicator = NO;
  109. _scrollView.showsHorizontalScrollIndicator = NO;
  110. }
  111. return _scrollView;
  112. }
  113. -(void)setAdDataList:(NSArray *)adDataList{
  114. _adDataList = adDataList;
  115. _itemsCount = adDataList.count;
  116. [self creatItemView];
  117. }
  118. -(void)creatItemView{
  119. if (_itemsCount == 0) {
  120. return;
  121. }
  122. if (_itemsCount == 1) {
  123. self.hiddenPageControl = YES;
  124. }
  125. [self addSubview:self.scrollView];
  126. for ( int i = 0; i < _itemsCount; i++ ) {
  127. KXAdAlertModel *adModel = [_adDataList objectAtIndex:i];
  128. KXItemView*item = [[KXItemView alloc]initWithFrame:CGRectMake(KXScreenWidth/2-300*KXSCREEN_MUTI/2+i*KXScrollWidth,0, 300*KXSCREEN_MUTI, 420*KXSCREEN_MUTI)];
  129. item.userInteractionEnabled = YES;
  130. item.index = i;
  131. item.tag = BaseTag+item.index;
  132. [item.imageView sd_setImageWithURL:[NSURL URLWithString:adModel.img_url] placeholderImage:[UIImage imageNamed:@"icon_home_placeholder"]];
  133. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  134. [item addGestureRecognizer:singleTap];
  135. [_scrollView addSubview:item];
  136. }
  137. CGFloat safeBottom = [self getSafeBottom];
  138. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  139. cancelBtn.frame = CGRectMake(KXScreenWidth/2-22, KXScreenHeight-100-safeBottom, 44, 44);
  140. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  141. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  142. [self addSubview:cancelBtn];
  143. //初始化pageControl
  144. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, KXScreenHeight-120-safeBottom, KXScreenWidth, 20)];
  145. pageControl.numberOfPages = _itemsCount;
  146. pageControl.currentPage = 0;
  147. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  148. pageControl.hidden = self.hiddenPageControl;
  149. [self addSubview:pageControl];
  150. }
  151. -(void)tapContentImgView:(UITapGestureRecognizer *)gesture{
  152. UIView *imageView = gesture.view;
  153. NSInteger itemTag = (long)imageView.tag-BaseTag;
  154. if ([self.delegate respondsToSelector:@selector(clickAlertViewAtIndex:)]){
  155. [self.delegate clickAlertViewAtIndex:itemTag];
  156. [self removeSelfFromSuperview];
  157. }
  158. }
  159. -(void)pageValueChange:(UIPageControl*)page{
  160. [UIView animateWithDuration:0.35 animations:^{
  161. self->_scrollView.contentOffset = CGPointMake(page.currentPage*KXScreenWidth, 0);
  162. }];
  163. }
  164. #pragma mark - UIScrollViewDelegate
  165. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  166. NSInteger index = scrollView.contentOffset.x/KXScreenWidth;
  167. pageControl.currentPage = index;
  168. }
  169. - (CGFloat)getSafeBottom{
  170. if (@available(iOS 11.0, *)) {
  171. return [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
  172. } else {
  173. return 0;
  174. }
  175. }
  176. #pragma mark - 获取当前时间
  177. - (NSString *)getNowTimeTimestampWithformatter:(NSString *)fmt{
  178. NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
  179. [formatter setDateStyle:NSDateFormatterMediumStyle];
  180. [formatter setTimeStyle:NSDateFormatterShortStyle];
  181. [formatter setDateFormat:fmt]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  182. //设置时区,这个对于时间的处理有时很重要
  183. NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
  184. [formatter setTimeZone:timeZone];
  185. NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
  186. NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
  187. return timeSp;
  188. }
  189. @end
  190. /*********D*********Y**********分**********割**********线************/
  191. //自定义中间主界面
  192. @implementation KXItemView
  193. -(id)initWithFrame:(CGRect)frame{
  194. if (self = [super initWithFrame:frame]) {
  195. self.backgroundColor = [UIColor clearColor];
  196. [self setSubViews];
  197. }
  198. return self;
  199. }
  200. -(void)setSubViews{
  201. self.userInteractionEnabled = YES;
  202. self.layer.masksToBounds = YES;
  203. self.layer.cornerRadius = 5;
  204. self.layer.shadowOpacity = .2;
  205. self.layer.shadowOffset = CGSizeMake(0, 2.5);
  206. self.layer.shadowColor = [UIColor blackColor].CGColor;
  207. [self addSubview:self.imageView];
  208. }
  209. -(UIImageView *)imageView{
  210. if (!_imageView) {
  211. _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
  212. _imageView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0];
  213. _imageView.userInteractionEnabled = YES;
  214. _imageView.layer.masksToBounds = YES;
  215. }
  216. return _imageView;
  217. }
  218. @end