1.首先要分析手绘礼物的构成。
主要要素是手势+触摸点+图片,只要记录触摸的各个点和监听手指在屏幕的滑动就可以了。
2.如何绘制
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
这三个方法用来监听和记录手指触摸的点,可以将触摸点设置为图片中心,也可以根据触摸点去设置图片的位置。
3.具体实现
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self];
if (fabs(fabs(_x) - fabs(point.x)) < 25 && fabs(fabs(_y) - fabs(point.y)) < 25) {
return;
}
// [_path addLineToPoint:point];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"timg.jpeg"]];
imageView.center = point;
[self.imgMutArray addObject:imageView];
[self addSubview:imageView];
// [self setNeedsDisplay];
_x = point.x;
_y = point.y;
[self.mutArray addObject:@(point)];
NSLog(@"(%.1f,%.1f)",point.x,point.y);
}
主要实现方法在这里,我记录了上次的点位置,利用限制间隔距离使得图片之间不重叠,用到UIBezierPath是用来测试路径,当然把图片去掉就是手绘了。
4.接下来看看效果