Skip to content

Instantly share code, notes, and snippets.

@C4Examples
Created July 25, 2012 20:26
Show Gist options
  • Select an option

  • Save C4Examples/3178451 to your computer and use it in GitHub Desktop.

Select an option

Save C4Examples/3178451 to your computer and use it in GitHub Desktop.
Advanced lineDashPattern
//
// C4WorkSpace.m
// Examples
//
// Created by Travis Kirton on 12-07-19.
//
#import "C4WorkSpace.h"
@interface C4WorkSpace ()
-(void)createLines;
@end
@implementation C4WorkSpace {
CGPoint endPoints[2];
C4Shape *line1, *line2, *line3, *line4, *line5;
}
-(void)setup {
//create all the lines
[self createLines];
//create a 1-point pattern
CGFloat pattern1[1] = {5.0f};
[line1 setDashPattern:pattern1 pointCount:1];
//create a 2-point pattern
CGFloat pattern2[2] = {5.0f, 10.0f};
[line2 setDashPattern:pattern2 pointCount:2];
//create a random pattern (at least 20 points long, up to 40 points)
NSInteger randomCount = 20+[C4Math randomInt:20];
CGFloat pattern3[randomCount];
for(int i = 0; i < randomCount; i++) {
pattern3[i] = [C4Math randomInt:10]+1;
}
[line3 setDashPattern:pattern3 pointCount:randomCount];
/*
The following 2 examples demonstrate how to set the lineDashPattern property with an NSArray
*/
//define 3 widths
NSInteger dot = 5;
NSInteger dash = 15;
NSInteger space = 5;
//create a pattern array using Number objects
//this is actually the letters A, B, C in morse code
NSArray *patternArray = [NSArray arrayWithObjects:
[NSNumber numberWithInt:dot],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dash],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dash],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dot],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dot],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dot],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dash],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dot],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dash],
[NSNumber numberWithInt:space],
[NSNumber numberWithInt:dot],
[NSNumber numberWithInt:space],
nil];
line4.lineDashPattern = patternArray;
//create an array of wide points...
//notice that the initial width is 1?
//this is because the lineCap is round
//so the points need to be small to account for lineCap style
NSArray *widePatternArray = [NSArray arrayWithObjects:
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:40],nil];
line5.lineDashPattern = widePatternArray;
line5.lineCap = CAPROUND;
}
-(void)createLines {
//create 2 end points
endPoints[0] = CGPointZero;
endPoints[0] = CGPointMake(self.canvas.width*0.9f,0);
//create 5 lines
line1 = [C4Shape line:endPoints];
line2 = [C4Shape line:endPoints];
line3 = [C4Shape line:endPoints];
line4 = [C4Shape line:endPoints];
line5 = [C4Shape line:endPoints];
//center all the lines
CGPoint center = self.canvas.center;
center.y = self.canvas.height/6;
line1.center = center;
center.y += self.canvas.height/6;
line2.center = center;
center.y += self.canvas.height/6;
line3.center = center;
center.y += self.canvas.height/6;
line4.center = center;
center.y += self.canvas.height/6;
line5.center = center;
//set their line widths
line1.lineWidth = 30.0f;
line2.lineWidth = 30.0f;
line3.lineWidth = 30.0f;
line4.lineWidth = 30.0f;
line5.lineWidth = 30.0f;
//add them to the canvas
[self.canvas addShape:line1];
[self.canvas addShape:line2];
[self.canvas addShape:line3];
[self.canvas addShape:line4];
[self.canvas addShape:line5];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment