// // YMRoomGraffitiGiftAnimationView.m // YUMI // // Created by YUMI on 2022/8/23. // #import "XPRoomGraffitiGiftAnimationView.h" #import "XPWeakTimer.h" #import "YUMIMacroUitls.h" #import "NSArray+Safe.h" @interface XPRoomGraffitiGiftAnimationView () @property (nonatomic,strong) NSTimer *timer; @property (nonatomic,assign) int index; ///数据 @property (nonatomic,strong) NSMutableArray *datasource; @end @implementation XPRoomGraffitiGiftAnimationView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.userInteractionEnabled = NO; self.timer = [XPWeakTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dramGraffitiAnimationView) userInfo:@"" repeats:YES]; } return self; } - (void)beginDrawAnimation { self.index = 0; for (int i = 0; i < self.pointArray.count; i++) { NSArray * pointArray = self.pointArray[i]; NSNumber * first = [pointArray firstObject]; NSNumber * second = [pointArray lastObject]; CGFloat x = first.floatValue / 1000.0 * KScreenWidth; CGFloat y = second.floatValue / 1000.0 * KScreenHeight; if (x <= 0 || x >= KScreenWidth || y <= 0 || y >= KScreenHeight) { continue; } CGPoint point = CGPointMake(x, y); [self.datasource addObject:[NSValue valueWithCGPoint:point]]; } self.timer.fireDate = NSDate.distantPast; } - (void)endDrawAnimation { self.timer.fireDate = NSDate.distantFuture; [self.datasource removeAllObjects]; self.index = 0; } - (void)dramGraffitiAnimationView { if (self.index >= (self.datasource.count -1)) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.timer.fireDate = NSDate.distantFuture; [self.datasource removeAllObjects]; self.index = 0; [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomGraffitiGiftAnimationViewCompletion:attachment:)]) { [self.delegate xPRoomGraffitiGiftAnimationViewCompletion:self attachment:self.model]; } }); return; } NSValue * valeu= [self.datasource safeObjectAtIndex1:self.index]; CGPoint point = valeu.CGPointValue; UIImageView * imageView = [[UIImageView alloc] init]; imageView.layer.masksToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.image = self.giftImage; imageView.frame = CGRectMake(point.x, point.y, 20, 20); [self addSubview:imageView]; self.index++; } - (NSMutableArray *)datasource { if (!_datasource) { _datasource = [NSMutableArray array]; } return _datasource; } @end