UIView简单用法

来源:互联网 发布:关于太空的软件 编辑:程序博客网 时间:2024/05/10 01:26
////  MyView.swift//  L01Test////  Created by robertkun on 15/9/7.//  Copyright (c) 2015 MAC. All rights reserved.//import UIKitclass MyView: UIView {    private var nValue:CGFloat = 0;    private var path = CGPathCreateMutable()        override init(frame: CGRect) {        super.init(frame: frame)    }    required init(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)        fatalError("init(coder:) has not been implemented")    }        func setProgessValue(value:CGFloat) {        nValue = value        setNeedsDisplay()    }        func getProgessValue()->CGFloat {        return nValue;    }        override func drawRect(rect: CGRect) {        // Drawing code                var context = UIGraphicsGetCurrentContext()        CGContextSetRGBFillColor(context, 1, 1, 1, 1)        CGContextSetLineWidth(context, 5)        CGContextAddPath(context, path)        CGContextFillPath(context)                var r = rect.width/2        UIColor.blackColor().set()        CGContextAddArc(context, r, r, r, 0, 3.14*2*r, 0)        CGContextFillPath(context)                        UIColor.redColor().set()        CGContextAddArc(context, r, r, r, 0, 3.14*2*nValue, 0)        CGContextAddLineToPoint(context, r, r)        CGContextFillPath(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()    }}


0 0
原创粉丝点击