020 涂鸦
/**
确定起点
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获得当前的触摸点
UITouch *touch = [touches anyObject];
CGPoint startPos = [touch locationInView:touch.view];
// 2.创建一个新的路径
UIBezierPath *currenPath = [UIBezierPath bezierPath];
currenPath.lineCapStyle = kCGLineCapRound;
currenPath.lineJoinStyle = kCGLineJoinRound;
// 设置起点
[currenPath moveToPoint:startPos];
// 3.添加路径到数组中
[self.paths addObject:currenPath];
[self setNeedsDisplay];
}
/**
连线
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pos = [touch locationInView:touch.view];
UIBezierPath *currentPath = [self.paths lastObject];
[currentPath addLineToPoint:pos];
[self setNeedsDisplay];
}
/**
连线
*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)setColor
{
self.color = [UIColor colorWithRed:(arc4random()%255 / 255.0) green:(arc4random()%255 / 255.0) blue:(arc4random()%255 / 255.0) alpha:(arc4random()%255 / 255.0)];
}
- (void)drawRect:(CGRect)rect
{
[self.color set];
for (UIBezierPath *path in self.paths) {
path.lineWidth = self.width;
[path stroke];
}
}
/**
确定起点
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获得当前的触摸点
UITouch *touch = [touches anyObject];
CGPoint startPos = [touch locationInView:touch.view];
// 2.创建一个新的路径
UIBezierPath *currenPath = [UIBezierPath bezierPath];
currenPath.lineCapStyle = kCGLineCapRound;
currenPath.lineJoinStyle = kCGLineJoinRound;
// 设置起点
[currenPath moveToPoint:startPos];
// 3.添加路径到数组中
[self.paths addObject:currenPath];
[self setNeedsDisplay];
}
/**
连线
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pos = [touch locationInView:touch.view];
UIBezierPath *currentPath = [self.paths lastObject];
[currentPath addLineToPoint:pos];
[self setNeedsDisplay];
}
/**
连线
*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)setColor
{
self.color = [UIColor colorWithRed:(arc4random()%255 / 255.0) green:(arc4random()%255 / 255.0) blue:(arc4random()%255 / 255.0) alpha:(arc4random()%255 / 255.0)];
}
- (void)drawRect:(CGRect)rect
{
[self.color set];
for (UIBezierPath *path in self.paths) {
path.lineWidth = self.width;
[path stroke];
}
}
本文介绍了一个简单的iOS涂鸦应用的实现过程,包括如何使用UIKit框架处理触摸事件来绘制线条,以及如何设置颜色和线宽等特性。
1995

被折叠的 条评论
为什么被折叠?



