Created
August 12, 2015 01:08
-
-
Save victorchee/048af3232867fac3b6cb to your computer and use it in GitHub Desktop.
draw a sawtooth view
This file contains 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
- (void)drawRect:(CGRect)rect | |
{ | |
const CGFloat frequency = 25.0f; | |
const CGFloat amplitude = 5.0f; | |
const CGFloat baseLineHeight = 50.0f; // if base line height equal to amplitude, the sawtooth is on the edge of the view | |
// Drawing code | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSaveGState(context); | |
CGContextSetLineWidth(context, 2.0f); // set line width | |
CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor); // set stroke color | |
CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor); // set fill color | |
CGMutablePathRef path = CGPathCreateMutable(); | |
CGPathMoveToPoint(path, NULL, 0, 0); | |
for (float x = 0; x < self.frame.size.width; ++x) { | |
float y = sinf(frequency * x / 180*M_PI) * amplitude + baseLineHeight; | |
CGPathAddLineToPoint(path, NULL, x, y); | |
} | |
CGPathAddLineToPoint(path, NULL, rect.size.width, 0); | |
CGPathAddLineToPoint(path, NULL, 0, 0); | |
CGPathCloseSubpath(path); // close path | |
CGContextAddPath(context, path); | |
CGContextDrawPath(context, kCGPathFillStroke); | |
CGPathRelease(path); | |
CGContextRestoreGState(context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment