forked from darkseed/MKInfoPanelDemo
/
MKInfoPanel.m
200 lines (156 loc) · 6.93 KB
/
MKInfoPanel.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
//
// MKInfoPanel.m
// HorizontalMenu
//
// Created by Mugunth on 25/04/11.
// Copyright 2011 Steinlogic. All rights reserved.
// Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
// Read my blog post at http://mk.sg/8e on how to use this code
// As a side note on using this code, you might consider giving some credit to me by
// 1) linking my website from your app's website
// 2) or crediting me inside the app's credits page
// 3) or a tweet mentioning @mugunthkumar
// 4) A paypal donation to mugunth.kumar@gmail.com
//
// A note on redistribution
// While I'm ok with modifications to this source code,
// if you are re-publishing after editing, please retain the above copyright notices
#import "MKInfoPanel.h"
#import <QuartzCore/QuartzCore.h>
// Private Methods
@interface MKInfoPanel ()
@property (nonatomic, assign) MKInfoPanelType type;
+ (MKInfoPanel*) infoPanel;
- (void)setup;
@end
@implementation MKInfoPanel
@synthesize titleLabel = _titleLabel;
@synthesize detailLabel = _detailLabel;
@synthesize thumbImage = _thumbImage;
@synthesize backgroundGradient = _backgroundGradient;
@synthesize onTouched = _onTouched;
@synthesize delegate = _delegate;
@synthesize onFinished = _onFinished;
@synthesize type = type_;
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Lifecycle
////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)dealloc {
[_delegate performSelector:_onFinished];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Setter/Getter
////////////////////////////////////////////////////////////////////////
-(void)setType:(MKInfoPanelType)type {
if(type == MKInfoPanelTypeError) {
self.backgroundGradient.image = [[UIImage imageNamed:@"Red"] stretchableImageWithLeftCapWidth:1 topCapHeight:5];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.detailLabel.font = [UIFont fontWithName:@"Helvetica Neue"
size:14];
self.thumbImage.image = [UIImage imageNamed:@"Warning"];
self.detailLabel.textColor = [UIColor colorWithRed:1.f green:0.651f blue:0.651f alpha:1.f];
}
else if(type == MKInfoPanelTypeInfo) {
self.backgroundGradient.image = [[UIImage imageNamed:@"Blue"] stretchableImageWithLeftCapWidth:1 topCapHeight:5];
self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
self.thumbImage.image = [UIImage imageNamed:@"Tick"];
self.detailLabel.textColor = RGBA(210, 210, 235, 1.0);
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Show/Hide
////////////////////////////////////////////////////////////////////////
+ (MKInfoPanel *)showPanelInView:(UIView *)view type:(MKInfoPanelType)type title:(NSString *)title subtitle:(NSString *)subtitle {
return [self showPanelInView:view type:type title:title subtitle:subtitle hideAfter:-1];
}
+(MKInfoPanel *)showPanelInView:(UIView *)view type:(MKInfoPanelType)type title:(NSString *)title subtitle:(NSString *)subtitle hideAfter:(NSTimeInterval)interval {
MKInfoPanel *panel = [MKInfoPanel infoPanel];
CGFloat panelHeight = 50; // panel height when no subtitle set
panel.type = type;
panel.titleLabel.text = title;
if(subtitle) {
panel.detailLabel.text = subtitle;
[panel.detailLabel sizeToFit];
panelHeight = MAX(CGRectGetMaxY(panel.thumbImage.frame), CGRectGetMaxY(panel.detailLabel.frame));
panelHeight += 10.f; // padding at bottom
} else {
panel.detailLabel.hidden = YES;
panel.thumbImage.frame = CGRectMake(15, 5, 35, 35);
panel.titleLabel.frame = CGRectMake(57, 12, 240, 21);
}
// update frame of panel
panel.frame = CGRectMake(0, 0, view.bounds.size.width, panelHeight);
[view addSubview:panel];
if (interval > 0) {
[panel performSelector:@selector(hidePanel) withObject:view afterDelay:interval];
}
return panel;
}
+ (MKInfoPanel *)showPanelInWindow:(UIWindow *)window type:(MKInfoPanelType)type title:(NSString *)title subtitle:(NSString *)subtitle {
return [self showPanelInWindow:window type:type title:title subtitle:subtitle hideAfter:-1];
}
+(MKInfoPanel *)showPanelInWindow:(UIWindow *)window type:(MKInfoPanelType)type title:(NSString *)title subtitle:(NSString *)subtitle hideAfter:(NSTimeInterval)interval {
MKInfoPanel *panel = [self showPanelInView:window type:type title:title subtitle:subtitle hideAfter:interval];
if (![UIApplication sharedApplication].statusBarHidden) {
CGRect frame = panel.frame;
frame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;
panel.frame = frame;
}
return panel;
}
-(void)hidePanel {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
[self.layer addAnimation:transition forKey:nil];
self.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
[self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.25];
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Touch Recognition
////////////////////////////////////////////////////////////////////////
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self performSelector:_onTouched];
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private
////////////////////////////////////////////////////////////////////////
+(MKInfoPanel *)infoPanel {
MKInfoPanel *panel = (MKInfoPanel*) [[[UINib nibWithNibName:@"MKInfoPanel" bundle:nil]
instantiateWithOwner:self options:nil] objectAtIndex:0];
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromBottom;
[panel.layer addAnimation:transition forKey:nil];
return panel;
}
- (void)setup {
self.onTouched = @selector(hidePanel);
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
@end