Files
yinmeng-ios/xplan-ios/Main/Room/View/AnimationView/XPRoomGraffitiGiftAnimationView.m
2022-08-24 18:28:13 +08:00

85 lines
2.4 KiB
Objective-C

//
// XPRoomGraffitiGiftAnimationView.m
// xplan-ios
//
// Created by 冯硕 on 2022/8/23.
//
#import "XPRoomGraffitiGiftAnimationView.h"
#import "XPWeakTimer.h"
#import "XPMacro.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)) {
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 objectAtIndex: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