DYAdAlertView.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. //
  2. // DYAdAlertView.m
  3. // DYAdAlertViewApp
  4. //
  5. // Created by Daniel Yao on 16/12/16.
  6. // Copyright © 2016年 Daniel Yao. All rights reserved.
  7. //
  8. #define BaseTag 100
  9. #define ScreenBounds [UIScreen mainScreen].bounds
  10. #define ScreenWidth [UIScreen mainScreen].bounds.size.width
  11. #define ScreenHeight [UIScreen mainScreen].bounds.size.height
  12. #define ScrollWidth _scrollView.frame.size.width
  13. #define ScrollHeight _scrollView.frame.size.height
  14. #import "DYAdAlertView.h"
  15. #import "DYAdModel.h"
  16. #import "FLAnimatedImageView.h"
  17. #import "FLAnimatedImage.h"
  18. #import "SimpleAudioPlayer.h"
  19. #import <Lottie/Lottie.h>
  20. @interface DYAdAlertView()<UIScrollViewDelegate>
  21. {
  22. UIPageControl *pageControl;
  23. UIButton *cancelBtn;
  24. NSString *placeHolderImgStr;
  25. }
  26. @property(nonatomic,strong)UIScrollView *scrollView;
  27. @property(nonatomic,assign)NSInteger itemsCount;
  28. @property(nonatomic,strong)NSArray *adDataList;
  29. @property(nonatomic,assign)BOOL hiddenPageControl;
  30. @property(nonatomic, strong)NSTimer *timer;
  31. @property(nonatomic, strong)JXAudioPlayer *audioPlayer;
  32. @property(nonatomic,assign)int currentTime;
  33. @property (nonatomic,weak) UIImageView *animatedImageView;
  34. @property (nonatomic,weak) CABasicAnimation *animaaaa;
  35. @property (nonatomic,weak) LOTAnimationView *animation;
  36. @end
  37. @implementation DYAdAlertView
  38. /*
  39. // Only override drawRect: if you perform custom drawing.
  40. // An empty implementation adversely affects performance during animation.
  41. - (void)drawRect:(CGRect)rect {
  42. // Drawing code
  43. }
  44. */
  45. - (JXAudioPlayer *)audioPlayer {
  46. if (!_audioPlayer) {
  47. _audioPlayer = [[JXAudioPlayer alloc] init];
  48. }
  49. return _audioPlayer;
  50. }
  51. +(DYAdAlertView *)showInView:(UIView *)view theDelegate:(id)delegate theADInfo: (NSArray *)dataList placeHolderImage: (NSString *)placeHolderStr{
  52. if (!dataList) {
  53. return nil;
  54. }
  55. DYAdAlertView *sqAlertView = [[DYAdAlertView alloc] initShowInView:view theDelegate:delegate theADInfo:dataList placeHolderImage:placeHolderStr];
  56. return sqAlertView;
  57. }
  58. -(DYAdAlertView *)showInViewSelf:(UIView *)view theDelegate:(id)delegate theADInfo: (NSArray *)dataList placeHolderImage: (NSString *)placeHolderStr{
  59. DYAdAlertView *sqAlertView = [self initShowInView:view theDelegate:delegate theADInfo:dataList placeHolderImage:placeHolderStr];
  60. return sqAlertView;
  61. }
  62. - (instancetype)initShowInView:(UIView *)view theDelegate:(id)delegate
  63. theADInfo:(NSArray *)dataList
  64. placeHolderImage: (NSString *)placeHolderStr{
  65. self = [super init];
  66. if (self) { //修改的龙
  67. _currentTime=0;
  68. self.userInteractionEnabled=YES;
  69. self.frame = [UIScreen mainScreen].bounds;
  70. //self.frame = CGRectMake(100,(ScreenHeight/2)-ScreenHeight/4, ScreenWidth-200, ScreenHeight/2);
  71. self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
  72. placeHolderImgStr = placeHolderStr;
  73. self.delegate = delegate;
  74. self.hiddenPageControl = NO;
  75. self.adDataList = dataList;
  76. // [[[UIApplication sharedApplication].windows objectAtIndex:0] endEditing:YES];
  77. // [[[UIApplication sharedApplication].windows objectAtIndex:0] addSubview:self];
  78. //
  79. [self showAlertAnimation];
  80. //UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeFromCurrentView:)];
  81. // [self addGestureRecognizer:tapGesture];
  82. }
  83. return self;
  84. }
  85. - (void)showAlertAnimation
  86. {
  87. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  88. animation.fromValue = [NSNumber numberWithFloat:0];
  89. animation.toValue = [NSNumber numberWithFloat:1];
  90. animation.duration = 0.25;
  91. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  92. [self.layer addAnimation:animation forKey:@"opacity"];
  93. DYAdModel *adModel = self.adDataList[0];
  94. if (adModel.audio.length>1) {
  95. [SimpleAudioPlayer stopAllPlayers];
  96. [SimpleAudioPlayer playFile:@"houjiao.mp3"];
  97. }
  98. _currentTime=adModel.duration;
  99. if (adModel.duration>0) {
  100. if (adModel.type == DYAdModelTypeGifttOpenAnimation) {
  101. // _timer = [NSTimer scheduledTimerWithTimeInterval:_currentTime target:self selector:@selector(tapContentImgViewaaa:) userInfo:nil repeats:NO];
  102. // [[NSRunLoop mainRunLoop] addTimer:_timer forMode: NSRunLoopCommonModes];
  103. }else {
  104. // _timer = [NSTimer scheduledTimerWithTimeInterval:adModel.duration target:self selector:@selector(removeSelfFromSuperview) userInfo:nil repeats:NO];
  105. // [[NSRunLoop mainRunLoop] addTimer:_timer forMode: NSRunLoopCommonModes];
  106. }
  107. } else {
  108. }
  109. }
  110. -(void)dealloc{
  111. _audioPlayer.delegate = nil;
  112. }
  113. -(void)removeFromCurrentView:(UIGestureRecognizer *)gesture
  114. {
  115. UIView * subView = (UIView *)[self viewWithTag:99];
  116. UIView * shadowView = self;
  117. if (CGRectContainsPoint(subView.frame, [gesture locationInView:shadowView]))
  118. {}else{
  119. [self removeSelfFromSuperview];
  120. }
  121. }
  122. - (void)removeSelfFromSuperview
  123. {
  124. [UIView animateWithDuration:0.2 animations:^{
  125. self.alpha = 0;
  126. } completion:^(BOOL finished) {
  127. [self removeFromSuperview];
  128. // 停止定时器
  129. [_timer invalidate];
  130. _timer = nil;
  131. }];
  132. }
  133. -(UIScrollView *)scrollView{
  134. if (!_scrollView) {
  135. _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-600/2, ScreenWidth, 600)];
  136. _scrollView.backgroundColor = [UIColor clearColor];
  137. _scrollView.userInteractionEnabled = YES;
  138. _scrollView.contentSize = CGSizeMake(self.frame.size.width*_itemsCount, 410);
  139. _scrollView.delegate = self;
  140. _scrollView.pagingEnabled = YES;
  141. _scrollView.bounces = YES;
  142. _scrollView.showsHorizontalScrollIndicator = NO;
  143. }
  144. return _scrollView;
  145. }
  146. -(void)setAdDataList:(NSArray *)adDataList{
  147. _adDataList = adDataList;
  148. _itemsCount = adDataList.count;
  149. [self creatItemView];
  150. }
  151. -(void)creatItemView{
  152. if (_itemsCount == 0) {
  153. return;
  154. }
  155. if (_itemsCount == 1) {
  156. self.hiddenPageControl = YES;
  157. }
  158. [self addSubview:self.scrollView];
  159. DYAdModel *adModel = [_adDataList objectAtIndex:0];
  160. typedef NS_ENUM(NSUInteger, DYAdModelType) {
  161. DYAdModelTypeLongAnimation = 0, // 龙动画
  162. DYAdModelTypeGiftOpen, // 宝箱打开
  163. DYAdModelTypeGifttOpenAnimation, // 宝箱动画
  164. DYAdModelTypeGiftAfterOpen, // 宝箱打开后
  165. DYAdModelTypePackRedOpen, // 红包打开后
  166. };
  167. _timer=nil;
  168. [_timer invalidate];
  169. switch (adModel.type) {
  170. case DYAdModelTypeLongAnimation:
  171. {
  172. [self setupView1];
  173. }
  174. break;
  175. case DYAdModelTypeGiftOpen:
  176. {
  177. [self setupView2];
  178. }
  179. break;
  180. case DYAdModelTypeGifttOpenAnimation:
  181. {
  182. [self setupView3];
  183. }
  184. break;
  185. case DYAdModelTypeGiftAfterOpen:
  186. {
  187. [self setupView4];
  188. }
  189. case DYAdModelTypePackRedOpen:
  190. {
  191. [self setupView5];
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. -(CGSize)titleBtnWight:(NSString *)titBtnW and:(UILabel *)titBtn {
  199. CGSize titleSize = [titBtnW boundingRectWithSize:CGSizeMake(ScreenWidth-60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titBtn.font} context:nil].size;
  200. return titleSize;
  201. }
  202. /**去购彩*/
  203. - (void)gotoGoucaiBtnDeleGate:(NSNotification *)note{
  204. if (_goBlock) {
  205. _goBlock(note);
  206. }
  207. }
  208. // 龙
  209. - (void)setupView1 {
  210. for ( int i = 0; i < _itemsCount; i++ ) {
  211. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  212. UILabel *titleLong = [[UILabel alloc]init];
  213. titleLong.font = [UIFont systemFontOfSize:25 weight:UIFontWeightMedium];
  214. titleLong.layer.shadowColor = [UIColor clearColor].CGColor;
  215. //titleLong.titleLabel.lineBreakMode = 0;//这句话很重要,不加这句话加上换行符也没用
  216. titleLong.numberOfLines=0;
  217. titleLong.textColor=[UIColor whiteColor];
  218. [self addSubview:titleLong];
  219. if (adModel.content.length>0) {
  220. titleLong.text=[NSString stringWithFormat:@"%@",adModel.content];
  221. }
  222. CGFloat titleH=[self titleBtnWight:adModel.content and:titleLong].height;
  223. titleLong.frame=CGRectMake(30,(JX_SCREEN_HEIGHT>=812?JX_SCREEN_HEIGHT/5:JX_SCREEN_HEIGHT/5-68) , ScreenWidth-60, titleH+20);
  224. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(100+i*ScrollWidth,JX_SCREEN_HEIGHT/6+80, JX_SCREEN_WIDTH-200, JX_SCREEN_HEIGHT/2)];
  225. item.userInteractionEnabled = YES;
  226. item.index = i;
  227. item.tag = BaseTag+item.index;
  228. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"kill-dragon"];
  229. animation.loopAnimation = NO;
  230. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  231. [self addSubview:animation];
  232. __weak __typeof(self)weakSelf = self;
  233. [animation playWithCompletion:^(BOOL animationFinished) {
  234. // Do Something
  235. [animation removeFromSuperview];
  236. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"dragon-idle"];
  237. animation.loopAnimation = YES;
  238. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  239. [weakSelf addSubview:animation];
  240. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:weakSelf action:@selector(tapContentImgView:)];
  241. [animation addGestureRecognizer:singleTap];
  242. UIButton *closeBtn = [[UIButton alloc]init];
  243. [closeBtn setTitle:@"x" forState:UIControlStateNormal];
  244. closeBtn.titleLabel.font=[UIFont systemFontOfSize:20 weight:UIControlStateNormal];
  245. closeBtn.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.5];
  246. closeBtn.layer.cornerRadius=22;
  247. closeBtn.layer.masksToBounds=22;
  248. closeBtn.frame=CGRectMake(JX_SCREEN_WIDTH-60, (JX_SCREEN_HEIGHT>=812?68:34), 44, 44);
  249. [closeBtn addTarget:self action:@selector(tapContentImgViewXX:) forControlEvents:(UIControlEventTouchUpInside)];
  250. [weakSelf addSubview:closeBtn];
  251. [animation playWithCompletion:^(BOOL animationFinished) {
  252. // Do Something
  253. [animation removeFromSuperview];
  254. }];
  255. }];
  256. [_scrollView addSubview:item];
  257. }
  258. //初始化pageControl
  259. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  260. pageControl.numberOfPages = _itemsCount;
  261. pageControl.currentPage = 0;
  262. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  263. pageControl.hidden = self.hiddenPageControl;
  264. [self addSubview:pageControl];
  265. }
  266. //点击文字 龙消失
  267. -(void)tapContentImgViewXX:(UITapGestureRecognizer *)gesture{
  268. [self removeSelfFromSuperview];
  269. }
  270. - (void)setupView2 {
  271. for ( int i = 0; i < _itemsCount; i++ ) {
  272. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  273. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-375/2+i*ScrollWidth,0, 375, 600)];
  274. item.userInteractionEnabled = NO;
  275. item.index = i;
  276. item.tag = BaseTag+item.index;
  277. item.lable.text = adModel.content;
  278. //NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:adModel.imgStr ofType:nil];
  279. // UIImageView *animatedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT)];
  280. // animatedImageView.image=[UIImage imageNamed:@"010004"];
  281. // [self addSubview:animatedImageView];
  282. // animatedImageView.userInteractionEnabled = YES;
  283. // self.animatedImageView=animatedImageView;
  284. //
  285. //
  286. // CABasicAnimation *anima = [CABasicAnimation animation];
  287. // anima.keyPath = @"transform.scale";
  288. // anima.toValue = @0.5;
  289. // anima.repeatCount = MAXFLOAT;
  290. // anima.duration = 0.3;
  291. // anima.autoreverses = YES;
  292. // [animatedImageView.layer addAnimation:anima forKey:nil];
  293. // self.animaaaa=anima;
  294. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-loop-animation"];
  295. animation.loopAnimation = YES;
  296. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  297. [self addSubview:animation];
  298. [animation playWithCompletion:^(BOOL animationFinished) {
  299. // Do Something
  300. }];
  301. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  302. [animation addGestureRecognizer:singleTap];
  303. [_scrollView addSubview:item];
  304. // 正常显示取消按钮
  305. if (adModel.showCancelButton) {
  306. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  307. cancelBtn.frame=CGRectMake(JX_SCREEN_WIDTH-60, JX_SCREEN_WIDTH/3+ (JX_SCREEN_HEIGHT>=812?0:0), 44, 44);
  308. //cancelBtn.frame = CGRectMake(ScreenWidth/2-22, ScreenHeight-70, 44, 44);
  309. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  310. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  311. [self addSubview:cancelBtn];
  312. }
  313. }
  314. //初始化pageControl
  315. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  316. pageControl.numberOfPages = _itemsCount;
  317. pageControl.currentPage = 0;
  318. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  319. pageControl.hidden = self.hiddenPageControl;
  320. [self addSubview:pageControl];
  321. }
  322. - (void)setupView3 {
  323. for ( int i = 0; i < _itemsCount; i++ ) {
  324. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  325. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-375/2+i*ScrollWidth,0, 375, 600)];
  326. item.userInteractionEnabled = NO;
  327. item.index = i;
  328. item.tag = BaseTag+item.index;
  329. item.lable.text = adModel.content;
  330. NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"准点红包2.gif" ofType:nil];
  331. NSData *imageData = [NSData dataWithContentsOfFile:filePath];
  332. if ([adModel.imgStr containsString:@".gif"]) {
  333. // FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
  334. // item.imageView.animatedImage = animatedImage;
  335. item.imageView.userInteractionEnabled = NO;
  336. item.userInteractionEnabled = NO;
  337. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-opened-loop-animation"];
  338. animation.loopAnimation = YES;
  339. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  340. [self addSubview:animation];
  341. [animation playWithCompletion:^(BOOL animationFinished) {
  342. // Do Something
  343. }];
  344. _animation = animation;
  345. }else {
  346. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
  347. item.imageView.animatedImage = animatedImage;
  348. item.imageView.userInteractionEnabled = NO;
  349. item.userInteractionEnabled = NO;
  350. // item.imageView.image = [UIImage imageWithData:imageData];
  351. // item.imageView.userInteractionEnabled = YES;
  352. // item.userInteractionEnabled = YES;
  353. }
  354. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  355. [item.imageView addGestureRecognizer:singleTap];
  356. [_scrollView addSubview:item];
  357. // 正常显示取消按钮
  358. if (adModel.showCancelButton) {
  359. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  360. cancelBtn.frame = CGRectMake(ScreenWidth/2-22, ScreenHeight-70, 44, 44);
  361. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  362. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  363. [self addSubview:cancelBtn];
  364. }
  365. }
  366. //初始化pageControl
  367. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  368. pageControl.numberOfPages = _itemsCount;
  369. pageControl.currentPage = 0;
  370. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  371. pageControl.hidden = self.hiddenPageControl;
  372. [self addSubview:pageControl];
  373. _currentTime=4;
  374. _timer = [NSTimer scheduledTimerWithTimeInterval:_currentTime target:self selector:@selector(tapContentImgViewaaa:) userInfo:nil repeats:NO];
  375. [[NSRunLoop mainRunLoop] addTimer:_timer forMode: NSRunLoopCommonModes];
  376. }
  377. - (void)tapContentImgViewaaa:(NSTimer *)timer{
  378. _currentTime--;
  379. if (_currentTime<=0) {
  380. _timer=nil;
  381. [_timer invalidate];
  382. [self setupView5];
  383. }
  384. }
  385. - (void)setupView4 {
  386. for ( int i = 0; i < _itemsCount; i++ ) {
  387. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  388. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-250/2+i*ScrollWidth,ScrollHeight/2-100, 250, 250)];
  389. item.userInteractionEnabled = YES;
  390. item.index = i;
  391. item.tag = BaseTag+item.index;
  392. // item.lable.text = adModel.content;
  393. NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:adModel.imgStr ofType:nil];
  394. NSData *imageData = [NSData dataWithContentsOfFile:filePath];
  395. if ([adModel.imgStr containsString:@".gif"]) {
  396. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
  397. item.imageView.animatedImage = animatedImage;
  398. item.imageView.userInteractionEnabled = YES;
  399. item.userInteractionEnabled = YES;
  400. }else {
  401. item.imageView.image = [UIImage imageWithData:imageData];
  402. item.imageView.userInteractionEnabled = YES;
  403. item.userInteractionEnabled = YES;
  404. }
  405. [_scrollView addSubview:item];
  406. // containerView
  407. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth/2-250/2+i*ScrollWidth,ScrollHeight/2-100, 250, 250)];
  408. containerView.backgroundColor = [UIColor clearColor];
  409. [_scrollView addSubview:containerView];
  410. // 添加动画边框
  411. NSString *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"光效" ofType:@"gif"];
  412. NSData *imageData1 = [NSData dataWithContentsOfFile:filePath1];
  413. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData1];
  414. FLAnimatedImageView *animatedImageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 10, containerView.frame.size.width, containerView.frame.size.width)];
  415. animatedImageView.contentMode = UIViewContentModeScaleToFill;
  416. animatedImageView.animatedImage = animatedImage;
  417. animatedImageView.backgroundColor = [UIColor clearColor];
  418. animatedImageView.layer.cornerRadius = 22;
  419. animatedImageView.layer.masksToBounds = YES;
  420. [containerView addSubview:animatedImageView];
  421. // 标题获得奖励 往下30
  422. UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, 30)];
  423. label1.font = [UIFont systemFontOfSize:14];
  424. label1.text = adModel.success ? @"获得奖励": @"很遗憾";
  425. label1.textAlignment = NSTextAlignmentCenter;
  426. label1.textColor = [UIColor whiteColor];
  427. [containerView addSubview:label1];
  428. // 恭喜您获得
  429. UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, containerView.frame.size.width, 30)];
  430. label2.font = [UIFont systemFontOfSize:12];
  431. label2.text = adModel.success ? @"恭喜您获得": @"下次加油哦💪💪💪";
  432. label2.textAlignment = NSTextAlignmentCenter;
  433. label2.textColor = [UIColor whiteColor];
  434. [containerView addSubview:label2];
  435. // 奖品图标
  436. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(95, 88, 60, 60)];
  437. if (adModel.success) {
  438. [imageView sd_setImageWithURL:[NSURL URLWithString:adModel.linkUrl] placeholderImage:[UIImage imageNamed:@"gift_default"]];
  439. }else {
  440. imageView.image = [UIImage imageNamed:@"face_cry"];
  441. }
  442. [containerView addSubview:imageView];
  443. // 奖品名称
  444. UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, containerView.frame.size.width, 30)];
  445. label3.font = [UIFont systemFontOfSize:12];
  446. label3.text = adModel.content;
  447. label3.textAlignment = NSTextAlignmentCenter;
  448. label3.textColor = [UIColor whiteColor];
  449. [containerView addSubview:label3];
  450. // 确定按钮
  451. UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 205, containerView.frame.size.width, 30)];
  452. label4.font = [UIFont systemFontOfSize:13];
  453. label4.text = @"确定";
  454. label4.textAlignment = NSTextAlignmentCenter;
  455. label4.textColor = [UIColor whiteColor];
  456. [containerView addSubview:label4];
  457. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  458. [label4 addGestureRecognizer:singleTap];
  459. label4.userInteractionEnabled = YES;
  460. // 正常显示取消按钮
  461. if (adModel.showCancelButton) {
  462. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  463. cancelBtn.frame=CGRectMake(JX_SCREEN_WIDTH-60, JX_SCREEN_WIDTH/3+ (JX_SCREEN_HEIGHT>=812?0:0), 44, 44);
  464. // cancelBtn.frame = CGRectMake(ScreenWidth/2-22, ScreenHeight-80, 44, 44);
  465. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  466. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  467. [self addSubview:cancelBtn];
  468. }
  469. }
  470. //初始化pageControl
  471. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  472. pageControl.numberOfPages = _itemsCount;
  473. pageControl.currentPage = 0;
  474. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  475. pageControl.hidden = self.hiddenPageControl;
  476. [self addSubview:pageControl];
  477. }
  478. - (void)setupView5 {
  479. for ( int i = 0; i < _itemsCount; i++ ) {
  480. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  481. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-375/2+i*ScrollWidth,0, 375, 600)];
  482. item.userInteractionEnabled = NO;
  483. item.index = i;
  484. item.tag = BaseTag+item.index;
  485. // UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  486. // [item.imageView addGestureRecognizer:singleTap];
  487. // [_scrollView addSubview:item];
  488. // 正常显示取消按钮
  489. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  490. cancelBtn.frame = CGRectMake(ScreenWidth-100, JX_SCREEN_HEIGHT/6, 44, 44);
  491. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  492. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  493. if ([adModel.imgStr isEqualToString:@"红包领取失败"]) {
  494. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-opened-loop-animation"];
  495. animation.loopAnimation = YES;
  496. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  497. [self addSubview:animation];
  498. [animation playWithCompletion:^(BOOL animationFinished) {
  499. // Do Something
  500. }];
  501. [self addSubview:cancelBtn];
  502. UILabel *titleLong = [[UILabel alloc]init];
  503. titleLong.textColor=[UIColor whiteColor];
  504. titleLong.userInteractionEnabled=YES;
  505. titleLong.textAlignment=NSTextAlignmentCenter;
  506. titleLong.numberOfLines = 0;
  507. [animation addSubview:titleLong];
  508. if ([adModel.imgStr isEqualToString:@"红包领取失败"]) {
  509. titleLong.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
  510. titleLong.text = [NSString stringWithFormat:@"很遗憾\n红包已被领完"];
  511. }else {
  512. titleLong.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
  513. titleLong.text = [NSString stringWithFormat:@"中奖了\n%@元",adModel.content];
  514. }
  515. [titleLong mas_makeConstraints:^(MASConstraintMaker *make) {
  516. make.centerX.mas_equalTo(animation.mas_centerX);
  517. make.centerY.mas_equalTo(animation.mas_centerY).mas_offset(-5);;
  518. }];
  519. }else {
  520. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-opened-loop-animation"];
  521. animation.loopAnimation = YES;
  522. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  523. [self addSubview:animation];
  524. [animation playWithCompletion:^(BOOL animationFinished) {
  525. // Do Something
  526. }];
  527. UITapGestureRecognizer *singleTapBt = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgViewXX:)];
  528. [animation addGestureRecognizer:singleTapBt];
  529. [self addSubview:cancelBtn];
  530. UILabel *titleLong = [[UILabel alloc]init];
  531. titleLong.textColor=[UIColor whiteColor];
  532. titleLong.userInteractionEnabled=YES;
  533. titleLong.textAlignment=NSTextAlignmentCenter;
  534. titleLong.numberOfLines = 0;
  535. [animation addSubview:titleLong];
  536. if ([adModel.imgStr isEqualToString:@"红包领取失败"]) {
  537. titleLong.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
  538. titleLong.text = [NSString stringWithFormat:@"很遗憾\n红包已被领完"];
  539. }else {
  540. titleLong.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
  541. titleLong.text = [NSString stringWithFormat:@"中奖了\n%@元",adModel.content];
  542. }
  543. [titleLong mas_makeConstraints:^(MASConstraintMaker *make) {
  544. make.centerX.mas_equalTo(animation.mas_centerX);
  545. make.centerY.mas_equalTo(animation.mas_centerY).mas_offset(-5);;
  546. }];
  547. }
  548. }
  549. //初始化pageControl
  550. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  551. pageControl.numberOfPages = _itemsCount;
  552. pageControl.currentPage = 0;
  553. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  554. pageControl.hidden = self.hiddenPageControl;
  555. [self addSubview:pageControl];
  556. }
  557. - (void)tapContentImgViewxx{
  558. [self removeFromSuperview];
  559. }
  560. +(void)dissmissUI{
  561. DYAdAlertView *dissVIew=[DYAdAlertView new];
  562. [dissVIew removeSelfFromSuperview];
  563. }
  564. -(void)dissmissDYAdAlertView{
  565. [self removeFromSuperview];
  566. }
  567. //点击文字 龙消失
  568. -(void)tapContentImgView:(UITapGestureRecognizer *)gesture{
  569. // UIView *imageView = gesture.view;
  570. // NSInteger itemTag = (long)imageView.tag-BaseTag;
  571. if ([self.delegate respondsToSelector:@selector(clickAlertViewAtIndex:)]){
  572. [self.delegate clickAlertViewAtIndex:0];
  573. [self removeSelfFromSuperview];
  574. // self.animaaaa=anima;
  575. //animatedImageView
  576. }
  577. }
  578. -(void)pageValueChange:(UIPageControl*)page{
  579. [UIView animateWithDuration:.35 animations:^{
  580. _scrollView.contentOffset = CGPointMake(page.currentPage*ScreenWidth, 0);
  581. }];
  582. }
  583. #pragma mark - UIScrollViewDelegate
  584. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  585. NSInteger index = scrollView.contentOffset.x/ScreenWidth;
  586. pageControl.currentPage = index;
  587. }
  588. @end
  589. /*********D*********Y**********分**********割**********线************/
  590. //自定义中间主界面
  591. @implementation DYItemView
  592. -(id)initWithFrame:(CGRect)frame{
  593. if (self = [super initWithFrame:frame]) {
  594. self.backgroundColor = [UIColor clearColor];
  595. [self setSubViews];
  596. }
  597. return self;
  598. }
  599. -(void)setSubViews{
  600. self.userInteractionEnabled = YES;
  601. self.layer.masksToBounds = YES;
  602. self.layer.cornerRadius = 5;
  603. self.layer.shadowOpacity = .2;
  604. self.layer.shadowOffset = CGSizeMake(0, 2.5);
  605. self.layer.shadowColor = [UIColor clearColor].CGColor;
  606. [self addSubview:self.imageView];
  607. [self addSubview:self.lable];
  608. }
  609. -(FLAnimatedImageView *)imageView{
  610. if (!_imageView) {
  611. _imageView = [[FLAnimatedImageView alloc]initWithFrame:self.bounds];
  612. _imageView.backgroundColor = [UIColor clearColor];
  613. _imageView.userInteractionEnabled = YES;
  614. _imageView.layer.masksToBounds = YES;
  615. }
  616. return _imageView;
  617. }
  618. -(UILabel *)lable {
  619. if (!_lable) {
  620. _lable = [[UILabel alloc] init];
  621. _lable.font = [UIFont systemFontOfSize:25];
  622. _lable.textColor = [UIColor whiteColor];
  623. _lable.userInteractionEnabled=YES;
  624. //_lable.backgroundColor=[UIColor redColor];
  625. _lable.frame = CGRectMake(0, 0, ScreenWidth, 10);
  626. //[_lable setCenter:CGPointMake(0, 50)];
  627. // _lable.text = @"哈哈哈啊哈哈\n哈哈 哈 哈哈\n 点击屠龙";
  628. _lable.numberOfLines = 0;
  629. _lable.textAlignment = NSTextAlignmentCenter;
  630. // [_lable sizeToFit];
  631. }
  632. return _lable;
  633. }
  634. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  635. [self endEditing:YES];
  636. }
  637. @end