您的位置:首页 > 移动开发 > IOS开发

ios8绘图方法小记touchesBegan&touchesMoved

2015-09-07 22:46 411 查看
//
//  MyView.swift
//  L01Test
//
//  Created by robertkun on 15/9/7.
//  Copyright (c) 2015 MAC. All rights reserved.
//

import UIKit

class MyView: UIView {

    var path = CGPathCreateMutable()

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        UIColor.redColor().set()

        var context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 5)
        CGContextAddPath(context, path)
        CGContextStrokePath(context)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let p = (touches as NSSet).anyObject()?.locationInView(self)

        CGPathMoveToPoint(path, nil, p!.x, p!.y)

    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        let p = (touches as NSSet).anyObject()?.locationInView(self)

        CGPathAddLineToPoint(path, nil, p!.x, p!.y)

        setNeedsDisplay()
    }

    func drawTwoLines(point:CGPoint, lineJoin:CGLineJoin) {
        let context = UIGraphicsGetCurrentContext()

        CGContextSetLineJoin(context, lineJoin)

        CGContextSetLineWidth(context, 5)

        CGContextMoveToPoint(context, point.x-100, point.y+50)

        CGContextAddLineToPoint(context, point.x, point.y)

        CGContextAddLineToPoint(context, point.x+100, point.y+50)

        CGContextStrokePath(context)
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: