zoukankan      html  css  js  c++  java
  • swift下的简单的绘图实现

    import UIKit
    
    class MyView: UIView {
    
        /*
        // Only override drawRect: if you perform custom drawing.
        // An empty implementation adversely affects performance during animation.
        override func drawRect(rect: CGRect) {
            // Drawing code
        }
        */
        
        var uiImage:CGImageRef? = UIImage(named: "004.jpg")?.CGImage
    
        //----简易画板-----
        var path = CGPathCreateMutable()
        
        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
            var p = touches.anyObject()?.locationInView(self)
            CGPathMoveToPoint(path, nil, p!.x, p!.y)
            
        }
        
        override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
            var p = touches.anyObject()?.locationInView(self)
            CGPathAddLineToPoint(path, nil, p!.x, p!.y)
            
            //执行重绘的操作
            setNeedsDisplay()
        }
        
    
        override func drawRect(rect: CGRect) {
            var context = UIGraphicsGetCurrentContext()
            
    //        CGContextSetRGBStrokeColor(context, 1, 0, 1, 1)//设置线的颜色
    //        CGContextSetLineWidth(context, 5)//设置线的宽度
    //        CGContextStrokePath(context)
    //        
    //        /*----fillpath为填充StrokePath为画线----*/
    //        
    //        //画线
    //        CGContextMoveToPoint(context, 100, 100)
    //        CGContextAddLineToPoint(context, 100, 200)
    //        CGContextAddLineToPoint(context, 200, 200)
    //        CGContextStrokePath(context)
    //        
    //        CGContextMoveToPoint(context, 100, 300)
    //        CGContextAddLineToPoint(context, 100, 400)
    //        CGContextAddLineToPoint(context, 200, 500)
    //        CGContextStrokePath(context)
    //        
    //        //画方块
    //        CGContextAddRect(context, CGRect(x: 200, y: 100,  100, height: 100))
    //        CGContextSetRGBFillColor(context, 1, 0, 0, 1)//改变方块的颜色
    //        /*--先把方块添加进去然后加边框,否则只显示边框--*/
    //        CGContextFillPath(context)
    //        CGContextStrokeRect(context, CGRect(x: 200, y: 100,  100, height:100))
    //        
    //        //画圆---弧线
    //        CGContextAddArc(context, 220, 350, 100, 0, 3.14*2, 0)
    //        CGContextSetRGBFillColor(context, 1, 0, 0, 1)
    //        CGContextFillPath(context)
    //        
    //        CGContextAddArc(context, 220, 350, 100, 0, 3.14*2, 0)//最后的0为顺时针,1为逆时针
    //        CGContextStrokePath(context)
    //        //---椭圆---宽高相等为圆形,宽高不等为椭圆
    //        CGContextAddEllipseInRect(context, CGRect(x: 50, y: 450,  200, height: 200))
    //        CGContextStrokePath(context)
    //        
    //        //------画图片
    //        //保存context------如果不保存与恢复图形会影响到后续的画图
    //        CGContextSaveGState(context)
    //        //画布的调整
    //        CGContextTranslateCTM(context, 10, 400)
    //        CGContextScaleCTM(context, 1, -1)
    //        CGContextDrawImage(context, CGRect(x: 100, y: 100,  200, height: 200), uiImage)
    //        //恢复context
    //        CGContextRestoreGState(context)
            
            
            //------简易画板
            //画板上的简单画线
    //        var path = CGPathCreateMutable()
    //        CGPathMoveToPoint(path, nil, 100, 100)
    //        CGPathAddLineToPoint(path, nil, 200, 200)
            CGContextAddPath(context, path)
            CGContextStrokePath(context)
        }
    
    }
    
  • 相关阅读:
    Scikit-learn学习记录【持续更新】——Scikit-learn框架的入门介绍
    【步骤超详细】mac系统 通过anaconda配置Pycharm的scikit-learn环境
    与高哥聊天有感
    蓝杰学长学姐交流记录
    Robcup2D足球学习记录【2020.01.06】
    分治算法
    多所学校就业报告分析总结
    想要半年之内拿到30万大厂的offer?看这里!!!
    C语言变量的存储类别
    C语言函数入门
  • 原文地址:https://www.cnblogs.com/anyezhuixing/p/4369636.html
Copyright © 2011-2022 走看看