Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 305 lines (262 sloc) 10.13 kb
8c60097 @y0n3l First commit. A HUD for iOS.
authored
1 //
2 // LGViewHUD.m
3 // Created by y0n3l on 4/13/11.
4 //
5
6 #import "LGViewHUD.h"
bccd267 @y0n3l rework API.
authored
7 #import "NString+Height.h"
8c60097 @y0n3l First commit. A HUD for iOS.
authored
8 #import <QuartzCore/QuartzCore.h>
9
10 static LGViewHUD* defaultHUD = nil;
11
bccd267 @y0n3l rework API.
authored
12 @interface LGViewHUD () {
13 UIFont* _labelsFont;
14 UIColor* _hudColor;
15 NSString* _topText;
16 NSString* _bottomText;
e5e234e @y0n3l fix rendering mode for image (to work properly on iOS7)
authored
17 UIImage* _image;
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
18 NSTimeInterval _displayDuration;
bccd267 @y0n3l rework API.
authored
19 NSTimer* displayTimer;
20 BOOL activityIndicatorOn;
21 UIActivityIndicatorView* activityIndicator;
22 }
23
8c60097 @y0n3l First commit. A HUD for iOS.
authored
24 @end
25
26 @implementation LGViewHUD
27
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
28 @synthesize displayDuration = _displayDuration;
29 @synthesize animationDuration = _animationDuration;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
30
31 #define kHUDDefaultAlphaValue 0.65
32 #define kHUDDefaultDisplayDuration 2
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
33 #define kHUDDefaultAnimationDuration 0.3
bccd267 @y0n3l rework API.
authored
34 #define kHUDCornerRadius 10
8c60097 @y0n3l First commit. A HUD for iOS.
authored
35
36 - (id)initWithFrame:(CGRect)frame {
37 self = [super initWithFrame:frame];
38 if (self) {
bccd267 @y0n3l rework API.
authored
39 _labelsFont = [[UIFont boldSystemFontOfSize:17] retain];
40 self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
c901b87 @y0n3l Autoresize mask for landscape / portrait rotation.
authored
41 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
bccd267 @y0n3l rework API.
authored
42 self.userInteractionEnabled = NO;
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
43 self.displayDuration = kHUDDefaultDisplayDuration;
44 self.animationDuration = kHUDDefaultAnimationDuration;
bccd267 @y0n3l rework API.
authored
45 _hudColor = [[UIColor colorWithWhite:0 alpha:kHUDDefaultAlphaValue] retain];
46 self.backgroundColor = [UIColor clearColor];
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
47 self.tintColor = [UIColor whiteColor];
c778550 @y0n3l Add parallax effect to HUD (iOS7 buds like)
authored
48 [self addParallaxEffect];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
49 }
50 return self;
51 }
52
53 - (void)dealloc {
bccd267 @y0n3l rework API.
authored
54 [_labelsFont release];
55 _labelsFont = nil;
56 [_hudColor release];
57 _hudColor = nil;
58 [super dealloc];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
59 }
60
c778550 @y0n3l Add parallax effect to HUD (iOS7 buds like)
authored
61 -(void) addParallaxEffect {
62 double parallaxAmount = 10.0;
63 UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
64 interpolationHorizontal.minimumRelativeValue = @(-parallaxAmount);
65 interpolationHorizontal.maximumRelativeValue = @(parallaxAmount);
66
67 UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
68 interpolationVertical.minimumRelativeValue = @(-parallaxAmount);
69 interpolationVertical.maximumRelativeValue = @(parallaxAmount);
70
71 [self addMotionEffect:interpolationHorizontal];
72 [self addMotionEffect:interpolationVertical];
73 [interpolationHorizontal release];
74 [interpolationVertical release];
75 }
76
77
8c60097 @y0n3l First commit. A HUD for iOS.
authored
78 +(LGViewHUD*) defaultHUD {
79 if (defaultHUD==nil)
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
80 defaultHUD = [[LGViewHUD alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
81 return defaultHUD;
82 }
83
84 -(void) setTopText:(NSString *)t {
bccd267 @y0n3l rework API.
authored
85 [t retain];
86 [_topText release];
87 _topText = t;
88 [self setNeedsDisplay];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
89 }
90
91 -(NSString*) topText {
bccd267 @y0n3l rework API.
authored
92 return _topText;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
93 }
94
95 -(void) setBottomText:(NSString *)t {
bccd267 @y0n3l rework API.
authored
96 [t retain];
97 [_bottomText release];
98 _bottomText = t;
99 [self setNeedsDisplay];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
100 }
101
102 -(NSString*) bottomText {
bccd267 @y0n3l rework API.
authored
103 return _bottomText;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
104 }
105
bccd267 @y0n3l rework API.
authored
106 /** This disables the activity indicator on if any. */
8c60097 @y0n3l First commit. A HUD for iOS.
authored
107 -(void) setImage:(UIImage*) img {
e5e234e @y0n3l fix rendering mode for image (to work properly on iOS7)
authored
108 img = [[img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate ] retain];
109 [_image release];
110 _image = img;
bccd267 @y0n3l rework API.
authored
111 self.activityIndicatorOn = NO;
112 [self setNeedsDisplay];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
113 }
114
115 -(UIImage*) image {
e5e234e @y0n3l fix rendering mode for image (to work properly on iOS7)
authored
116 return _image;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
117 }
118
119 -(BOOL) activityIndicatorOn {
120 return activityIndicatorOn;
121 }
122
123 -(void) setActivityIndicatorOn:(BOOL)isOn {
124 if (activityIndicatorOn!=isOn) {
125 activityIndicatorOn=isOn;
126 if (activityIndicatorOn) {
127 activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
128 [activityIndicator startAnimating];
5aeca12 @y0n3l remove "test" label in bottom bar + adjust center position with boun…
authored
129 activityIndicator.center=CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
8c60097 @y0n3l First commit. A HUD for iOS.
authored
130 [self addSubview:activityIndicator];
131 } else {
132 //when applying an image, this will auto hide the HUD.
133 [activityIndicator removeFromSuperview];
134 [activityIndicator release];
135 activityIndicator=nil;
136 }
bccd267 @y0n3l rework API.
authored
137 [self setNeedsDisplay];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
138 }
139 }
140
bccd267 @y0n3l rework API.
authored
141 -(void) drawRect:(CGRect)rect {
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
142 CGFloat labelSideMargins = 5;
143
bccd267 @y0n3l rework API.
authored
144 UIBezierPath* bPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:kHUDCornerRadius];
145 [_hudColor setFill];
146 [bPath fill];
147
2eb2e5b @y0n3l The central canvas size should always be the same (whatever activity …
authored
148 CGFloat imgCanvasHeight = rect.size.height / 2.0;
bccd267 @y0n3l rework API.
authored
149
150 CGFloat labelsWidth = self.frame.size.width - 2*labelSideMargins;
151 CGFloat topLabelHeight = [_topText heightForWidth:labelsWidth usingFont:_labelsFont];
152
2eb2e5b @y0n3l The central canvas size should always be the same (whatever activity …
authored
153 CGFloat labelsMaxHeight = (self.frame.size.height - imgCanvasHeight) / 2.0;
bccd267 @y0n3l rework API.
authored
154 NSDictionary* attrs = nil;
155 NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
156 paragraphStyle.alignment = NSTextAlignmentCenter;
157 attrs = @{NSFontAttributeName: _labelsFont,
158 NSForegroundColorAttributeName: [UIColor whiteColor],
159 NSParagraphStyleAttributeName: paragraphStyle};
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
160 CGFloat topY = (labelsMaxHeight - topLabelHeight) / 2.0;
161 topY = topY<labelSideMargins?labelSideMargins:topY;
162 [_topText drawInRect:CGRectMake(labelSideMargins, topY, labelsWidth, topLabelHeight)
163 withAttributes:attrs];
bccd267 @y0n3l rework API.
authored
164
165 CGFloat bottomLabelHeight = [_bottomText heightForWidth:labelsWidth usingFont:_labelsFont];
2eb2e5b @y0n3l The central canvas size should always be the same (whatever activity …
authored
166 CGFloat bottomY = labelsMaxHeight + imgCanvasHeight + (labelsMaxHeight - bottomLabelHeight) /2.0;
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
167 bottomY = (bottomY + bottomLabelHeight > rect.size.height - labelSideMargins) ? rect.size.height - bottomLabelHeight - labelSideMargins:bottomY;
bccd267 @y0n3l rework API.
authored
168 [_bottomText drawInRect:CGRectMake(labelSideMargins,
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
169 bottomY,
bccd267 @y0n3l rework API.
authored
170 labelsWidth,
171 bottomLabelHeight)
172 withAttributes:attrs];
173
174 [paragraphStyle release];
175
176 if (!activityIndicatorOn) {
e5e234e @y0n3l fix rendering mode for image (to work properly on iOS7)
authored
177 CGRect imageFrame = CGRectMake((self.frame.size.width - _image.size.width) /2.0,
178 (self.frame.size.height - _image.size.height) / 2.0,
179 _image.size.width,
180 _image.size.height);
181 [_image drawInRect:imageFrame];
bccd267 @y0n3l rework API.
authored
182 }
183 [super drawRect:rect];
184 }
185
8c60097 @y0n3l First commit. A HUD for iOS.
authored
186 -(void) layoutSubviews {
bccd267 @y0n3l rework API.
authored
187 activityIndicator.center = CGPointMake(ceilf(self.frame.size.width / 2.0),
188 ceilf(self.frame.size.height / 2.0));
189
190 [super layoutSubviews];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
191 }
192
57bb4b9 @y0n3l new API signatures + remove shadow.
authored
193 -(void) show {
194 [self showWithAnimation:HUDAnimationNone];
195 }
196
197 -(void) showWithAnimation:(HUDAnimation)animation {
198 [self showInView:[UIApplication sharedApplication].windows.firstObject withAnimation:animation];
199 }
200
8c60097 @y0n3l First commit. A HUD for iOS.
authored
201 -(void) showInView:(UIView*)view {
202 [self showInView:view withAnimation:HUDAnimationNone];
203 }
204
205 -(void) showInView:(UIView *)view withAnimation:(HUDAnimation)animation {
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
206 [self showInView:view withAnimation:animation forDuration:0];
207 }
208
209 -(void) showInView:(UIView *)view withAnimation:(HUDAnimation)animation forDuration:(NSTimeInterval)showDuration {
210 showDuration = (showDuration==0)?_displayDuration:showDuration;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
211 switch (animation) {
212 case HUDAnimationNone:
213 self.alpha=1.0;
5aeca12 @y0n3l remove "test" label in bottom bar + adjust center position with boun…
authored
214 self.transform=CGAffineTransformMakeScale(1, 1);
4ae0b4e @y0n3l use bounds instead of frame. Improve timer behavior (ex change NSRunL…
authored
215 self.center=CGPointMake(view.bounds.size.width/2.0, view.bounds.size.height/2.0);
8c60097 @y0n3l First commit. A HUD for iOS.
authored
216 [view addSubview:self];
217 break;
c778550 @y0n3l Add parallax effect to HUD (iOS7 buds like)
authored
218 case HUDAnimationFade:
219 self.alpha=0.0;
220 [UIView beginAnimations:@"HUDShowFade" context:nil];
221 [UIView setAnimationDelegate:self];
222 [UIView setAnimationDuration:self.animationDuration];
223 self.alpha=1.0;
224 [UIView commitAnimations];
225 break;
96ad327 @y0n3l Simplify animation styles constants.
authored
226 case HUDAnimationZoom:
4ae0b4e @y0n3l use bounds instead of frame. Improve timer behavior (ex change NSRunL…
authored
227 self.center=CGPointMake(view.bounds.size.width/2.0, view.bounds.size.height/2.0);
8c60097 @y0n3l First commit. A HUD for iOS.
authored
228 self.alpha=0;
229 self.transform=CGAffineTransformMakeScale(1.7, 1.7);
230 [view addSubview:self];
231 [UIView beginAnimations:@"HUDShowZoom" context:nil];
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
232 [UIView setAnimationDuration:self.animationDuration];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
233 [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
234 self.transform=CGAffineTransformMakeScale(1, 1);
235 self.alpha=1.0;
236 [UIView commitAnimations];
237 default:
238 break;
239 }
240 if (!activityIndicatorOn) {
96ad327 @y0n3l Simplify animation styles constants.
authored
241 HUDAnimation disappearAnimation = HUDAnimationFade;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
242 switch (animation) {
96ad327 @y0n3l Simplify animation styles constants.
authored
243 case HUDAnimationZoom:
244 disappearAnimation = HUDAnimationZoom;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
245 break;
246 default:
96ad327 @y0n3l Simplify animation styles constants.
authored
247 disappearAnimation = HUDAnimationFade;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
248 break;
249 }
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
250 [self hideAfterDelay:showDuration withAnimation:disappearAnimation ];
4ae0b4e @y0n3l use bounds instead of frame. Improve timer behavior (ex change NSRunL…
authored
251 } else {
252 //invalidate current timer for hide if any.
253 [displayTimer invalidate];
254 [displayTimer release];
255 displayTimer=nil;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
256 }
257 }
258
259 -(void) hideAfterDelay:(NSTimeInterval)delayDuration withAnimation:(HUDAnimation) animation{
260 [displayTimer invalidate];
261 [displayTimer release];
4ae0b4e @y0n3l use bounds instead of frame. Improve timer behavior (ex change NSRunL…
authored
262 displayTimer = [[NSTimer timerWithTimeInterval:delayDuration target:self selector:@selector(displayTimeOut:)
263 userInfo:[NSNumber numberWithInt:animation] repeats:NO] retain];
264 [[NSRunLoop mainRunLoop] addTimer:displayTimer forMode:NSRunLoopCommonModes];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
265 }
266
267 -(void) displayTimeOut:(NSTimer*)timer {
268 [self hideWithAnimation:(HUDAnimation)[[timer userInfo] intValue]];
269 }
270
271 -(void) hideWithAnimation:(HUDAnimation)animation {
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
272 [displayTimer release];
273 displayTimer=nil;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
274 switch (animation) {
96ad327 @y0n3l Simplify animation styles constants.
authored
275 case HUDAnimationZoom:
8c60097 @y0n3l First commit. A HUD for iOS.
authored
276 [UIView beginAnimations:@"HUDHideZoom" context:nil];
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
277 [UIView setAnimationDuration:self.animationDuration];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
278 [UIView setAnimationDelegate:self];
279 [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
280 self.transform=CGAffineTransformMakeScale(0.1, 0.1);
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
281 self.alpha=0.0;
8c60097 @y0n3l First commit. A HUD for iOS.
authored
282 [UIView commitAnimations];
283 break;
96ad327 @y0n3l Simplify animation styles constants.
authored
284 case HUDAnimationFade:
8c60097 @y0n3l First commit. A HUD for iOS.
authored
285 [UIView beginAnimations:@"HUDHideFade" context:nil];
286 [UIView setAnimationDelegate:self];
e327f7e @y0n3l Adjust animation duration property + several others API improvments.
authored
287 [UIView setAnimationDuration:self.animationDuration];
8c60097 @y0n3l First commit. A HUD for iOS.
authored
288 self.alpha=0.0;
289 [UIView commitAnimations];
290 break;
291 case HUDAnimationNone:
292 default:
293 [self removeFromSuperview];
294 break;
295 }
296 }
297
298 -(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
5aeca12 @y0n3l remove "test" label in bottom bar + adjust center position with boun…
authored
299 if (self.alpha==0.0) {
8c60097 @y0n3l First commit. A HUD for iOS.
authored
300 [self removeFromSuperview];
5aeca12 @y0n3l remove "test" label in bottom bar + adjust center position with boun…
authored
301 }
8c60097 @y0n3l First commit. A HUD for iOS.
authored
302 }
303
304 @end
Something went wrong with that request. Please try again.