-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCustomView.m
176 lines (102 loc) · 3.27 KB
/
MyCustomView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// MyCustomView.m
// RedSquare
//
// Created by Steve Aquillano on 3/9/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MyCustomView.h"
#define kAccelerometerFrequency 10 //Hz
@implementation MyCustomView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void) awakeFromNib {
// You have to initialize your view here since it's getting
// instantiated by the nib
squareSize = 100.0f;
twoFingers = NO;
rotation = 0.5f;
// You have to explicitly turn on multitouch for the view
self.multipleTouchEnabled = YES;
// Configure for accelerometer
[self configureAccelerometer];
}
- (void)configureAccelerometer {
UIAccelerometer *theAccelerometer = [UIAccelerometer sharedAccelerometer];
if(theAccelerometer) {
theAccelerometer.delegate = self;
theAccelerometer.updateInterval = 1 / kAccelerometerFrequency;
}
else {
NSLog(@"Oops we're not running on the device!");
}
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
// Do something with the values.
xField.text = [NSString stringWithFormat:@"%.5f", x];
yField.text = [NSString stringWithFormat:@"%.5f", y];
zField.text = [NSString stringWithFormat:@"%.5f", z];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches began count %d, %@", [touches count], touches);
// Count returns the number of touches in the set.
if([touches count] > 1) {
twoFingers = YES;
}
// Tell the view to redraw
[self setNeedsDisplay];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches moved count %d, %@", [touches count], touches);
// tell the view to redraw
[self setNeedsDisplay];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches ended count %d, %@", [touches count], touches);
// reset the var
twoFingers = NO;
// tell the view to redraw
[self setNeedsDisplay];
}
// Draws the receiver's image within the passed-in rectangle
// rect is a rectangle defining the area to restrict drawing to
- (void)drawRect:(CGRect)rect {
// Drawing code
NSLog(@"drawRect");
CGFloat centerx = rect.size.width/2;
CGFloat centery = rect.size.height/2;
CGFloat half = squareSize/2;
CGRect theRect = CGRectMake(-half, -half, squareSize, squareSize);
// Grab the drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
// like Processing pushMatrix
CGContextSaveGState(context);
CGContextTranslateCTM(context, centerx, centery);
// Uncomment to see the rotated square
//CGContextRotateCTM(context, rotation);
// Set red stroke
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
// Set different based on multitouch
if (!twoFingers) {
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
} else {
CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1.0);
}
// Draw a rect with a red stroke
CGContextFillRect(context, theRect);
CGContextStrokeRect(context, theRect);
// like Processing popMatrix
CGContextRestoreGState(context);
}
- (void)dealloc {
[super dealloc];
}
@end