swift简易画板

控制器类
//
//  ViewController.swift
//  简易画板
//
//  Created by admin on 16/1/25.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var drawingBoard: DrawingBoard!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func back(sender: AnyObject) {
        self.drawingBoard.back()
    }
    @IBAction func clear(sender: AnyObject) {
        self.drawingBoard.clear()
    }
    @IBAction func save(sender: AnyObject) {
        self.drawingBoard.save()
    }
    @IBAction func selectColor(sender: UIButton) {
        self.drawingBoard.selectColor(sender.backgroundColor!)
    }
}
画板类
//
//  DrawingBoard.swift
//  简易画板
//
//  Created by admin on 16/1/25.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit
// 线结构体
struct Line {
    let color:UIColor
    var points:[CGPoint] = []
}
// 画板类,使用上下文方式画
class DrawingBoard: UIView {
    // 线的集合
    var lines:[Line] = []
    // 当前选择的颜色
    var lineColor:UIColor = UIColor.blackColor()
    // 开始触摸方法,在开始触摸的时候新初始化一个线结构体
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let tempLine = Line(color: self.lineColor, points: [])
        lines.append(tempLine)
    }
    // 移动的时候给当前的线结构体添加点
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = (touches as NSSet).anyObject()
        // 添加当前线的结构体到线集合数组
        lines[self.lines.count - 1].points.append((touch?.locationInView(touch?.view))!)
        self.setNeedsDisplay()
    }
    // 画
    override func drawRect(rect: CGRect) {
        // 先获取上下文,在添加点
        let context = UIGraphicsGetCurrentContext()
        // 设置线的样式
        CGContextSetLineCap(context, CGLineCap.Round)
        CGContextSetLineJoin(context, CGLineJoin.Round)
        CGContextSetLineWidth(context, 10)
        for var j = 0;j < self.lines.count;j++
        {
            self.lines[j].color.set()
            for var i = 0;i < self.lines[j].points.count;i++
            {
                let point:CGPoint = (self.lines[j].points[i])
                if i == 0
                {
                    CGContextMoveToPoint(context, point.x, point.y)
                }
                else
                {
                    CGContextAddLineToPoint(context, point.x, point.y)
                }
            }
            CGContextStrokePath(context)
        }
    }
    // 回退
    func back()
    {
        if self.lines.count > 0
        {
            self.lines.removeLast()
            self.setNeedsDisplay()
        }
    }
    // 清屏
    func clear()
    {
        self.lines = []
        self.setNeedsDisplay()
    }
    // 保存颜色
    func save()
    {
        // 开始位图上下文
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
        // 画图
        self.setNeedsDisplay()
        // 获取当前图片
        let context = UIGraphicsGetCurrentContext()
        self.layer.renderInContext(context!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 转换成 NSData 数据
        let resImage = UIImagePNGRepresentation(image)
        // 保存
        resImage?.writeToFile("/Users/admin/Desktop/DrawingBoard.png", atomically: true)
    }
    // 
    func selectColor(color:UIColor)
    {
        self.lineColor = color
    }
}
画板类,UIBezierPath方式画
//
//  DrawBoardBezierPath.swift
//  简易画板
//
//  Created by admin on 16/1/25.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit
// 线结构体,为了和之前的区分,这里的名字有点长
struct LineBezierPath {
    let color:UIColor
    var points:UIBezierPath
}
// 画板类,这个使用BezierPath画
class DrawBoardBezierPath: UIView {
    var lines:[LineBezierPath] = []
    // 当前选择的颜色
    var lineColor:UIColor = UIColor.blackColor()
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = (touches as NSSet).anyObject()
        // 初始化 UIBezierPath 对象
        let bezierPath = UIBezierPath()
        // 设置线的开始点
        bezierPath.moveToPoint((touch?.locationInView(touch?.view))!)
        // 初始化线的结构体
        let line = LineBezierPath(color: self.lineColor, points: bezierPath)
        // 添加线到线集合
        self.lines.append(line)
    }
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = (touches as NSSet).anyObject()
        // 给当前的线添加点
        self.lines.last?.points.addLineToPoint((touch?.locationInView(touch?.view))!)
        self.setNeedsDisplay()
    }
    override func drawRect(rect: CGRect) {
        // 画
        for var i = 0;i < self.lines.count;i++
        {
            self.lines[i].points.lineCapStyle = CGLineCap.Round
            self.lines[i].points.lineJoinStyle = CGLineJoin.Round
            self.lines[i].points.lineWidth = 10
            self.lines[i].color.set()
            self.lines[i].points.stroke()
        }
    }
    // 回退
    func back()
    {
        if self.lines.count > 0
        {
            self.lines.removeLast()
            self.setNeedsDisplay()
        }
    }
    // 清屏
    func clear()
    {
        self.lines = []
        self.setNeedsDisplay()
    }
    // 保存颜色
    func save()
    {
        // 开始位图上下文
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
        // 画图
        self.setNeedsDisplay()
        // 获取当前图片
        let context = UIGraphicsGetCurrentContext()
        self.layer.renderInContext(context!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 转换成 NSData 数据
        let resImage = UIImagePNGRepresentation(image)
        // 保存
        resImage?.writeToFile("/Users/admin/Desktop/DrawingBoard.png", atomically: true)
    }
    //
    func selectColor(color:UIColor)
    {
        self.lineColor = color
    }
}

标签: swift, quartz 2d, 触摸事件

添加新评论