//
//  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:CGFloat
    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
        
        offset=screenWidth/15
        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, height: viewHeight)
        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, (CGFloat)(sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 4))
        CGContextAddLineToPoint(cgRef, (viewWidth / 2), 0)
        CGContextAddLineToPoint(cgRef, viewWidth - ((CGFloat)(sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 4))
        CGContextAddLineToPoint(cgRef, viewWidth - ((CGFloat)(sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 2) + (viewWidth / 4))
        CGContextAddLineToPoint(cgRef, (viewWidth / 2), viewWidth)
        CGContextAddLineToPoint(cgRef,(CGFloat)(sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 2) + (viewWidth / 4))
        
        //关闭路劲
        CGContextClosePath(cgRef)
        
        // 3.绘制图形
        CGContextFillPath(cgRef)
    }
    //移动监听
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        let touch : UITouch = touches.first!
        let currentP:CGPoint=touch.locationInView(self)
        let preP:CGPoint=touch.previousLocationInView(self)
        
        let offsetX=currentP.x-preP.x
        let offsetY=currentP.y-preP.y
        
        self.transform=CGAffineTransformTranslate(self.transform, offsetX, offsetY)
        
    }
    
    
}

 

posted on 2016-07-27 21:05  青年程序猿  阅读(235)  评论(0)    收藏  举报