分类 quartz 2d 下的文章

Quartz2d基本使用

基本使用,主要是画下线矩形,圆形之类的
//
//  RectangleView.swift
//  画图
//
//  Created by admin on 16/1/23.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class RectangleView: UIView {
    override func drawRect(rect: CGRect) {
        self.drawRectImage()
    }
    // 图片
    func drawRectImage()
    {
        let image = UIImage(imageLiteral: "1")
        // 超过部分就没了
//        image.drawAtPoint(CGPoint.zero)
        // 图片会缩放
//        image.drawInRect(CGRectMake(0, 0, 100, 100))
        // 不够的部分会重复
        image.drawAsPatternInRect(CGRectMake(0, 0, self.frame.size.width, self.frame.size.height))
    }
    // 画文字,换文字的时候需要注意文字超过视图的边界的话就不回显示
    func drawRectText()
    {
        let str:NSString = "中蒙边境最低气温-35℃受西伯利亚冷空气的影响,地处新疆中蒙边境的新疆生产建设兵团第十师一八三团近期温度大幅下降8至10℃,最低气温降至-35℃。在当地的一户牧民家的羊圈中,笔者无意发现一只猫咪因为受不了寒冷的天气,依偎在两只小绵羊身上取暖,而且关系相处得特别融洽。"
        // 这个方式画的字不回换行,超过部分不回显示
//        str.drawAtPoint(CGPoint.zero, withAttributes: nil)
        // 这个方法会换行,但是超过设置的rect大小,超过的话也不回显示,可以通过计算字体的方式设置rect
//        str.drawInRect(CGRectMake(0, 0, self.frame.size.width, 50), withAttributes: nil)
        let attr:[String : AnyObject] = [NSFontAttributeName:UIFont.systemFontOfSize(15),NSForegroundColorAttributeName:UIColor.whiteColor()]
        let strSize = str.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attr, context: nil).size
        str.drawInRect(CGRectMake(0, 0, self.frame.size.width, strSize.height), withAttributes: attr)
    }
    // 画圆,注意画圆和画弧用的方法一致但是化弧需要用到额外的一个点和CGContextClosePath
    func drawRectSector()
    {
        let sector = UIGraphicsGetCurrentContext()
        let startPoint = CGPointMake(100,100)
        CGContextMoveToPoint(sector, startPoint.x, startPoint.y)
        CGContextAddArc(sector,startPoint.x, startPoint.y, 50, CGFloat(M_PI * 0.3), CGFloat(M_PI * 1.5), 0)
        CGContextClosePath(sector)
        CGContextStrokePath(sector)
    }
    // 弧度
    func drawRectArc()
    {
        let arc = UIGraphicsGetCurrentContext()
        // 参数含义分别为 圆心点,园半径,弧的开始角度,弧的结束角度,是否顺时针
        CGContextAddArc(arc, 100, 100, 50, 0, CGFloat(M_PI), 0)
//        CGContextStrokePath(arc)
        CGContextFillPath(arc)
    }
    // 花园
    func drawRectCircle()
    {
        let circle = UIGraphicsGetCurrentContext()
        CGContextAddEllipseInRect(circle, CGRectMake(100, 100, 50, 50))
        CGContextStrokePath(circle)
    }
    // 注意闭合路径(CGContextClosePath)的使用
    func drawRectTriangle()
    {
        let triangle = UIGraphicsGetCurrentContext()
        let startPoint = CGPoint(x: 10, y: 10)
        let triangleW:CGFloat = 100
        let triangleH:CGFloat = 100
        CGContextMoveToPoint(triangle, startPoint.x, startPoint.y)
        CGContextAddLineToPoint(triangle, startPoint.x + triangleW, startPoint.y)
        CGContextAddLineToPoint(triangle, 0, startPoint.x + triangleH)
        // 关闭路径
        CGContextClosePath(triangle)
//        CGContextFillPath(triangle)
        CGContextStrokePath(triangle)
    }
    // 注意使用CGContextStrokePath和CGContextFillPath渲染方式的不同
    func drawRectRectengle() {
        let rectangle = UIGraphicsGetCurrentContext()
        // 设置矩形的长宽和左上角坐标信息
        let rectangleW:CGFloat = 100
        let rectangleH:CGFloat = 200
        let startPointX:CGFloat = 10
        let startPointY:CGFloat = 10
        // *** 第一种方式:逐个点连接 ***
        // 设置起点
        //        CGContextMoveToPoint(rectangle, startPointX, startPointY)
        //        // 设置第二个点
        //        CGContextAddLineToPoint(rectangle, startPointX + rectangleW, startPointY)
        //        // 第三个点
        //        CGContextAddLineToPoint(rectangle, startPointX + rectangleW, startPointY + rectangleH)
        //        // 第四个点
        //        CGContextAddLineToPoint(rectangle, startPointX, startPointY + rectangleH)
        //        // 回到原点
        //        CGContextAddLineToPoint(rectangle, startPointX, startPointY)
        // *** 第二种方式:调用写好的函数 ***
        CGContextAddRect(rectangle, CGRectMake(startPointX, startPointY, rectangleW, rectangleH))
        // 不填充渲染
        //        CGContextStrokePath(rectangle)
        // 填充渲染
        CGContextFillPath(rectangle)
    }
    func drawRectLine()
    {
        // 获得实体上下文
        let line = UIGraphicsGetCurrentContext()

        // 设置线的颜色
        CGContextSetRGBStrokeColor(line, 1.0, 0.0, 0.0, 1)
        // 设置线的宽度
        CGContextSetLineWidth(line, 10)
        // 设置线的头的样式
        CGContextSetLineCap(line, CGLineCap.Round)
        // 设置线的连接点的样式
        CGContextSetLineJoin(line, CGLineJoin.Bevel)
        // 设置起点
        CGContextMoveToPoint(line, 30 , 30)
        // 设置第二个点
        CGContextAddLineToPoint(line, 100, 200)
        // 设置第三个点
        CGContextAddLineToPoint(line, 30, 300)
        // 渲染
        CGContextStrokePath(line)
    }
}