This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEGEdgyView.m
171 lines (134 loc) · 5.25 KB
/
EGEdgyView.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
//
// EGEdgyView.m
// ImageProcessing
//
// Created by Chris Marcellino on 8/26/10.
// Copyright 2010 EGEdgyView. All rights reserved.
//
#import "EGEdgyView.h"
static const CGFloat buttonAlpha = 0.8;
@interface EGEdgyView ()
- (void)setControlAlpha:(CGFloat)alpha;
@end
@implementation EGEdgyView
@synthesize imageView, torchButton, captureButton, cameraToggle, colorToggle, cannyThresholdSlider, bannerView;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
imageView = [[UIImageView alloc] init];
[imageView setBackgroundColor:[UIColor blackColor]];
[imageView setContentScaleFactor:1.0];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[[imageView layer] setMagnificationFilter:@"nearest"];
[self addSubview:imageView];
torchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[torchButton setImage:[UIImage imageNamed:@"lightbulb_small.png"] forState:UIControlStateNormal];
[torchButton setImage:[UIImage imageNamed:@"lightbulb_small_selected.png"] forState:UIControlStateSelected];
[torchButton sizeToFit];
[self addSubview:torchButton];
captureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[captureButton setImage:[UIImage imageNamed:@"camera_dslr_small.png"] forState:UIControlStateNormal];
[captureButton sizeToFit];
[self addSubview:captureButton];
cameraToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cameraToggle setImage:[UIImage imageNamed:@"reload_small.png"] forState:UIControlStateNormal];
[cameraToggle sizeToFit];
[self addSubview:cameraToggle];
colorToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[colorToggle setImage:[UIImage imageNamed:@"colors_small.png"] forState:UIControlStateNormal];
[colorToggle sizeToFit];
[self addSubview:colorToggle];
cannyThresholdSlider = [[UISlider alloc] init];
[cannyThresholdSlider setMinimumValue:5.0];
[cannyThresholdSlider setMaximumValue:300.0];
[self addSubview:cannyThresholdSlider];
[self setControlAlpha:0.0];
bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
[bannerView setHidden:YES];
[self addSubview:bannerView];
}
return self;
}
- (void)layoutSubviews
{
CGRect bounds = [self bounds];
[imageView setFrame:bounds];
CGFloat yOffset = (bannerView && ![bannerView isHidden]) ? [bannerView frame].size.height : 0.0;
CGRect buttonFrame;
buttonFrame.size = CGSizeMake(40.0, 40.0);
buttonFrame.origin = CGPointMake(8.0, 8.0);
[torchButton setFrame:buttonFrame];
buttonFrame.origin = CGPointMake(8.0, bounds.size.height - buttonFrame.size.height - 8.0 - yOffset);
[captureButton setFrame:buttonFrame];
buttonFrame.origin = CGPointMake(bounds.size.width - buttonFrame.size.width - 8.0, 8.0);
[colorToggle setFrame:buttonFrame];
buttonFrame.origin = CGPointMake(CGRectGetMinX([colorToggle frame]) - buttonFrame.size.width - 8.0, 8.0);
[cameraToggle setFrame:buttonFrame];
// Position the slider at the bottom center
CGRect sliderFrame = CGRectMake(bounds.size.width / 4, bounds.size.height - 35.0 - yOffset, bounds.size.width / 2, 30.0);
[cannyThresholdSlider setFrame:sliderFrame];
CGRect bannerFrame = [bannerView frame];
bannerFrame.origin = CGPointMake(0.0, bounds.size.height - bannerFrame.size.height);
[bannerView setFrame:bannerFrame];
}
- (void)setControlAlpha:(CGFloat)alpha
{
[torchButton setAlpha:alpha];
[captureButton setAlpha:alpha];
[cameraToggle setAlpha:alpha];
[colorToggle setAlpha:alpha];
[cannyThresholdSlider setAlpha:alpha];
}
- (void)setButtonImageTransform:(CGAffineTransform)transform animated:(BOOL)animated
{
if (animated) {
[UIView beginAnimations:nil context:NULL];
}
[torchButton setTransform:transform];
[captureButton setTransform:transform];
[cameraToggle setTransform:transform];
[colorToggle setTransform:transform];
if (animated) {
[UIView commitAnimations];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self clearFadeTimer];
CGFloat oldAlpha = [[self torchButton] alpha];
if (oldAlpha == 0.0) {
[self setControlAlpha:buttonAlpha];
} else {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self setControlAlpha:0.0];
[UIView commitAnimations];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self restartFadeTimer];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self restartFadeTimer];
}
- (void)clearFadeTimer
{
[fadeTimer invalidate];
fadeTimer = nil;
}
- (void)restartFadeTimer
{
[self clearFadeTimer];
fadeTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(fadeTimerFired) userInfo:nil repeats:NO];
}
- (void)fadeTimerFired
{
fadeTimer = nil;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self setControlAlpha:0.0];
[UIView commitAnimations];
}
@end