ATMHud.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * ATMHud.m
  3. * ATMHud
  4. *
  5. * Created by Marcel Müller on 2011-03-01.
  6. * Copyright (c) 2010-2011, Marcel Müller (atomcraft)
  7. * All rights reserved.
  8. *
  9. * https://github.com/atomton/ATMHud
  10. */
  11. #import "ATMHud.h"
  12. #import <QuartzCore/QuartzCore.h>
  13. #import <AudioToolbox/AudioServices.h>
  14. #import "ATMHudView.h"
  15. #import "ATMProgressLayer.h"
  16. #import "ATMHudDelegate.h"
  17. #import "ATMSoundFX.h"
  18. #import "ATMHudQueueItem.h"
  19. @interface ATMHud (Private)
  20. - (void)construct;
  21. @end
  22. @implementation ATMHud
  23. @synthesize margin, padding, alpha, appearScaleFactor, disappearScaleFactor, progressBorderRadius, progressBorderWidth, progressBarRadius, progressBarInset;
  24. @synthesize delegate, accessoryPosition;
  25. @synthesize center;
  26. @synthesize shadowEnabled, blockTouches, allowSuperviewInteraction;
  27. @synthesize showSound, updateSound, hideSound;
  28. @synthesize __view, sound, displayQueue, queuePosition;
  29. @synthesize isShowing;
  30. static ATMHud *shared;
  31. +(ATMHud*)sharedInstance{
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. shared=[[ATMHud alloc]init];
  35. });
  36. // shared.alpha = 0.5;
  37. shared.allowSuperviewInteraction = NO;
  38. [shared setActivity:YES];
  39. [shared setActivityStyle:UIActivityIndicatorViewStyleWhite];
  40. return shared;
  41. }
  42. - (id)init {
  43. if ((self = [super init])) {
  44. [self construct];
  45. }
  46. return self;
  47. }
  48. - (id)initWithDelegate:(id)hudDelegate {
  49. if ((self = [super init])) {
  50. delegate = hudDelegate;
  51. [self construct];
  52. }
  53. return self;
  54. }
  55. - (void)loadView {
  56. UIView *base = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  57. base.backgroundColor = [UIColor clearColor];
  58. base.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
  59. UIViewAutoresizingFlexibleHeight);
  60. base.userInteractionEnabled = NO;
  61. [base addSubview:__view];
  62. self.view = base;
  63. // [base release];
  64. }
  65. - (void)viewDidLoad {
  66. [super viewDidLoad];
  67. }
  68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  69. return YES;
  70. }
  71. - (void)didReceiveMemoryWarning {
  72. [super didReceiveMemoryWarning];
  73. }
  74. - (void)viewDidUnload {
  75. [super viewDidUnload];
  76. }
  77. - (void)dealloc {
  78. //// [sound release];
  79. //// [__view release];
  80. //// [displayQueue release];
  81. ////
  82. //// [showSound release];
  83. //// [updateSound release];
  84. //// [hideSound release];
  85. //
  86. // [super dealloc];
  87. }
  88. + (NSString *)buildInfo {
  89. return @"atomHUD 1.2 • 2011-03-01";
  90. }
  91. #pragma mark -
  92. #pragma mark Overrides
  93. - (void)setAppearScaleFactor:(CGFloat)value {
  94. if (value == 0) {
  95. value = 0.01;
  96. }
  97. appearScaleFactor = value;
  98. }
  99. - (void)setDisappearScaleFactor:(CGFloat)value {
  100. if (value == 0) {
  101. value = 0.01;
  102. }
  103. disappearScaleFactor = value;
  104. }
  105. - (void)setAlpha:(CGFloat)value {
  106. alpha = value;
  107. [CATransaction begin];
  108. [CATransaction setDisableActions:YES];
  109. __view.backgroundLayer.backgroundColor = [UIColor colorWithWhite:0.0 alpha:value].CGColor;
  110. [CATransaction commit];
  111. }
  112. - (void)setShadowEnabled:(BOOL)value {
  113. shadowEnabled = value;
  114. if (shadowEnabled) {
  115. __view.layer.shadowOpacity = 0.4;
  116. } else {
  117. __view.layer.shadowOpacity = 0.0;
  118. }
  119. }
  120. #pragma mark -
  121. #pragma mark Property forwards
  122. - (void)setCaption:(NSString *)caption {
  123. __view.caption = caption;
  124. }
  125. - (void)setImage:(UIImage *)image {
  126. __view.image = image;
  127. }
  128. - (void)setActivity:(BOOL)activity {
  129. __view.showActivity = activity;
  130. if (activity) {
  131. [__view.activity startAnimating];
  132. } else {
  133. [__view.activity stopAnimating];
  134. }
  135. }
  136. - (void)setActivityStyle:(UIActivityIndicatorViewStyle)activityStyle {
  137. __view.activityStyle = activityStyle;
  138. if (activityStyle == UIActivityIndicatorViewStyleWhiteLarge) {
  139. __view.activitySize = CGSizeMake(37, 37);
  140. } else {
  141. __view.activitySize = CGSizeMake(20, 20);
  142. }
  143. }
  144. - (void)setFixedSize:(CGSize)fixedSize {
  145. __view.fixedSize = fixedSize;
  146. }
  147. - (void)setProgress:(CGFloat)progress {
  148. __view.progress = progress;
  149. [__view.progressLayer setTheProgress:progress];
  150. [__view.progressLayer setNeedsDisplay];
  151. }
  152. #pragma mark -
  153. #pragma mark Queue
  154. - (void)addQueueItem:(ATMHudQueueItem *)item {
  155. [displayQueue addObject:item];
  156. }
  157. - (void)addQueueItems:(NSArray *)items {
  158. [displayQueue addObjectsFromArray:items];
  159. }
  160. - (void)clearQueue {
  161. [displayQueue removeAllObjects];
  162. }
  163. - (void)startQueue {
  164. queuePosition = 0;
  165. if (!CGSizeEqualToSize(__view.fixedSize, CGSizeZero)) {
  166. CGSize newSize = __view.fixedSize;
  167. CGSize targetSize;
  168. ATMHudQueueItem *queueItem;
  169. for (int i = 0; i < [displayQueue count]; i++) {
  170. queueItem = [displayQueue objectAtIndex:i];
  171. targetSize = [__view calculateSizeForQueueItem:queueItem];
  172. if (targetSize.width > newSize.width) {
  173. newSize.width = targetSize.width;
  174. }
  175. if (targetSize.height > newSize.height) {
  176. newSize.height = targetSize.height;
  177. }
  178. }
  179. [self setFixedSize:newSize];
  180. }
  181. [self showQueueAtIndex:queuePosition];
  182. }
  183. - (void)showNextInQueue {
  184. queuePosition++;
  185. [self showQueueAtIndex:queuePosition];
  186. }
  187. - (void)showQueueAtIndex:(NSInteger)index {
  188. if ([displayQueue count] > 0) {
  189. queuePosition = index;
  190. if (queuePosition == [displayQueue count]) {
  191. [self hide];
  192. return;
  193. }
  194. ATMHudQueueItem *item = [displayQueue objectAtIndex:queuePosition];
  195. __view.caption = item.caption;
  196. __view.image = item.image;
  197. BOOL flag = item.showActivity;
  198. __view.showActivity = flag;
  199. if (flag) {
  200. [__view.activity startAnimating];
  201. } else {
  202. [__view.activity stopAnimating];
  203. }
  204. self.accessoryPosition = item.accessoryPosition;
  205. [self setActivityStyle:item.activityStyle];
  206. if (queuePosition == 0) {
  207. [__view show];
  208. } else {
  209. [__view update];
  210. }
  211. }
  212. }
  213. #pragma mark -
  214. #pragma mark Controlling
  215. - (void)show {
  216. // [self hide];
  217. isShowing = YES;
  218. [__view show];
  219. }
  220. - (void)show:(UIView*)parent{
  221. [parent addSubview:self.view];
  222. // [parent bringSubviewToFront:self.view];
  223. [self show];
  224. }
  225. - (void)start{
  226. self.alpha = 0.5;
  227. [self setActivity:YES];
  228. [self show:[UIApplication sharedApplication].keyWindow];
  229. }
  230. - (void)startWithClearColor {
  231. self.alpha = 0;
  232. [self setActivity:YES];
  233. [self show:[UIApplication sharedApplication].keyWindow];
  234. }
  235. - (void)start:(NSString*)s{
  236. self.alpha = 0.5;
  237. [self setCaption:s];
  238. [self setActivity:YES];
  239. [self show:[UIApplication sharedApplication].keyWindow];
  240. }
  241. - (void)start:(NSString*)s inView:(UIView *)parent{
  242. self.alpha = 0.5;
  243. [self setCaption:s];
  244. [self setActivity:YES];
  245. [self show:parent];
  246. }
  247. - (void)start:(NSString*)s delay:(int)delay{
  248. self.alpha = 0.5;
  249. [self setCaption:s];
  250. [self show:[UIApplication sharedApplication].keyWindow];
  251. [self hideAfter:delay];
  252. }
  253. - (void)update {
  254. [__view update];
  255. }
  256. - (void)hide {
  257. isShowing = NO;
  258. [__view hide];
  259. // [[UIApplication sharedApplication].keyWindow sendSubviewToBack:self.view];
  260. [self.view removeFromSuperview];
  261. }
  262. - (void)stop {
  263. [self hide];
  264. }
  265. - (void)hideAfter:(NSTimeInterval)delay {
  266. [self performSelector:@selector(hide) withObject:nil afterDelay:delay];
  267. }
  268. #pragma mark -
  269. #pragma mark Internal methods
  270. - (void)construct {
  271. isShowing = NO;
  272. margin = padding = 10.0;
  273. alpha = 0.7;
  274. progressBorderRadius = 8.0;
  275. progressBorderWidth = 2.0;
  276. progressBarRadius = 5.0;
  277. progressBarInset = 3.0;
  278. accessoryPosition = ATMHudAccessoryPositionBottom;
  279. appearScaleFactor = disappearScaleFactor = 1.4;
  280. __view = [[ATMHudView alloc] initWithFrame:CGRectZero andController:self];
  281. __view.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin |
  282. UIViewAutoresizingFlexibleRightMargin |
  283. UIViewAutoresizingFlexibleBottomMargin |
  284. UIViewAutoresizingFlexibleLeftMargin);
  285. displayQueue = [[NSMutableArray alloc] init];
  286. queuePosition = 0;
  287. center = CGPointZero;
  288. blockTouches = NO;
  289. allowSuperviewInteraction = NO;
  290. }
  291. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  292. if (!blockTouches) {
  293. UITouch *aTouch = [touches anyObject];
  294. if (aTouch.tapCount == 1) {
  295. CGPoint p = [aTouch locationInView:self.view];
  296. if (CGRectContainsPoint(__view.frame, p)) {
  297. if ([(id)self.delegate respondsToSelector:@selector(userDidTapHud:)]) {
  298. [self.delegate userDidTapHud:self];
  299. }
  300. }
  301. }
  302. }
  303. }
  304. - (void)playSound:(NSString *)soundPath {
  305. sound = [[ATMSoundFX alloc] initWithContentsOfFile:soundPath];
  306. [sound play];
  307. }
  308. @end