脚本宝典收集整理的这篇文章主要介绍了UI笔记 - 训练营| Quart 2D:撕衣服,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
撕衣服
1 - 设计思路
① 两张图层:底层图充当最终效果图;上层图充当绘制层(实际操作层)
② 两张图层的 frame 要保持一致且上层图背景颜色置透明色
2 - 代码示例
1 #import "ViewController.h" 2 // 默认尺寸 3 #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width 4 #define SCREENH_HeiGHT [UIScreen mainScreen].bounds.size.height 5 #import "SecondViewController.h" 6 @interface ViewController() 7 8 @PRoPErty(nonatomic,strong)UIImageView *imageBack;// 显示底层图 9 @property(nonatomic,strong)UIImageView *imageFont;// 显示上层图 10 @property(nonatomic,assign) BOOL isTouch; 11 12 @end 13 14 @implementation ViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 self.view.backgroundColor = [UIColor yellowColor]; 19 20 // 底层 21 self.imageBack = [[UIImageView alloc] inITWithFrame:CGRectMake(10, 84, SCREEN_WIDTH-20, SCREENH_HEIGHT-104)]; 22 self.imageBack.backgroundColor = [UIColor redColor]; 23 self.imageBack.image = [UIImage imageNamed:@"09B.jpg"]; 24 [self.view addSubview:self.imageBack]; 25 26 // 上层 27 self.imageFont = [[UIImageView alloc] initWithFrame:self.imageBack.frame]; 28 self.imageFont.backgroundColor = [UIColor clearColor]; 29 self.imageFont.userInteractionEnabled = YES; 30 self.imageFont.image = [UIImage imageNamed:@"09A.jpg"]; 31 [self.view addSubview:self.imageFont]; 32 33 } 34 35 // 判断上层图片,打开触摸 36 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 37 38 UITouch *touch = [touches anyObject]; 39 if (touch.view == self.imageFont) { 40 self.isTouch = YES; 41 } 42 } 43 44 - (void)touchESMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 45 46 if (self.isTouch) { 47 48 // 开启上下文 49 UIGraphicsBeginImageContext(self.imageFont.frame.size); 50 // 将上层图片绘制到图形上下文中 51 [self.imageFont.image drawInRect:self.imageFont.bounds]; 52 53 // 清空手指触摸的位置:拿到手指,根据手指的位置,让对应的位置成为透明 54 UITouch *touch = [touches anyObject]; 55 CGPoint point = [touch locationInView:touch.view]; 56 57 // 清空区域 58 CGRect rect = CGRectMake(point.x - 40, point.y - 40, 40, 40); 59 CGContextClearRect(UIGraphicsGetcurrentContext(), rect);// 清空 60 self.imageFont.image = UIGraphicsGetImageFromCurrentImageContext(); 61 UIGraphicsEndImageContext(); // 关闭 62 } 63 64 } 65 66 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 67 self.isTouch = NO; 68 } 69 70 @end
素材链接
https://pan.baidu.COM/s/1AoLNp_RA1qM7ECac7NfcTQ
rong
以上是脚本宝典为你收集整理的UI笔记 - 训练营| Quart 2D:撕衣服全部内容,希望文章能够帮你解决UI笔记 - 训练营| Quart 2D:撕衣服所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。