iOS绘画板

作者: 徐老茂 | 来源:发表于2015-12-07 12:36 被阅读262次

今天给大家讲讲如何用代码实现绘图功能,虽然是很简陋的绘图功能,但也值得大家一看.先看看效果图吧.


从这副图中大家可以看出我在艺术方面的造诣还是挺高的,怎么实现的呢,其实也不难,直接上代码吧.

自定义的TouchLineView

#import "TouchLineView.h"
@implementation TouchLineView
{
    NSMutableArray *strokes;
    NSMutableDictionary *touchPaths;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;//打开用户交互
        strokes = [NSMutableArray array];
        touchPaths = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)clear
{
    [strokes removeAllObjects];//移除所有对象
    [self setNeedsDisplay];
}
//触摸开始时的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        NSString *key = [NSString stringWithFormat:@"%d", (int) touch];
        CGPoint point = [touch locationInView:self];//触摸点的位置
        
        UIBezierPath *path = [UIBezierPath bezierPath];//UIBezierPath类允许您定义一个路径组成的直线和曲线段和渲染路径在您的自定义视图
        path.lineWidth =  4;//线宽
        path.lineCapStyle = kCGLineCapRound;//线条样式
        [path moveToPoint:point];//当前点移动到指定的位置
        
        [touchPaths setObject:path forKey:key];//将path保存在字典里
    }
}
//手指移动时的方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        NSString *key = [NSString stringWithFormat:@"%d", (int) touch];
        UIBezierPath *path = [touchPaths objectForKey:key];
        if (!path) break;
        
        CGPoint point = [touch locationInView:self];
        [path addLineToPoint:point];//将移动的点添加到path里
    }
    [self setNeedsDisplay];//标志着当前view的整个边界矩形需要重绘
}
//手指离开屏幕时的方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        NSString *key = [NSString stringWithFormat:@"%d", (int) touch];
        UIBezierPath *path = [touchPaths objectForKey:key];
        if (path) [strokes addObject:path];
        [touchPaths removeObjectForKey:key];//当手指离开屏幕让移除touchPaths里的值,只剩strokes,这时灰线变成黑线
    }
    [self setNeedsDisplay];
}

//传入矩形内的线条。
- (void)drawRect:(CGRect)rect
{
    //手指离开屏幕时线条的颜色
    [[UIColor blackColor] set];
    for (UIBezierPath *path in strokes)
        [path stroke];//画一条线沿接收器使用当前绘图属性的路径
    
    //手指在屏幕上移动时屏幕的颜色
    [[UIColor grayColor] set];
    for (UIBezierPath *path in [touchPaths allValues])
    {
        [path stroke];
    }
}

@end

ViewController

#import "XMHDrawViewController.h"
#import "TouchLineView.h"
@interface XMHDrawViewController ()
@property(nonatomic, strong)TouchLineView *touchlineView;
@end

@implementation XMHDrawViewController

- (void)loadView
{
    [super loadView];
    _touchlineView = [[TouchLineView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
    self.view.backgroundColor = [UIColor whiteColor];
      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStylePlain target:self action:@selector(clear)];
    [self.view addSubview:_touchlineView];
}

- (void)clear
{
    [_touchlineView clear];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这样运行就可以了,大家去画一画试试吧.祝大家开心😄

相关文章

  • 绘画板隐私政策

    绘画板 非常重视用户隐私的保护。本隐私政策适用于绘画板 iOS 客户端(iPad和iPhone)的产品和服务。请您...

  • iOS绘画板

    今天给大家讲讲如何用代码实现绘图功能,虽然是很简陋的绘图功能,但也值得大家一看.先看看效果图吧. 从这副图中大家可...

  • 无标题文章

    会合Yui绘画板

  • SAI 2.0简体中文版软件+安装教程

    SAI一直以来都是许多插画师首选的绘画工具,这款软件兼容几乎所有型号的绘画板,这款软件兼容几乎所有型号的绘画板,通...

  • 自学画画第5天

    就当做稍微休息一下吧,最近考虑买绘画板。

  • 2019-04-20

    哥哥给我买了一个新玩具,这个玩具是充上电才能用的。这个玩具叫绘画板,有电了在绘画板可以画很多小动物。我可以画好多的...

  • 入账绘画板

    因为是写日记的,所以排版晒出来,内容就没写!这是我花了一天绘画出来的,是人物与植物间隔,圆框用蓝描,里面用土色,代...

  • 资源网址

    插画设计神器 Krita 免费 Corel Painter 专业ps兼容 SAI 兼容所有绘画板 systemax...

  • 在 iOS 中使用 OpenGL ES 实现绘画板

    今天我们使用 OpenGL ES 来实现一个绘画板,主要介绍在 OpenGL ES 中绘制平滑曲线的实现方案。 首...

  • 板绘

    这个是原图,我先用彩铅画了一遍。然后用绘画板画了一遍加深印象。

网友评论

本文标题:iOS绘画板

本文链接:https://www.haomeiwen.com/subject/gglehttx.html