DYAdAlertView.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-loop-animation"];
  280. animation.loopAnimation = YES;
  281. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  282. [self addSubview:animation];
  283. [animation playWithCompletion:^(BOOL animationFinished) {
  284. // Do Something
  285. }];
  286. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  287. [animation addGestureRecognizer:singleTap];
  288. [_scrollView addSubview:item];
  289. // 正常显示取消按钮
  290. if (adModel.showCancelButton) {
  291. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  292. cancelBtn.frame=CGRectMake(JX_SCREEN_WIDTH-60, JX_SCREEN_WIDTH/3+ (JX_SCREEN_HEIGHT>=812?0:0), 44, 44);
  293. //cancelBtn.frame = CGRectMake(ScreenWidth/2-22, ScreenHeight-70, 44, 44);
  294. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  295. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  296. [self addSubview:cancelBtn];
  297. }
  298. }
  299. //初始化pageControl
  300. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  301. pageControl.numberOfPages = _itemsCount;
  302. pageControl.currentPage = 0;
  303. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  304. pageControl.hidden = self.hiddenPageControl;
  305. [self addSubview:pageControl];
  306. }
  307. - (void)setupView3 {
  308. for ( int i = 0; i < _itemsCount; i++ ) {
  309. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  310. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-375/2+i*ScrollWidth,0, 375, 600)];
  311. item.userInteractionEnabled = NO;
  312. item.index = i;
  313. item.tag = BaseTag+item.index;
  314. item.lable.text = adModel.content;
  315. NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"准点红包2.gif" ofType:nil];
  316. NSData *imageData = [NSData dataWithContentsOfFile:filePath];
  317. if ([adModel.imgStr containsString:@".gif"]) {
  318. // FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
  319. // item.imageView.animatedImage = animatedImage;
  320. item.imageView.userInteractionEnabled = NO;
  321. item.userInteractionEnabled = NO;
  322. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-opened-loop-animation"];
  323. animation.loopAnimation = YES;
  324. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  325. [self addSubview:animation];
  326. [animation playWithCompletion:^(BOOL animationFinished) {
  327. // Do Something
  328. }];
  329. _animation = animation;
  330. }else {
  331. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
  332. item.imageView.animatedImage = animatedImage;
  333. item.imageView.userInteractionEnabled = NO;
  334. item.userInteractionEnabled = NO;
  335. // item.imageView.image = [UIImage imageWithData:imageData];
  336. // item.imageView.userInteractionEnabled = YES;
  337. // item.userInteractionEnabled = YES;
  338. }
  339. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  340. [self addGestureRecognizer:singleTap];
  341. [_scrollView addSubview:item];
  342. // 正常显示取消按钮
  343. if (adModel.showCancelButton) {
  344. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  345. cancelBtn.frame = CGRectMake(ScreenWidth/2-22, ScreenHeight-70, 44, 44);
  346. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  347. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  348. [self addSubview:cancelBtn];
  349. }
  350. }
  351. //初始化pageControl
  352. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  353. pageControl.numberOfPages = _itemsCount;
  354. pageControl.currentPage = 0;
  355. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  356. pageControl.hidden = self.hiddenPageControl;
  357. [self addSubview:pageControl];
  358. _currentTime=4;
  359. // _timer = [NSTimer scheduledTimerWithTimeInterval:_currentTime target:self selector:@selector(tapContentImgViewaaa:) userInfo:nil repeats:NO];
  360. // [[NSRunLoop mainRunLoop] addTimer:_timer forMode: NSRunLoopCommonModes];
  361. }
  362. - (void)tapContentImgViewaaa:(NSTimer *)timer{
  363. _currentTime--;
  364. if (_currentTime<=0) {
  365. _timer=nil;
  366. [_timer invalidate];
  367. [self setupView5];
  368. }
  369. }
  370. - (void)setupView4 {
  371. for ( int i = 0; i < _itemsCount; i++ ) {
  372. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  373. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-250/2+i*ScrollWidth,ScrollHeight/2-100, 250, 250)];
  374. item.userInteractionEnabled = YES;
  375. item.index = i;
  376. item.tag = BaseTag+item.index;
  377. // item.lable.text = adModel.content;
  378. NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:adModel.imgStr ofType:nil];
  379. NSData *imageData = [NSData dataWithContentsOfFile:filePath];
  380. if ([adModel.imgStr containsString:@".gif"]) {
  381. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
  382. item.imageView.animatedImage = animatedImage;
  383. item.imageView.userInteractionEnabled = YES;
  384. item.userInteractionEnabled = YES;
  385. }else {
  386. item.imageView.image = [UIImage imageWithData:imageData];
  387. item.imageView.userInteractionEnabled = YES;
  388. item.userInteractionEnabled = YES;
  389. }
  390. [_scrollView addSubview:item];
  391. // containerView
  392. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth/2-250/2+i*ScrollWidth,ScrollHeight/2-100, 250, 250)];
  393. containerView.backgroundColor = [UIColor clearColor];
  394. [_scrollView addSubview:containerView];
  395. // 添加动画边框
  396. NSString *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"光效" ofType:@"gif"];
  397. NSData *imageData1 = [NSData dataWithContentsOfFile:filePath1];
  398. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData1];
  399. FLAnimatedImageView *animatedImageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 10, containerView.frame.size.width, containerView.frame.size.width)];
  400. animatedImageView.contentMode = UIViewContentModeScaleToFill;
  401. animatedImageView.animatedImage = animatedImage;
  402. animatedImageView.backgroundColor = [UIColor clearColor];
  403. animatedImageView.layer.cornerRadius = 22;
  404. animatedImageView.layer.masksToBounds = YES;
  405. [containerView addSubview:animatedImageView];
  406. // 标题获得奖励 往下30
  407. UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, 30)];
  408. label1.font = [UIFont systemFontOfSize:14];
  409. label1.text = adModel.success ? @"获得奖励": @"很遗憾";
  410. label1.textAlignment = NSTextAlignmentCenter;
  411. label1.textColor = [UIColor whiteColor];
  412. [containerView addSubview:label1];
  413. // 恭喜您获得
  414. UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, containerView.frame.size.width, 30)];
  415. label2.font = [UIFont systemFontOfSize:12];
  416. label2.text = adModel.success ? @"恭喜您获得": @"下次加油哦💪💪💪";
  417. label2.textAlignment = NSTextAlignmentCenter;
  418. label2.textColor = [UIColor whiteColor];
  419. [containerView addSubview:label2];
  420. // 奖品图标
  421. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(95, 88, 60, 60)];
  422. if (adModel.success) {
  423. [imageView sd_setImageWithURL:[NSURL URLWithString:adModel.linkUrl] placeholderImage:[UIImage imageNamed:@"gift_default"]];
  424. }else {
  425. imageView.image = [UIImage imageNamed:@"face_cry"];
  426. }
  427. [containerView addSubview:imageView];
  428. // 奖品名称
  429. UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, containerView.frame.size.width, 30)];
  430. label3.font = [UIFont systemFontOfSize:12];
  431. label3.text = adModel.content;
  432. label3.textAlignment = NSTextAlignmentCenter;
  433. label3.textColor = [UIColor whiteColor];
  434. [containerView addSubview:label3];
  435. // 确定按钮
  436. UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 205, containerView.frame.size.width, 30)];
  437. label4.font = [UIFont systemFontOfSize:13];
  438. label4.text = @"确定";
  439. label4.textAlignment = NSTextAlignmentCenter;
  440. label4.textColor = [UIColor whiteColor];
  441. [containerView addSubview:label4];
  442. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  443. [self addGestureRecognizer:singleTap];
  444. label4.userInteractionEnabled = YES;
  445. // 正常显示取消按钮
  446. //if (adModel.showCancelButton) {
  447. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  448. cancelBtn.frame=CGRectMake(JX_SCREEN_WIDTH-60, JX_SCREEN_WIDTH/3+ (JX_SCREEN_HEIGHT>=812?0:0), 44, 44);
  449. // cancelBtn.frame = CGRectMake(ScreenWidth/2-22, ScreenHeight-80, 44, 44);
  450. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  451. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  452. // [self addSubview:cancelBtn];
  453. // }
  454. }
  455. //初始化pageControl
  456. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  457. pageControl.numberOfPages = _itemsCount;
  458. pageControl.currentPage = 0;
  459. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  460. pageControl.hidden = self.hiddenPageControl;
  461. [self addSubview:pageControl];
  462. }
  463. - (void)setupView5 {
  464. for ( int i = 0; i < _itemsCount; i++ ) {
  465. DYAdModel *adModel = [_adDataList objectAtIndex:i];
  466. DYItemView*item = [[DYItemView alloc]initWithFrame:CGRectMake(ScreenWidth/2-375/2+i*ScrollWidth,0, 375, 600)];
  467. item.userInteractionEnabled = NO;
  468. item.index = i;
  469. item.tag = BaseTag+item.index;
  470. // UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgView:)];
  471. // [item.imageView addGestureRecognizer:singleTap];
  472. // [_scrollView addSubview:item];
  473. // 正常显示取消按钮
  474. cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  475. // cancelBtn.frame = CGRectMake(ScreenWidth-100, JX_SCREEN_HEIGHT/6, 44, 44);
  476. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  477. [cancelBtn addTarget:self action:@selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  478. if ([adModel.imgStr isEqualToString:@"红包领取失败"]) {
  479. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-opened-loop-animation"];
  480. animation.loopAnimation = YES;
  481. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  482. [self addSubview:animation];
  483. [animation playWithCompletion:^(BOOL animationFinished) {
  484. // Do Something
  485. }];
  486. [self addSubview:cancelBtn];
  487. UILabel *titleLong = [[UILabel alloc]init];
  488. titleLong.textColor=[UIColor whiteColor];
  489. titleLong.userInteractionEnabled=YES;
  490. titleLong.textAlignment=NSTextAlignmentCenter;
  491. titleLong.numberOfLines = 0;
  492. [animation addSubview:titleLong];
  493. if ([adModel.imgStr isEqualToString:@"红包领取失败"]) {
  494. titleLong.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
  495. titleLong.text = [NSString stringWithFormat:@"很遗憾\n红包已被领完"];
  496. }else {
  497. titleLong.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
  498. titleLong.text = [NSString stringWithFormat:@"中奖了\n%@元",adModel.content];
  499. }
  500. [titleLong mas_makeConstraints:^(MASConstraintMaker *make) {
  501. make.centerX.mas_equalTo(animation.mas_centerX);
  502. make.centerY.mas_equalTo(animation.mas_centerY).mas_offset(-5);;
  503. }];
  504. }else {
  505. LOTAnimationView *animation = [LOTAnimationView animationNamed:@"red-packet-opened-loop-animation"];
  506. animation.loopAnimation = YES;
  507. animation.frame = CGRectMake(0, JX_SCREEN_HEIGHT>=812?64:0, ScreenWidth, ScreenHeight>=812?JX_SCREEN_HEIGHT-JX_SCREEN_TOP-64:JX_SCREEN_HEIGHT);
  508. [self addSubview:animation];
  509. [animation playWithCompletion:^(BOOL animationFinished) {
  510. // Do Something
  511. }];
  512. UITapGestureRecognizer *singleTapBt = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContentImgViewXX:)];
  513. [animation addGestureRecognizer:singleTapBt];
  514. [self addSubview:cancelBtn];
  515. UILabel *titleLong = [[UILabel alloc]init];
  516. titleLong.textColor=[UIColor whiteColor];
  517. titleLong.userInteractionEnabled=YES;
  518. titleLong.textAlignment=NSTextAlignmentCenter;
  519. titleLong.numberOfLines = 0;
  520. [animation addSubview:titleLong];
  521. if ([adModel.imgStr isEqualToString:@"红包领取失败"]) {
  522. titleLong.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
  523. titleLong.text = [NSString stringWithFormat:@"很遗憾\n红包已被领完"];
  524. }else {
  525. titleLong.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
  526. titleLong.text = [NSString stringWithFormat:@"中奖了\n%@元",adModel.content];
  527. }
  528. [titleLong mas_makeConstraints:^(MASConstraintMaker *make) {
  529. make.centerX.mas_equalTo(animation.mas_centerX);
  530. make.centerY.mas_equalTo(animation.mas_centerY).mas_offset(-5);;
  531. }];
  532. }
  533. }
  534. //初始化pageControl
  535. pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight-120, ScreenWidth, 20)];
  536. pageControl.numberOfPages = _itemsCount;
  537. pageControl.currentPage = 0;
  538. [pageControl addTarget:self action:@selector(pageValueChange:) forControlEvents:UIControlEventValueChanged];
  539. pageControl.hidden = self.hiddenPageControl;
  540. [self addSubview:pageControl];
  541. }
  542. - (void)tapContentImgViewxx{
  543. [self removeFromSuperview];
  544. }
  545. +(void)dissmissUI{
  546. DYAdAlertView *dissVIew=[DYAdAlertView new];
  547. [dissVIew removeSelfFromSuperview];
  548. }
  549. -(void)dissmissDYAdAlertView{
  550. [self removeFromSuperview];
  551. }
  552. //点击文字 龙消失
  553. -(void)tapContentImgView:(UITapGestureRecognizer *)gesture{
  554. // UIView *imageView = gesture.view;
  555. // NSInteger itemTag = (long)imageView.tag-BaseTag;
  556. if ([self.delegate respondsToSelector:@selector(clickAlertViewAtIndex:)]){
  557. [self.delegate clickAlertViewAtIndex:0];
  558. [self removeSelfFromSuperview];
  559. // self.animaaaa=anima;
  560. //animatedImageView
  561. }
  562. }
  563. -(void)pageValueChange:(UIPageControl*)page{
  564. [UIView animateWithDuration:.35 animations:^{
  565. _scrollView.contentOffset = CGPointMake(page.currentPage*ScreenWidth, 0);
  566. }];
  567. }
  568. #pragma mark - UIScrollViewDelegate
  569. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  570. NSInteger index = scrollView.contentOffset.x/ScreenWidth;
  571. pageControl.currentPage = index;
  572. }
  573. @end
  574. /*********D*********Y**********分**********割**********线************/
  575. //自定义中间主界面
  576. @implementation DYItemView
  577. -(id)initWithFrame:(CGRect)frame{
  578. if (self = [super initWithFrame:frame]) {
  579. self.backgroundColor = [UIColor clearColor];
  580. [self setSubViews];
  581. }
  582. return self;
  583. }
  584. -(void)setSubViews{
  585. self.userInteractionEnabled = YES;
  586. self.layer.masksToBounds = YES;
  587. self.layer.cornerRadius = 5;
  588. self.layer.shadowOpacity = .2;
  589. self.layer.shadowOffset = CGSizeMake(0, 2.5);
  590. self.layer.shadowColor = [UIColor clearColor].CGColor;
  591. [self addSubview:self.imageView];
  592. [self addSubview:self.lable];
  593. }
  594. -(FLAnimatedImageView *)imageView{
  595. if (!_imageView) {
  596. _imageView = [[FLAnimatedImageView alloc]initWithFrame:self.bounds];
  597. _imageView.backgroundColor = [UIColor clearColor];
  598. _imageView.userInteractionEnabled = YES;
  599. _imageView.layer.masksToBounds = YES;
  600. }
  601. return _imageView;
  602. }
  603. -(UILabel *)lable {
  604. if (!_lable) {
  605. _lable = [[UILabel alloc] init];
  606. _lable.font = [UIFont systemFontOfSize:25];
  607. _lable.textColor = [UIColor whiteColor];
  608. _lable.userInteractionEnabled=YES;
  609. //_lable.backgroundColor=[UIColor redColor];
  610. _lable.frame = CGRectMake(0, 0, ScreenWidth, 10);
  611. //[_lable setCenter:CGPointMake(0, 50)];
  612. // _lable.text = @"哈哈哈啊哈哈\n哈哈 哈 哈哈\n 点击屠龙";
  613. _lable.numberOfLines = 0;
  614. _lable.textAlignment = NSTextAlignmentCenter;
  615. // [_lable sizeToFit];
  616. }
  617. return _lable;
  618. }
  619. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  620. [self endEditing:YES];
  621. }
  622. @end