// // XPGraffitiGiftView.m // xplan-ios // // Created by 冯硕 on 2022/8/22. // #import "XPGraffitiGiftView.h" ///Third #import ///Tool #import "ThemeColor.h" #import "XPMacro.h" #import "XCHUDTool.h" @interface XPGraffitiGiftView () ///回话的内容 @property (nonatomic,strong) UIView * contentView; ///绘画的view @property (nonatomic,strong) UIView * drawView; ///类型 @property (nonatomic,strong) UIStackView *tipsStackView; ///✋🏻 @property (nonatomic,strong) UIImageView *handImageView; ///提醒 @property (nonatomic,strong) UILabel *tipsLabel; ///toolView @property (nonatomic,strong) UIView * toolView; ///至少花多少个 @property (nonatomic,strong) UILabel *titleLabel; ///类型 @property (nonatomic,strong) UIStackView *toolStackView; ///撤销 @property (nonatomic,strong) UIButton *repealButton; ///删除 @property (nonatomic,strong) UIButton *deleteButton; ///关闭 @property (nonatomic,strong) UIButton *closeButton; ///手指滑动的point @property (nonatomic,assign) CGPoint panPoint; ///开始的point @property (nonatomic,assign) CGPoint beginPoint; ///两个涂鸦的间距 @property (nonatomic,assign) CGFloat step; /// @property (nonatomic,assign) int index; ///是否展示超过最大值的 @property (nonatomic,assign) BOOL isShowMaxToast; @property (nonatomic,strong) NSMutableArray *viewsArray; @property (nonatomic,strong) NSMutableArray *indexArray; @property (nonatomic,strong) NSMutableArray *> *pointArray; @end @implementation XPGraffitiGiftView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { self.isShowMaxToast = YES; self.step = 10; UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [self addGestureRecognizer:panGes]; self.backgroundColor = [UIColor clearColor]; [self addSubview:self.contentView]; [self.contentView addSubview:self.tipsStackView]; [self.contentView addSubview:self.drawView]; [self.contentView addSubview:self.toolView]; [self.tipsStackView addArrangedSubview:self.handImageView]; [self.tipsStackView addArrangedSubview:self.tipsLabel]; [self.toolView addSubview:self.titleLabel]; [self.toolView addSubview:self.toolStackView]; [self.toolStackView addArrangedSubview:self.repealButton]; [self.toolStackView addArrangedSubview:self.deleteButton]; [self.toolStackView addArrangedSubview:self.closeButton]; } - (void)initSubViewConstraints { CGFloat avatarHeight = (43 + 15 * 2); CGFloat bottomHeight = (45); [self mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(KScreenWidth); make.height.mas_equalTo(KScreenHeight - avatarHeight - bottomHeight); }]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self); }]; [self.tipsStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.contentView); make.width.mas_equalTo(KScreenWidth); }]; [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.contentView).offset(kSafeAreaTopHeight); make.left.right.mas_equalTo(self.contentView); make.bottom.mas_equalTo(self.toolView.mas_top); }]; [self.handImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(78, 93)); }]; [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.left.right.mas_equalTo(self.contentView); make.height.mas_equalTo(56); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.toolView).offset(15); make.centerY.mas_equalTo(self.toolView); }]; [self.toolStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.toolView).offset(-15); make.height.mas_equalTo(22); make.centerY.mas_equalTo(self.toolView); }]; [self.repealButton mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(22); }]; [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(22); }]; [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(22); }]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.tipsStackView.hidden = YES; UITouch *touch = touches.anyObject; CGPoint point = [touch locationInView:self.contentView]; self.beginPoint = point; } - (void)panAction:(UIPanGestureRecognizer *)tap { if (self.pointArray.count >= 300) { if (self.isShowMaxToast) { [XCHUDTool showErrorWithMessage:@"最多只能画300个呦"]; self.isShowMaxToast = YES; } return; } if (tap.state == UIGestureRecognizerStateBegan) { [self.indexArray addObject:[NSNumber numberWithInteger:self.viewsArray.count]]; } else if(tap.state == UIGestureRecognizerStateEnded) { self.isShowMaxToast = YES; } CGPoint point = [tap translationInView:self.contentView]; CGFloat dx = [self myAbs:point.x - self.panPoint.x]; CGFloat dy = [self myAbs:point.y - self.panPoint.y]; CGPoint drawPoint = CGPointMake(self.beginPoint.x + point.x , self.beginPoint.y + point.y); if ((dx * dx + dy * dy) > 2 * self.step * self.step) { [self addSubView:drawPoint]; self.panPoint = point; } } - (int)myAbs:(int)num { if (num >= 0) { return num; } return -num; } - (void)addSubView:(CGPoint)point { UIImageView * imageView = [[UIImageView alloc] init]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.layer.masksToBounds = YES; imageView.image = self.image; imageView.frame = CGRectMake(point.x, point.y, 20, 20); [self.drawView addSubview:imageView]; [self.viewsArray addObject:imageView]; [self.pointArray addObject:@[@([self changeWidthDraw:point.x]), @([self changeHeightDraw:point.y])]]; [self cratePriceAttribute]; if (self.pointArray.count >= 10 && self.delegate && [self.delegate respondsToSelector:@selector(xPGraffitiGiftView:didDrawCompletion:)]) { [self.delegate xPGraffitiGiftView:self didDrawCompletion:self.pointArray]; } } - (NSInteger)changeWidthDraw:(CGFloat)number { return number / KScreenWidth * 1000; } - (NSInteger)changeHeightDraw:(CGFloat)number { return number / KScreenHeight * 1000; } - (void)clearData { [self.viewsArray makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self.viewsArray removeAllObjects]; [self.pointArray removeAllObjects]; [self.indexArray removeAllObjects]; } #pragma mark - Event Response - (void)repealButtonAction:(UIButton *)sender { NSInteger index = [self.indexArray lastObject].integerValue; if (index < self.viewsArray.count) { NSArray * array = [self.viewsArray subarrayWithRange:NSMakeRange(index, self.viewsArray.count - index)]; NSArray * array2 = [self.pointArray subarrayWithRange:NSMakeRange(index, self.pointArray.count - index)]; [self.pointArray removeObjectsInArray:array2]; [self.viewsArray removeObjectsInArray:array]; if (array.count) { [array makeObjectsPerformSelector:@selector(removeFromSuperview)]; } [self.indexArray removeLastObject]; [self cratePriceAttribute]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGraffitiGiftView:didDrawCompletion:)]) { [self.delegate xPGraffitiGiftView:self didDrawCompletion:self.pointArray]; } } } - (void)deleteButtonAction:(UIButton *)sender { [self clearData]; [self cratePriceAttribute]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGraffitiGiftView:didDrawCompletion:)]) { [self.delegate xPGraffitiGiftView:self didDrawCompletion:self.pointArray]; } } - (void)closeButtonAction:(UIButton *)sender { [self clearData]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGraffitiGiftView:didDrawCompletion:)]) { [self.delegate xPGraffitiGiftView:self didDrawCompletion:self.pointArray]; } if (self.delegate && [self.delegate respondsToSelector:@selector(xPGraffitiGiftView:didClickClose:)]) { [self.delegate xPGraffitiGiftView:self didClickClose:sender]; } } - (void)cratePriceAttribute { if (self.viewsArray.count < 10) { NSString * title = @"至少画10个才能送出"; NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:[UIColor whiteColor]}]; self.titleLabel.attributedText = attribute; } else { NSInteger number = self.selectUidNumber > 0 ? self.selectUidNumber : 1; NSString * count = [NSString stringWithFormat:@"%ld", self.viewsArray.count]; NSString * price = [NSString stringWithFormat:@"%.0f", self.viewsArray.count * self.price * number]; NSString * title = [NSString stringWithFormat:@"已画%@个,需花费%@钻石", count, price]; NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:[UIColor whiteColor]}]; [attribute addAttribute:NSForegroundColorAttributeName value:[ThemeColor appMainColor] range:[title rangeOfString:count]]; [attribute addAttribute:NSForegroundColorAttributeName value:[ThemeColor appMainColor] range:[title rangeOfString:price]]; self.titleLabel.attributedText = attribute; } } #pragma mark - Getters And Setters - (void)setSelectUidNumber:(NSInteger)selectUidNumber { _selectUidNumber = selectUidNumber; [self cratePriceAttribute]; } - (UIView *)contentView { if (!_contentView) { _contentView = [[UIView alloc] init]; _contentView.backgroundColor = UIColorRGBAlpha(0x000000, 0.7); } return _contentView; } - (UIView *)drawView { if (!_drawView) { _drawView = [[UIView alloc] init]; _drawView.backgroundColor = [UIColor clearColor]; _drawView.layer.masksToBounds = YES; } return _drawView; } - (UIStackView *)tipsStackView { if (!_tipsStackView) { _tipsStackView = [[UIStackView alloc] init]; _tipsStackView.axis = UILayoutConstraintAxisVertical; _tipsStackView.distribution = UIStackViewDistributionFill; _tipsStackView.alignment = UIStackViewAlignmentCenter; _tipsStackView.spacing = 17; } return _tipsStackView; } - (UIImageView *)handImageView { if (!_handImageView) { _handImageView = [[UIImageView alloc] init]; _handImageView.userInteractionEnabled = YES; _handImageView.image = [UIImage imageNamed:@"room_gift_graffiti_hand"]; } return _handImageView; } - (UILabel *)tipsLabel { if (!_tipsLabel) { _tipsLabel = [[UILabel alloc] init]; _tipsLabel.font = [UIFont systemFontOfSize:14]; _tipsLabel.textColor = [ThemeColor textThirdColor]; _tipsLabel.text = @"滑动手指,绘制图形"; } return _tipsLabel; } - (UIView *)toolView { if (!_toolView) { _toolView = [[UIView alloc] init]; _toolView.backgroundColor = UIColorRGBAlpha(0xFFFFFF, 0.2); CAShapeLayer * layer = [CAShapeLayer layer]; layer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, KScreenWidth, 56) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)].CGPath; _toolView.layer.mask = layer; } return _toolView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.textColor = [UIColor whiteColor]; _titleLabel.text = @"至少画10个才能送出"; } return _titleLabel; } - (UIStackView *)toolStackView { if (!_toolStackView) { _toolStackView = [[UIStackView alloc] init]; _toolStackView.axis = UILayoutConstraintAxisHorizontal; _toolStackView.distribution = UIStackViewDistributionFill; _toolStackView.alignment = UIStackViewAlignmentCenter; _toolStackView.spacing = 12; } return _toolStackView; } - (UIButton *)repealButton { if (!_repealButton) { _repealButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_repealButton setImage:[UIImage imageNamed:@"room_gift_graffiti_repeal"] forState:UIControlStateNormal]; [_repealButton setImage:[UIImage imageNamed:@"room_gift_graffiti_repeal"] forState:UIControlStateSelected]; [_repealButton addTarget:self action:@selector(repealButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _repealButton; } - (UIButton *)deleteButton { if (!_deleteButton) { _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_deleteButton setImage:[UIImage imageNamed:@"room_gift_graffiti_delete"] forState:UIControlStateNormal]; [_deleteButton setImage:[UIImage imageNamed:@"room_gift_graffiti_delete"] forState:UIControlStateSelected]; [_deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _deleteButton; } - (UIButton *)closeButton { if (!_closeButton) { _closeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_closeButton setImage:[UIImage imageNamed:@"room_gift_graffiti_close"] forState:UIControlStateNormal]; [_closeButton setImage:[UIImage imageNamed:@"room_gift_graffiti_close"] forState:UIControlStateSelected]; [_closeButton addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _closeButton; } - (NSMutableArray *)indexArray { if (!_indexArray) { _indexArray = [NSMutableArray array]; } return _indexArray; } - (NSMutableArray *)pointArray { if (!_pointArray) { _pointArray = [NSMutableArray array]; } return _pointArray; } - (NSMutableArray *)viewsArray { if (!_viewsArray) { _viewsArray = [NSMutableArray array]; } return _viewsArray; } @end