Created
October 8, 2015 09:11
-
-
Save sooop/6117e2f986cb3991ba24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| class ClockView: UIView { | |
| let loadDate: NSDate | |
| override init(frame: CGRect){ | |
| loadDate = NSDate() | |
| super.init(frame: frame) | |
| } | |
| override func drawRect(rect: CGRect) | |
| { | |
| let ctx: CGContextRef? = UIGraphicsGetCurrentContext() | |
| let rad = CGRectGetWidth(rect) / 3.5 | |
| let endAngle = CGFloat( 2 * M_PI ) | |
| // 원을 그린다. | |
| // (컨텍스트, 중심점좌표 x, y, 반지름, 시작각도, 끝각도, 1(?)) | |
| // 맨마지막 파라미터는 시계방향인지 여부 (Int32) | |
| CGContextAddArc(ctx, | |
| CGRectGetMidX(rect), | |
| CGRectGetMidY(rect), | |
| rad, | |
| 0, | |
| endAngle, | |
| 1); | |
| // 원에 칠할 색을 설정한다. | |
| CGContextSetFillColorWithColor(ctx, UIColor.grayColor().CGColor) | |
| // 선의 색은 stroke path 시에 입혀진다. 대신 붓 종류?를 지정한다. | |
| CGContextSetStrokeColorWithColor(ctx, UIColor.whiteColor().CGColor) | |
| CGContextSetLineWidth(ctx, 4.0) | |
| // 원을 그린다. | |
| CGContextDrawPath(ctx, kCGPathFillStroke); | |
| // 시계의 눈금을 그린다. | |
| for i in 1...60 { | |
| CGContextSaveGState(ctx) | |
| do{ | |
| // 중심점이동 - 기본적으로 origin을 기점으로 회전하기 때문에, 시계의 중심을 기준으로 회전하기 | |
| // 위해 중심점을 이동한다. | |
| CGContextTranslateCTM(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect)) | |
| // 회전 | |
| CGContextRotateCTM(ctx, degree2radian(CGFloat(i) * 6)) | |
| let offset = i % 5 == 0 ? rad - 15 : rad - 10 | |
| drawSecondMarker(ctx:ctx, x:offset, y:0, radius:rad, color:UIColor.whiteColor()) | |
| } | |
| CGContextRestoreGState(ctx) | |
| } | |
| } | |
| private func degree2radian(a: CGFloat) -> CGFloat | |
| { | |
| return CGFloat(M_PI) * a / 180 | |
| } | |
| func drawSecondMarker(ctx ctx: CGContextRef?, x: CGFloat, y: CGFloat, radius: CGFloat, color:UIColor) | |
| { | |
| let path = CGPathCreateMutable() | |
| CGPathMoveToPoint(path, nil, radius, 0) | |
| CGPathAddLineToPoint(path, nil, x, y) | |
| CGPathCloseSubPath(path) | |
| CGContextAddPath(ctx, path) | |
| CGContextSetLineWidth(ctx, 1.5) | |
| CGContextSetStrokeColorWithColor(ctx, color.CGColor) | |
| CGContextStrokePath(ctx) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment