-
Notifications
You must be signed in to change notification settings - Fork 7
/
PrompterView.m
202 lines (150 loc) · 4.49 KB
/
PrompterView.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// PrompterView.m
// GreatTeleprompter
//
// Created by Scott Means on 6/11/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "PrompterView.h"
@implementation PrompterView
@synthesize theSpeech, paused, currentTouches, speechOffset;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
return self;
}
- (void)awakeFromNib
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
scrollVelocity = (1/TICK_INTERVAL);
self.currentTouches = [[NSMutableSet alloc] initWithCapacity:5];
blackOnWhite = [[NSUserDefaults standardUserDefaults] boolForKey:BOW_KEY];
if (blackOnWhite) {
self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
}
}
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat fontColor = blackOnWhite ? 0.0 : 1.0;
CGContextSetRGBFillColor(c, fontColor, fontColor, fontColor, 1.0);
double o = self.touchGap > 0 ? 0 : speechOffset;
CGRect rc = CGRectMake(rect.origin.x, rect.origin.y - (o * (baseSize.width/self.bounds.size.width)), rect.size.width, rect.size.height + o);
[theSpeech drawInRect:rc withFont:self.currentFont];
}
- (void)setTheSpeech:(NSString *)newSpeech
{
[theSpeech release];
theSpeech = [newSpeech retain];
tickTimer = [NSTimer scheduledTimerWithTimeInterval:TICK_INTERVAL target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
speechOffset = 0.0;
[self recalcMetrics];
[self setNeedsDisplay];
}
- (void)recalcMetrics
{
baseSize = [theSpeech sizeWithFont:self.currentFont constrainedToSize:CGSizeMake(self.bounds.size.width, INFINITY)];
}
- (void)layoutSubviews
{
[self setNeedsDisplay];
}
- (void)timerTick:(NSTimer*)theTimer
{
if (round(scrollVelocity) != 0) {
speechOffset += scrollVelocity * TICK_INTERVAL;
if (speechOffset < 0) {
speechOffset = 0;
self.paused = true;
}
float maxOffset = MAX(0, baseSize.height - self.bounds.size.height);
if (speechOffset > maxOffset) {
speechOffset = maxOffset;
self.paused = true;
}
[self setNeedsDisplay];
}
if (paused) {
scrollVelocity *= .91;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[currentTouches unionSet:touches];
[self initTouchInfo];
NSLog(@"touchesBegan");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([currentTouches count] == 1) {
UITouch *t = [touches anyObject];
CGPoint newTouchPos = [t locationInView:self];
speechOffset -= newTouchPos.y - lastTouchPos.y;
CFTimeInterval now = CACurrentMediaTime();
scrollVelocity = (lastTouchPos.y - newTouchPos.y)/(now - lastTouchTime);
lastTouchPos = newTouchPos;
lastTouchTime = now;
[self setNeedsDisplay];
} if ([currentTouches count] == 2) {
NSLog(@"touchGap delta: %f", self.touchGap-baseTouchGap);
[self setNeedsDisplay];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([currentTouches count] == 2) {
[[NSUserDefaults standardUserDefaults] setFloat:self.currentFont.pointSize forKey:FONT_SIZE_KEY];
[self recalcMetrics];
}
[currentTouches minusSet:touches];
[self initTouchInfo];
NSLog(@"touchesEnded");
}
- (float)touchGap
{
if ([currentTouches count] == 2) {
UITouch *t1 = [[currentTouches allObjects] objectAtIndex:0];
CGPoint p1 = [t1 locationInView:self];
UITouch *t2 = [[currentTouches allObjects] objectAtIndex:1];
CGPoint p2 = [t2 locationInView:self];
float dx = p2.x - p1.x;
float dy = p2.y - p1.y;
return sqrt(dx*dx + dy*dy);
}
return 0;
}
- (UIFont *)currentFont
{
float fontSize = [[NSUserDefaults standardUserDefaults] floatForKey:FONT_SIZE_KEY];
if (!fontSize) {
fontSize = DEFAULT_FONT_SIZE;
}
if (self.touchGap) {
float newFontSize = fontSize + (self.touchGap-baseTouchGap)/2;
newFontSize = MIN(MAX_FONT_SIZE, MAX(newFontSize, MIN_FONT_SIZE));
return [UIFont systemFontOfSize:newFontSize];
} else {
return [UIFont systemFontOfSize:fontSize];
}
}
- (void)initTouchInfo
{
if ([currentTouches count] == 1) {
UITouch *t = [currentTouches anyObject];
lastTouchPos = [t locationInView:self];
lastTouchTime = CACurrentMediaTime();
} else if ([currentTouches count] == 2) {
baseTouchGap = self.touchGap;
}
}
- (void)setPaused:(_Bool)newPaused
{
scrollVelocity = newPaused ? 0 : (1/TICK_INTERVAL);
paused = newPaused;
}
- (void)dealloc {
[super dealloc];
}
@end