Skip to content

Instantly share code, notes, and snippets.

@mchirico
Created August 29, 2014 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchirico/a7cb5c43e7cb4a4554c5 to your computer and use it in GitHub Desktop.
Save mchirico/a7cb5c43e7cb4a4554c5 to your computer and use it in GitHub Desktop.
Simple UIView to draw lines and squares
import Foundation
import UIKit
class MyUIV:UIView {
//ddrawRect:(CGRect)rect
override func drawRect(rect: CGRect) {
var ctx = UIGraphicsGetCurrentContext()
CGContextClearRect(ctx, rect);
let parentViewBounds = self.bounds
let parentViewWidth = CGRectGetWidth(parentViewBounds)
let parentViewHeight = CGRectGetHeight(parentViewBounds)
println ("w,h = \(parentViewWidth),\(parentViewHeight) ")
// CGContextClearRect(ctx, rect);
CGContextSetRGBFillColor(ctx, 0.0, 1.0, 0.0, 1); // green
CGContextFillRect(ctx, CGRectMake(0, 0, parentViewWidth, parentViewHeight));
// println("in drawRect, fractionalHeight = \(fractionalHeight)")
var h: CGFloat = 200 // fractionalHeight*parentViewHeight
// CGContextSetRGBFillColor(ctx, 1.0, 0.0, 0.0, 1); // red
CGContextFillRect(ctx, CGRectMake(0, 0, parentViewWidth, h))
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 0.0, 1); //yellow
CGContextFillRect(ctx, CGRectMake(0, 0, 200, 200))
println("in drawRect, h = \(h)")
// This works nice
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); // red
CGContextMoveToPoint(ctx, 200, 200);
CGContextAddLineToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, 50, 500);
CGContextAddCurveToPoint(ctx, 0.0, 100, 200, 3, 300, 233)
CGContextStrokePath(ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment