-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacheView.m
237 lines (196 loc) · 8.68 KB
/
StacheView.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// StacheView.m
// Stached
//
// Created by Kaitlyn McDonald on 12-10-18.
// Copyright (c) 2012 Kaitlyn McDonald. All rights reserved.
//
#import "StacheView.h"
@implementation StacheView
@synthesize pickerRef, touchView, startPoint, moustacheFrame, pix, curStache, scrollView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.opaque = YES;
self.backgroundColor = [UIColor clearColor];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat camHeight = (screenRect.size.height == 568) ? screenRect.size.height - 61 : screenRect.size.height;
pix = [[NSMutableArray alloc] init];
[pix addObject:[UIImage imageNamed:@"1.png"]];
[pix addObject:[UIImage imageNamed:@"2.png"]];
[pix addObject:[UIImage imageNamed:@"3.png"]];
[pix addObject:[UIImage imageNamed:@"4.png"]];
[pix addObject:[UIImage imageNamed:@"5.png"]];
[pix addObject:[UIImage imageNamed:@"6.png"]];
[pix addObject:[UIImage imageNamed:@"7.png"]];
[pix addObject:[UIImage imageNamed:@"8.png"]];
[pix addObject:[UIImage imageNamed:@"9.png"]];
[pix addObject:[UIImage imageNamed:@"10.png"]];
[pix addObject:[UIImage imageNamed:@"11.png"]];
[pix addObject:[UIImage imageNamed:@"12.png"]];
curStache = 0;
UIImage *moustache = [pix objectAtIndex:curStache];
moustacheFrame = [[[UIImageView alloc] initWithImage:moustache] autorelease];
moustacheFrame.contentMode = UIViewContentModeScaleAspectFit;
moustacheFrame.center = CGPointMake((screenWidth/2), (camHeight/2));
[self addSubview:moustacheFrame];
/*UIImageView *border = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_bar_.png"]] autorelease];
[self addSubview:border];*/
UIImageView *tabBar = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button_bg_org_.jpg"]] autorelease];
tabBar.frame = CGRectMake(0, (screenHeight-49), screenWidth, 49);
[self addSubview:tabBar];
UIImageView *delete = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"close_.png"]] autorelease];
delete.frame = CGRectMake(65, (screenHeight-40), delete.bounds.size.width, delete.bounds.size.height);
[self addSubview:delete];
UIButton *buttonDelete = [UIButton buttonWithType:UIButtonTypeCustom];
buttonDelete.frame = CGRectMake(55, (screenHeight-49), 40, 49);
[buttonDelete addTarget:self action:@selector(deleted) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:buttonDelete];
UIImageView *keep = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accept_.png"]] autorelease];
keep.frame = CGRectMake(225, (screenHeight-40), keep.bounds.size.width, keep.bounds.size.height);
[self addSubview:keep];
UIButton *buttonKeep = [UIButton buttonWithType:UIButtonTypeCustom];
buttonKeep.frame = CGRectMake(215, (screenHeight-49), 40, 49);
[buttonKeep addTarget:self action:@selector(keeped) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:buttonKeep];
[self browseStaches];
touchView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, screenWidth, (screenHeight-120))];
touchView.userInteractionEnabled = YES;
[self addSubview:touchView];
}
return self;
}
- (void) deleted
{
pickerRef.showPhoto = NO;
[pickerRef displayScreen];
}
- (void) keeped
{
[pickerRef saveWithStache:pix :curStache];
}
- (void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [touches anyObject];
startPoint = [touch locationInView:touchView];
}
- (void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:touchView];
CGFloat deltaX = fabsf(startPoint.x - currentPosition.x);
if(deltaX > 30)
{
if(currentPosition.x < startPoint.x)
{
[self styleThatStache:1 :NO];
[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self addStaches:curStache];
}
else if(currentPosition.x > startPoint.x)
{
[self styleThatStache:0 :NO];
[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self addStaches:curStache];
}
}
}
- (void) styleThatStache: (NSInteger)der :(BOOL)specify
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat camHeight = (screenRect.size.height == 568) ? screenRect.size.height - 61 : screenRect.size.height;
if(specify)
{
curStache = der;
}
else
{
if(der == 0)
curStache = curStache - 1;
else
curStache = curStache + 1;
}
if(curStache < 0)
curStache = ([pix count] - 1);
else if(curStache > ([pix count] - 1))
curStache = 0;
moustacheFrame.image = nil;
UIImage *moustache = [pix objectAtIndex:curStache];
moustacheFrame = [[UIImageView alloc] initWithImage:moustache];
moustacheFrame.center = CGPointMake((screenWidth/2), (camHeight/2));
[self addSubview:moustacheFrame];
[self bringSubviewToFront:moustacheFrame];
}
- (void) browseStaches
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGRect frame = CGRectMake(0, (screenHeight-108), screenWidth, 60);
scrollView = [[[UIScrollView alloc] initWithFrame:frame] autorelease];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor colorWithRed:76/255.0 green:62/255.0 blue:26/255.0 alpha:1.0];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addStaches:0];
[self addSubview:scrollView];
}
- (void) addStaches: (int) index;
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGSize elementSize = CGSizeMake(80, 60);
NSMutableArray *icons = [[NSMutableArray alloc] init];
[icons addObject:[UIImage imageNamed:@"0_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"1_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"2_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"3_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"4_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"5_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"6_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"7_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"8_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"9_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"10_icon_off.png"]];
[icons addObject:[UIImage imageNamed:@"11_icon_off.png"]];
for(int i = 0; i < icons.count; i++)
{
UIView* subview = [[UIView new] autorelease];
[subview setUserInteractionEnabled:YES];
UITapGestureRecognizer *selected = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector (changeImg: )];
[subview addGestureRecognizer: selected];
subview.backgroundColor = [UIColor colorWithPatternImage:[icons objectAtIndex:i]];
subview.tag = i;
if(i == index)
subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_icon_on.png", index]]];
int elementX = elementSize.width * i;
subview.frame = CGRectMake(elementX, 0, elementSize.width, elementSize.height);
[scrollView addSubview:subview];
}
scrollView.contentSize = CGSizeMake((80*pix.count), scrollView.frame.size.height);
[scrollView scrollRectToVisible:CGRectMake(screenWidth*(index/4), 0, screenWidth , (screenHeight/2)) animated:YES];
}
- (void)changeImg: (id)sender
{
UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
UIImageView *imageView = (UIImageView *) recognizer.view;
NSInteger index = [imageView tag];
[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self addStaches:index];
[self styleThatStache:index :YES];
}
- (void) dealloc
{
[pickerRef release];
[touchView release];
[moustacheFrame release];
[pix release];
[scrollView release];
[super dealloc];
}
@end