//
//  UISixRangeView.swift
//  SixRangeDemo
//
//  Created by 徐风 on 16/7/25.
//  Copyright © 2016年 徐风. All rights reserved.
//

import Foundation
import UIKit

class UISixRangeView: UIView {
    
    var screenWidth:CGFloat
    var screenHeight:CGFloat
    var viewWidth:CGFloat
    var viewHeight:CGFloat
    var rangeLength:CGFloat
    var a,b,c:CGFloat
    let offset=5
    let PI=3.1415926;
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        var screenRect:CGRect=UIScreen.mainScreen().bounds
        screenWidth=screenRect.size.width
        screenHeight=screenRect.size.height
        
        var radian30:Double=30*M_PI/180;
        viewWidth=((screenWidth-(CGFloat)(3*offset))/3)
        
        rangeLength=(viewWidth/2)*(CGFloat)(cos(radian30))
        
        a=viewWidth/2
        b=a*(CGFloat)(tan(radian30))
        c=2*b
        
        viewHeight=2*c
        
        super.init(frame: frame)
        
        self.frame=CGRect(x: 0, y: 0, width: viewWidth+10, height: viewHeight+10)
        self.backgroundColor=UIColor.clearColor()
    }
    
    override func drawRect(rect: CGRect) {
        //得到画笔
        let cgRef:CGContextRef=UIGraphicsGetCurrentContext()!
        //填充色 灰度 0.0 透明度 0.25
        CGContextSetGrayFillColor(cgRef, 0.0, 0.25)
        CGContextSetRGBFillColor(cgRef, 0, 0, 2, 0.8)
        
        //开始路径
        CGContextBeginPath(cgRef)
        //移动到各个角
        CGContextMoveToPoint(cgRef, viewWidth/2, 0)
        CGContextAddLineToPoint(cgRef, 0, b)
        CGContextAddLineToPoint(cgRef, 0, b+c)
        CGContextAddLineToPoint(cgRef, a, 2*c)
        CGContextAddLineToPoint(cgRef, 2*a, b+c)
        CGContextAddLineToPoint(cgRef, 2*a, b)
        //关闭路劲
        CGContextClosePath(cgRef)
        
        // 3.绘制图形
        CGContextFillPath(cgRef)
    }
    
    
}

 

posted on 2016-07-26 20:35  青年程序猿  阅读(191)  评论(0)    收藏  举报