手绘区域
RKPaintedGiftView.h
//
// RKPaintedGiftView.h
// iphoneLive
//
// Created by YB007 on 2020/8/27.
// Copyright © 2020 cat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RKPaintedGiftView : UIView
@property(nonatomic,strong)NSString *giftNameStr; //礼物名称
@property(nonatomic,strong)NSString *giftPathStr; //礼物地址
@property(nonatomic,assign)int minNum; //礼物最少个数【暂时未做判断】
@property(nonatomic,assign)int maxNum; //礼物最大个数【暂时未做判断】
@property(nonatomic,assign)BOOL showPainted; //展示绘制区域
-(CGSize)getPaintedRegion;
-(NSArray<NSString *> *)getPaintedPointArray;
@end
RKPaintedGiftView.m
//
// RKPaintedGiftView.m
// iphoneLive
//
// Created by YB007 on 2020/8/27.
// Copyright © 2020 cat. All rights reserved.
//
#import "RKPaintedGiftView.h"
@interface RKPaintedGiftView()
{
CGPoint _lastPoint;
}
@property(nonatomic,strong)UIView *funBgView;
@property(nonatomic,strong)UIButton *closeBtn;
@property(nonatomic,strong)UIButton *clearLastBtn;
@property(nonatomic,strong)UIButton *clearAllBtn;
@property(nonatomic,strong)NSMutableArray<NSString *> *pointsArray;
@property(nonatomic,strong)NSMutableArray<UIImageView *> *paintedGiftsArray;
@property(nonatomic,assign)CGFloat giftWidth;
@property(nonatomic,assign)CGFloat giftHeight;
@end
@implementation RKPaintedGiftView
- (instancetype)init {
self = [super init];
if (self) {
[self createUI];
}
return self;
}
-(void)createUI {
_minNum = 10;
_maxNum = 99;
self.backgroundColor = RGB_COLOR(@"#000000", 0.2);
self.pointsArray = [NSMutableArray array];
self .paintedGiftsArray = [NSMutableArray array];
_giftWidth = _giftHeight = _window_width/10;
[self addSubview:self.funBgView];
[_funBgView addSubview:self.closeBtn];
[_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_funBgView.mas_right).offset(0);
make.centerY.equalTo(_funBgView);
}];
[self addSubview:self.clearAllBtn];
[_clearAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(_closeBtn);
make.right.equalTo(_closeBtn.mas_left).offset(-8);
}];
[self addSubview:self.clearLastBtn];
[_clearLastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(_closeBtn);
make.right.equalTo(_clearAllBtn.mas_left).offset(-8);
}];
[_funBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).offset(-10);
make.height.mas_equalTo(50);
make.bottom.equalTo(self);
make.left.equalTo(_clearLastBtn.mas_left).offset(-20);
}];
//默认隐藏
self.hidden = YES;
}
- (void)setShowPainted:(BOOL)showPainted {
_showPainted = showPainted;
[_pointsArray removeAllObjects];
self.hidden = !_showPainted;
for (UIImageView* subImgView in _paintedGiftsArray) {
if ([subImgView isKindOfClass:[UIImageView class]]) {
[subImgView removeFromSuperview];
}
}
}
- (CGSize)getPaintedRegion {
return CGSizeMake(self.width, self.height);
}
-(NSArray<NSString *> *)getPaintedPointArray; {
return [NSArray arrayWithArray:_pointsArray];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer* )gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer* )otherGestureRecognizer {
return YES;
}
- (void)touchesBegan:(NSSet<UITouch* >* )touches withEvent:(UIEvent* )event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//NSLog(@"touchesBegan:x:=%f y:=%f",point.x,point.y);
if ([PublicObj checkNull:_giftPathStr] || CGRectContainsPoint(_funBgView.frame,point)) {
return;
}
if ( point.y > _giftHeight/2 && point.y < self.height-_giftHeight/2 ) {
if (_pointsArray.count <= _maxNum) {
UIImageView* imageView = [[UIImageView alloc] init];
[imageView sd_setImageWithURL:[NSURL URLWithString:_giftPathStr]];
[self addSubview:imageView];
[_paintedGiftsArray addObject:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker* make) {
make.top.equalTo(self).offset(point.y - _giftHeight/2);
make.left.equalTo(self).offset(point.x - _giftWidth/2);
make.width.mas_equalTo(_giftWidth);
make.height.mas_equalTo(_giftHeight);
}];
NSString* x = [NSString stringWithFormat:@"%d",(int)point.x];
NSString* y = [NSString stringWithFormat:@"%d",(int)point.y];
NSDictionary* pointDic = [NSDictionary dictionaryWithObjectsAndKeys:x,@"x",y,@"y", nil];
NSString* dicStr = [self dictionaryToJson:pointDic];
[_pointsArray addObject:dicStr];
_lastPoint = point;
/*
if (_pointsAry.count < 10) {
_contentLabel.text =[NSString stringWithFormat:@"至少绘制10个%@",_giftNameStr];
}else
{
_contentLabel.text = [NSString stringWithFormat:@"手绘%zd个%@,共计%zd¥",_pointsAry.count,_giftNameStr,_pointsAry.count*10];
[_giveBtn setBackgroundColor:[UIColor colorWithRed:252/255.f green:82/255.f blue:81/255.f alpha:1.f]];
}
*/
}
}
}
- (void)touchesMoved:(NSSet<UITouch* >* )touches withEvent:(UIEvent* )event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//NSLog(@"touchesMoved:x:=%f y:=%f",point.x,point.y);
if ([PublicObj checkNull:_giftPathStr] || CGRectContainsPoint(_funBgView.frame,point)) {
return;
}
if (point.y > _giftHeight/2 && point.y < self.height-_giftHeight/2 ) {
if ( fabs(point.x - _lastPoint.x) > _giftHeight/2 || fabs(point.y - _lastPoint.y) > _giftHeight/2) {
if (_pointsArray.count <= _maxNum) {
UIImageView* imageView = [[UIImageView alloc] init];
[imageView sd_setImageWithURL:[NSURL URLWithString:_giftPathStr]];
[self addSubview:imageView];
[_paintedGiftsArray addObject:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker* make) {
make.top.equalTo(self).offset(point.y - _giftHeight/2);
make.left.equalTo(self).offset(point.x - _giftWidth/2);
make.width.mas_equalTo(_giftWidth);
make.height.mas_equalTo(_giftHeight);
}];
NSString* x = [NSString stringWithFormat:@"%d",(int)point.x];
NSString* y = [NSString stringWithFormat:@"%d",(int)point.y];
NSDictionary* pointDic = [NSDictionary dictionaryWithObjectsAndKeys:x,@"x",y,@"y", nil];
NSString* dicStr = [self dictionaryToJson:pointDic];
[_pointsArray addObject:dicStr];
_lastPoint = point;
/*
if (_pointsAry.count < 10) {
_contentLabel.text =[NSString stringWithFormat:@"至少绘制10个%@",_giftNameStr];
}else
{
_contentLabel.text = [NSString stringWithFormat:@"手绘%zd个%@,共计%zd¥",_pointsAry.count,_giftNameStr,_pointsAry.count*10];
[_giveBtn setBackgroundColor:[UIColor colorWithRed:252/255.f green:82/255.f blue:81/255.f alpha:1.f]];
}
*/
}
}
}
}
- (void)touchesEnded:(NSSet<UITouch* >* )touches withEvent:(UIEvent* )event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"touchesEnded:x:=%f y:=%f",point.x,point.y);
}
- (UIView *)funBgView {
if (!_funBgView) {
_funBgView = [[UIView alloc]init];
_funBgView.backgroundColor = UIColor.redColor;
}
return _funBgView;
}
- (UIButton *)closeBtn {
if (!_closeBtn) {
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_closeBtn.titleLabel.font = SYS_Font(13);
[_closeBtn addTarget:self action:@selector(clikcCloseBtn) forControlEvents:UIControlEventTouchUpInside];
[_closeBtn setTitle:YZMsg(@"关闭") forState:0];
[_closeBtn setTitleColor:Pink_Cor forState:0];
_closeBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
_closeBtn.backgroundColor = UIColor.greenColor;
}
return _closeBtn;
}
-(void)clikcCloseBtn {
self.showPainted = NO;
}
- (UIButton *)clearAllBtn {
if (!_clearAllBtn) {
_clearAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_clearAllBtn.titleLabel.font = SYS_Font(13);
[_clearAllBtn addTarget:self action:@selector(clikcClearAllBtn) forControlEvents:UIControlEventTouchUpInside];
[_clearAllBtn setTitle:YZMsg(@"清除全部") forState:0];
[_clearAllBtn setTitleColor:Pink_Cor forState:0];
_clearAllBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
_clearAllBtn.backgroundColor = UIColor.greenColor;
}
return _clearAllBtn;
}
-(void)clikcClearAllBtn {
[_pointsArray removeAllObjects];
for (UIImageView* subImgView in _paintedGiftsArray) {
if ([subImgView isKindOfClass:[UIImageView class]]) {
[subImgView removeFromSuperview];
}
}
}
- (UIButton *)clearLastBtn {
if (!_clearLastBtn) {
_clearLastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_clearLastBtn.titleLabel.font = SYS_Font(13);
[_clearLastBtn addTarget:self action:@selector(clikcClearLastBtn) forControlEvents:UIControlEventTouchUpInside];
[_clearLastBtn setTitle:YZMsg(@"回退") forState:0];
[_clearLastBtn setTitleColor:Pink_Cor forState:0];
_clearLastBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
_clearLastBtn.backgroundColor = UIColor.greenColor;
}
return _clearLastBtn;
}
-(void)clikcClearLastBtn {
if (_pointsArray.count>0) {
[_pointsArray removeLastObject];
UIImageView *imgIV = [_paintedGiftsArray lastObject];
[imgIV removeFromSuperview];
[_paintedGiftsArray removeLastObject];
}
}
- (NSString* )dictionaryToJson:(NSDictionary* )dic {
NSError* parseError = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
手绘绘制完毕,送出展示区域
RKShowPaintedView.h
//
// RKShowPaintedView.h
// iphoneLive
//
// Created by YB007 on 2020/8/27.
// Copyright © 2020 cat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RKShowPaintedView : UIView
@property(nonatomic,strong)NSString *giftPathStr; //手绘礼物地址
@property(nonatomic,strong)NSArray *paintedPointArray; //手绘礼物图形路径
@property(nonatomic,assign)CGFloat paintedWidth; //发送方手绘礼物区域宽度
@property(nonatomic,assign)CGFloat paintedHeight; //发送方手绘礼物区域高度
@end
RKShowPaintedView.m
//
// RKShowPaintedView.m
// iphoneLive
//
// Created by YB007 on 2020/8/27.
// Copyright © 2020 cat. All rights reserved.
//
#import "RKShowPaintedView.h"
@interface RKShowPaintedView() {
int imgCount;
}
@property(nonatomic,strong)NSMutableArray<UIImageView *> *paintedGiftsArray;
@property(nonatomic,assign)CGFloat giftWidth;
@property(nonatomic,assign)CGFloat giftHeight;
@end
@implementation RKShowPaintedView
- (instancetype)init {
self = [super init];
if (self) {
self.hidden = YES;
imgCount = 0;
_giftWidth = _giftHeight = _window_width/10;
self.paintedGiftsArray = [NSMutableArray array];
}
return self;
}
- (void)setPaintedWidth:(CGFloat)paintedWidth{
_paintedWidth = paintedWidth;
[self resetSelfUI];
}
- (void)setPaintedHeight:(CGFloat)paintedHeight {
_paintedHeight = paintedHeight;
[self resetSelfUI];
}
-(void)resetSelfUI {
if (_paintedWidth<=0) {
_paintedWidth = 1;
}
if (_paintedHeight<=0) {
_paintedHeight = 1;
}
CGFloat hhhhh = self.superview.width*_paintedHeight/_paintedWidth;
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.superview);
make.width.equalTo(self.superview);
make.height.mas_equalTo(hhhhh);
make.top.equalTo(self.superview.mas_top).offset(statusbarHeight);
}];
}
- (void)setPaintedPointArray:(NSArray *)paintedPointArray {
_paintedPointArray = paintedPointArray;
[_paintedGiftsArray removeAllObjects];
[self layoutIfNeeded];
self.hidden = NO;
[self removeAllSubViews];
if ([PublicObj checkNull:_giftPathStr]) {
return;
}
for (NSString* dicStr in _paintedPointArray) {
NSDictionary* dic = [self dictionaryWithJsonString:dicStr];
//坐标转换
CGFloat originX = [[dic objectForKey:@"x"] floatValue];
CGFloat originY = [[dic objectForKey:@"y"] floatValue];
if (_paintedWidth<=0) {
_paintedWidth = 1;
}
if (_paintedHeight<=0) {
_paintedHeight = 1;
}
CGFloat nowX = originX * self.width/_paintedWidth;
CGFloat nowY = originY * self.height/_paintedHeight;
CGPoint point = CGPointMake(nowX, nowY);
UIImageView* imageView = [[UIImageView alloc] init];
[imageView sd_setImageWithURL:[NSURL URLWithString:_giftPathStr]];
[self addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker* make) {
make.top.equalTo(self).offset(point.y - _giftWidth/2);
make.left.equalTo(self).offset(point.x - _giftHeight/2);
make.width.mas_equalTo(_giftWidth);
make.height.mas_equalTo(_giftHeight);
}];
imageView.hidden = YES;
[_paintedGiftsArray addObject:imageView];
}
[self showAnimation];
NSLog(@"00000000-----");
}
-(void)showAnimation {
NSLog(@"000000000");
if (imgCount >= _paintedGiftsArray.count) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
imgCount = 0;
[_paintedGiftsArray removeAllObjects];
self.hidden = YES;
for (UIImageView *imgIV in _paintedGiftsArray) {
[imgIV.layer removeAnimationForKey:@"animateLayer"];
}
[self removeAllSubViews];
});
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self startAnimation:_paintedGiftsArray[imgCount]];
imgCount ++;
[self showAnimation];
});
}
-(void)startAnimation:(UIImageView *)imgIV {
NSLog(@"11111111111");
imgIV.hidden = NO;
CABasicAnimation* momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
momAnimation.fromValue = [NSNumber numberWithFloat:-0.3];
momAnimation.toValue = [NSNumber numberWithFloat:0.3];
momAnimation.duration = 0.5;
momAnimation.repeatCount = CGFLOAT_MAX;
momAnimation.autoreverses = YES;
[imgIV.layer addAnimation:momAnimation forKey:@"animateLayer"];
}
- (NSDictionary* )dictionaryWithJsonString:(NSString* )jsonString {
if (jsonString == nil) {
return nil;
}
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError* err;
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if (err) {
NSLog(@"json解析失败:%@",err);
return nil;
}
return dic;
}
@end
外部调用
手绘区域
- (RKPaintedGiftView *)paintedRegion {
if (!_paintedRegion) {
_paintedRegion = [[RKPaintedGiftView alloc]init];
//_paintedRegion.backgroundColor = UIColor.redColor;
}
return _paintedRegion;
}
//选中礼物开始手绘 - 赋值
_paintedRegion.giftNameStr = @"手绘礼物名称";
_paintedRegion.giftPathStr = @"手绘礼物地址";
_paintedRegion.showPainted = YES;
//绘制完毕准备送出
NSMutableDictionary *dic = @{
@"avatar":[Config getUserAvatar],
@"nickname":[Config getOwnNicename],
@"uid":[Config getOwnID],
#warning rk_手绘路径
@"paintedPath":[_paintedRegion getPaintedPointArray],
@"paintedW":@([_paintedRegion getPaintedRegion].width),
@"paintedH":@([_paintedRegion getPaintedRegion].height),
}.mutableCopy;
礼物展示区域
- (RKShowPaintedView *)paintedShowRegion {
if (!_paintedShowRegion) {
_paintedShowRegion = [[RKShowPaintedView alloc]init];
}
return _paintedShowRegion;
}
//收到上面送出发送的 dic
_paintedShowRegion.giftPathStr = minstr([playDic valueForKey:@"gifticon"]);
_paintedShowRegion.paintedWidth = [minstr([playDic valueForKey:@"paintedW"]) floatValue];
_paintedShowRegion.paintedHeight = [minstr([playDic valueForKey:@"paintedH"]) floatValue];
//这里数组赋值便会自动显示动画
_paintedShowRegion.paintedPointArray = [NSArray arrayWithArray:[playDic valueForKey:@"paintedPath"]];