-
Notifications
You must be signed in to change notification settings - Fork 73
/
ZSeatSelector.m
162 lines (138 loc) · 5.5 KB
/
ZSeatSelector.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
//
// ZSeatSelector.m
//
//
// Created by Ricardo Zertuche on 7/30/15.
//
//
#import "ZSeatSelector.h"
@implementation ZSeatSelector
#pragma mark - Init and Configuration
- (void)setSeatSize:(CGSize)size{
seat_width = size.width;
seat_height = size.height;
}
- (void)setMap:(NSString*)map{
self.delegate = self;
zoomable_view = [[UIView alloc]init];
int initial_seat_x = 0;
int initial_seat_y = 0;
int final_width = 0;
for (int i = 0; i<map.length; i++) {
char seat_at_position = [map characterAtIndex:i];
if (seat_at_position == 'A') {
[self createSeatButtonWithPosition:initial_seat_x and:initial_seat_y isAvailable:TRUE isDisabled:FALSE];
initial_seat_x += 1;
} else if (seat_at_position == 'D') {
[self createSeatButtonWithPosition:initial_seat_x and:initial_seat_y isAvailable:TRUE isDisabled:TRUE];
initial_seat_x += 1;
} else if (seat_at_position == 'U') {
[self createSeatButtonWithPosition:initial_seat_x and:initial_seat_y isAvailable:FALSE isDisabled:FALSE];
initial_seat_x += 1;
} else if(seat_at_position=='_'){
initial_seat_x += 1;
} else {
if (initial_seat_x>final_width) {
final_width = initial_seat_x;
}
initial_seat_x = 0;
initial_seat_y += 1;
}
}
zoomable_view.frame = CGRectMake(0, 0, final_width*seat_width, initial_seat_y*seat_height);
[self setContentSize:zoomable_view.frame.size];
CGFloat newContentOffsetX = (self.contentSize.width - self.frame.size.width) / 2;
self.contentOffset = CGPointMake(newContentOffsetX, 0);
selected_seats = [[NSMutableArray alloc]init];
[self addSubview:zoomable_view];
}
- (void)createSeatButtonWithPosition:(int)initial_seat_x and:(int)initial_seat_y isAvailable:(BOOL)available isDisabled:(BOOL)disabled{
ZSeat *seatButton = [[ZSeat alloc]initWithFrame:
CGRectMake(initial_seat_x*seat_width,
initial_seat_y*seat_height,
seat_width,
seat_height)];
if (available && disabled) {
[self setSeatAsDisabled:seatButton];
} else if (available && !disabled) {
[self setSeatAsAvaiable:seatButton];
} else {
[self setSeatAsUnavaiable:seatButton];
}
[seatButton setAvailable:available];
[seatButton setDisabled:disabled];
[seatButton setRow:initial_seat_y+1];
[seatButton setColumn:initial_seat_x+1];
[seatButton setPrice:self.seat_price];
[seatButton addTarget:self action:@selector(seatSelected:) forControlEvents:UIControlEventTouchDown];
[zoomable_view addSubview:seatButton];
}
#pragma mark - Seat Selector Methods
- (void)seatSelected:(ZSeat*)sender{
if (!sender.selected_seat && sender.available) {
if (self.selected_seat_limit) {
[self checkSeatLimitWithSeat:sender];
} else {
[self setSeatAsSelected:sender];
[selected_seats addObject:sender];
}
} else {
[selected_seats removeObject:sender];
if (sender.available && sender.disabled) {
[self setSeatAsDisabled:sender];
} else if (sender.available && !sender.disabled) {
[self setSeatAsAvaiable:sender];
}
}
[self.seat_delegate seatSelected:sender];
[self.seat_delegate getSelectedSeats:selected_seats];
}
- (void)checkSeatLimitWithSeat:(ZSeat*)sender{
if ([selected_seats count]<self.selected_seat_limit) {
[self setSeatAsSelected:sender];
[selected_seats addObject:sender];
} else {
ZSeat *seat_to_make_avaiable = [selected_seats objectAtIndex:0];
if (seat_to_make_avaiable.disabled)
[self setSeatAsDisabled:seat_to_make_avaiable];
else
[self setSeatAsAvaiable:seat_to_make_avaiable];
[selected_seats removeObjectAtIndex:0];
[self setSeatAsSelected:sender];
[selected_seats addObject:sender];
}
}
#pragma mark - Seat Images & Availability
- (void)setAvailableImage:(UIImage *)available_image andUnavailableImage:(UIImage *)unavailable_image andDisabledImage:(UIImage *)disabled_image andSelectedImage:(UIImage *)selected_image{
self.available_image = available_image;
self.unavailable_image = unavailable_image;
self.disabled_image = disabled_image;
self.selected_image = selected_image;
}
- (void)setSeatAsUnavaiable:(ZSeat*)sender{
[sender setImage:self.unavailable_image forState:UIControlStateNormal];
[sender setSelected_seat:TRUE];
}
- (void)setSeatAsAvaiable:(ZSeat*)sender{
[sender setImage:self.available_image forState:UIControlStateNormal];
[sender setSelected_seat:FALSE];
}
- (void)setSeatAsDisabled:(ZSeat*)sender{
[sender setImage:self.disabled_image forState:UIControlStateNormal];
[sender setSelected_seat:FALSE];
}
- (void)setSeatAsSelected:(ZSeat*)sender{
[sender setImage:self.selected_image forState:UIControlStateNormal];
[sender setSelected_seat:TRUE];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
// NSLog(@"didZoom");
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [self.subviews objectAtIndex:0];
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
//NSLog(@"scrollViewWillBeginZooming");
}
@end