CustomIOSAlertView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //
  2. // CustomIOSAlertView.m
  3. // CustomIOSAlertView
  4. //
  5. // Created by Richard on 20/09/2013.
  6. // Copyright (c) 2013-2015 Wimagguc.
  7. //
  8. // Lincesed under The MIT License (MIT)
  9. // http://opensource.org/licenses/MIT
  10. //
  11. #import "CustomIOSAlertView.h"
  12. #import <QuartzCore/QuartzCore.h>
  13. #import "const.h"
  14. const static CGFloat kCustomIOSAlertViewDefaultButtonHeight = 50;
  15. const static CGFloat kCustomIOSAlertViewDefaultButtonSpacerHeight = 1;
  16. const static CGFloat kCustomIOSAlertViewCornerRadius = 15;
  17. const static CGFloat kCustomIOS7MotionEffectExtent = 10.0;
  18. @implementation CustomIOSAlertView
  19. CGFloat buttonHeight = 0;
  20. CGFloat buttonSpacerHeight = 0;
  21. @synthesize parentView, containerView, dialogView, onButtonTouchUpInside;
  22. @synthesize delegate;
  23. @synthesize buttonTitles;
  24. @synthesize useMotionEffects;
  25. - (id)initWithParentView: (UIView *)_parentView
  26. {
  27. self = [self init];
  28. if (_parentView) {
  29. self.frame = _parentView.frame;
  30. self.parentView = _parentView;
  31. }
  32. return self;
  33. }
  34. - (id)init
  35. {
  36. self = [super init];
  37. if (self) {
  38. self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  39. delegate = self;
  40. useMotionEffects = false;
  41. buttonTitles = @[@"Close"];
  42. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  46. }
  47. return self;
  48. }
  49. // Create the dialog view, and animate opening the dialog
  50. - (void)show
  51. {
  52. dialogView = [self createContainerView];
  53. dialogView.layer.shouldRasterize = YES;
  54. dialogView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
  55. self.layer.shouldRasterize = YES;
  56. self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
  57. #if (defined(__IPHONE_7_0))
  58. if (useMotionEffects) {
  59. [self applyMotionEffects];
  60. }
  61. #endif
  62. self.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
  63. [self addSubview:dialogView];
  64. // Can be attached to a view or to the top most window
  65. // Attached to a view:
  66. if (parentView != NULL) {
  67. [parentView addSubview:self];
  68. // Attached to the top most window
  69. } else {
  70. // On iOS7, calculate with orientation
  71. // if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  72. //
  73. // UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  74. // switch (interfaceOrientation) {
  75. // case UIInterfaceOrientationLandscapeLeft:
  76. // self.transform = CGAffineTransformMakeRotation(M_PI * 270.0 / 180.0);
  77. // break;
  78. //
  79. // case UIInterfaceOrientationLandscapeRight:
  80. // self.transform = CGAffineTransformMakeRotation(M_PI * 90.0 / 180.0);
  81. // break;
  82. //
  83. // case UIInterfaceOrientationPortraitUpsideDown:
  84. // self.transform = CGAffineTransformMakeRotation(M_PI * 180.0 / 180.0);
  85. // break;
  86. //
  87. // default:
  88. // break;
  89. // }
  90. //
  91. // [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  92. //
  93. // // On iOS8, just place the dialog in the middle
  94. // } else
  95. {
  96. CGSize screenSize = [self countScreenSize];
  97. CGSize dialogSize = [self countDialogSize];
  98. CGSize keyboardSize = CGSizeMake(0, 0);
  99. dialogView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  100. }
  101. [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self];
  102. }
  103. dialogView.layer.opacity = 0.5f;
  104. dialogView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
  105. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
  106. animations:^{
  107. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
  108. self->dialogView.layer.opacity = 1.0f;
  109. self->dialogView.layer.transform = CATransform3DMakeScale(1, 1, 1);
  110. }
  111. completion:NULL
  112. ];
  113. }
  114. // Button has been touched
  115. - (IBAction)customIOS7dialogButtonTouchUpInside:(id)sender
  116. {
  117. if (delegate != NULL) {
  118. [delegate customIOS7dialogButtonTouchUpInside:self clickedButtonAtIndex:[sender tag]];
  119. }
  120. if (onButtonTouchUpInside != NULL) {
  121. onButtonTouchUpInside(self, (int)[sender tag]);
  122. }
  123. }
  124. // Default button behaviour
  125. - (void)customIOS7dialogButtonTouchUpInside: (CustomIOSAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  126. {
  127. DebugLog(@"Button Clicked! %d, %d", (int)buttonIndex, (int)[alertView tag]);
  128. [self close];
  129. }
  130. // Dialog close animation then cleaning and removing the view from the parent
  131. - (void)close
  132. {
  133. CATransform3D currentTransform = dialogView.layer.transform;
  134. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  135. CGFloat startRotation = [[dialogView valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
  136. CATransform3D rotation = CATransform3DMakeRotation(-startRotation + M_PI * 270.0 / 180.0, 0.0f, 0.0f, 0.0f);
  137. dialogView.layer.transform = CATransform3DConcat(rotation, CATransform3DMakeScale(1, 1, 1));
  138. }
  139. dialogView.layer.opacity = 1.0f;
  140. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  141. animations:^{
  142. self.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
  143. self.dialogView.layer.transform = CATransform3DConcat(currentTransform, CATransform3DMakeScale(0.6f, 0.6f, 1.0));
  144. self.dialogView.layer.opacity = 0.0f;
  145. }
  146. completion:^(BOOL finished) {
  147. for (UIView *v in [self subviews]) {
  148. [v removeFromSuperview];
  149. }
  150. [self removeFromSuperview];
  151. }
  152. ];
  153. }
  154. - (void)setSubView: (UIView *)subView
  155. {
  156. containerView = subView;
  157. }
  158. // Creates the container view here: create the dialog, then add the custom content and buttons
  159. - (UIView *)createContainerView
  160. {
  161. if (containerView == NULL) {
  162. containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
  163. }
  164. CGSize screenSize = [self countScreenSize];
  165. CGSize dialogSize = [self countDialogSize];
  166. // For the black background
  167. [self setFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
  168. // This is the dialog's container; we attach the custom content and the buttons to this one
  169. UIView *dialogContainer = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height)];
  170. // First, we style the dialog to match the iOS7 UIAlertView >>>
  171. CAGradientLayer *gradient = [CAGradientLayer layer];
  172. gradient.frame = dialogContainer.bounds;
  173. gradient.colors = [NSArray arrayWithObjects:
  174. (id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
  175. (id)[[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0f] CGColor],
  176. (id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
  177. nil];
  178. CGFloat cornerRadius = kCustomIOSAlertViewCornerRadius;
  179. gradient.cornerRadius = cornerRadius;
  180. [dialogContainer.layer insertSublayer:gradient atIndex:0];
  181. dialogContainer.layer.cornerRadius = cornerRadius;
  182. dialogContainer.layer.borderColor = [[UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f] CGColor];
  183. dialogContainer.layer.borderWidth = 1;
  184. dialogContainer.layer.shadowRadius = cornerRadius + 5;
  185. dialogContainer.layer.shadowOpacity = 0.1f;
  186. dialogContainer.layer.shadowOffset = CGSizeMake(0 - (cornerRadius+5)/2, 0 - (cornerRadius+5)/2);
  187. dialogContainer.layer.shadowColor = [UIColor blackColor].CGColor;
  188. dialogContainer.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:dialogContainer.bounds cornerRadius:dialogContainer.layer.cornerRadius].CGPath;
  189. // There is a line above the button
  190. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight, dialogContainer.bounds.size.width, buttonSpacerHeight)];
  191. lineView.backgroundColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f];
  192. [dialogContainer addSubview:lineView];
  193. // ^^^
  194. // Add the custom container if there is any
  195. [dialogContainer addSubview:containerView];
  196. // Add the buttons too
  197. [self addButtonsToView:dialogContainer];
  198. return dialogContainer;
  199. }
  200. // Helper function: add buttons to container
  201. - (void)addButtonsToView: (UIView *)container
  202. {
  203. if (buttonTitles==NULL) { return; }
  204. CGFloat buttonWidth = container.bounds.size.width / [buttonTitles count];
  205. for (int i=0; i<[buttonTitles count]; i++) {
  206. UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  207. [closeButton setFrame:CGRectMake(i * buttonWidth, container.bounds.size.height - buttonHeight, buttonWidth, buttonHeight)];
  208. [closeButton addTarget:self action:@selector(customIOS7dialogButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  209. [closeButton setTag:i];
  210. [closeButton setTitle:[buttonTitles objectAtIndex:i] forState:UIControlStateNormal];
  211. [closeButton setTitleColor:[UIColor colorWithRed:0.0f green:0.5f blue:1.0f alpha:1.0f] forState:UIControlStateNormal];
  212. [closeButton setTitleColor:[UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:0.5f] forState:UIControlStateHighlighted];
  213. [closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
  214. [closeButton.layer setCornerRadius:kCustomIOSAlertViewCornerRadius];
  215. [container addSubview:closeButton];
  216. }
  217. }
  218. // Helper function: count and return the dialog's size
  219. - (CGSize)countDialogSize
  220. {
  221. CGFloat dialogWidth = containerView.frame.size.width;
  222. CGFloat dialogHeight = containerView.frame.size.height + buttonHeight + buttonSpacerHeight;
  223. return CGSizeMake(dialogWidth, dialogHeight);
  224. }
  225. // Helper function: count and return the screen's size
  226. - (CGSize)countScreenSize
  227. {
  228. if (buttonTitles!=NULL && [buttonTitles count] > 0) {
  229. buttonHeight = kCustomIOSAlertViewDefaultButtonHeight;
  230. buttonSpacerHeight = kCustomIOSAlertViewDefaultButtonSpacerHeight;
  231. } else {
  232. buttonHeight = 0;
  233. buttonSpacerHeight = 0;
  234. }
  235. CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
  236. CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  237. // On iOS7, screen width and height doesn't automatically follow orientation
  238. // if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  239. // UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  240. // if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
  241. // CGFloat tmp = screenWidth;
  242. // screenWidth = screenHeight;
  243. // screenHeight = tmp;
  244. // }
  245. // }
  246. //
  247. return CGSizeMake(screenWidth, screenHeight);
  248. }
  249. #if (defined(__IPHONE_7_0))
  250. // Add motion effects
  251. - (void)applyMotionEffects {
  252. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
  253. return;
  254. }
  255. UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
  256. type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
  257. horizontalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
  258. horizontalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
  259. UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
  260. type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
  261. verticalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
  262. verticalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
  263. UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
  264. motionEffectGroup.motionEffects = @[horizontalEffect, verticalEffect];
  265. [dialogView addMotionEffect:motionEffectGroup];
  266. }
  267. #endif
  268. - (void)dealloc
  269. {
  270. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
  271. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
  272. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  273. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  274. }
  275. //// Rotation changed, on iOS7
  276. //- (void)changeOrientationForIOS7 {
  277. //
  278. // UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  279. //
  280. // CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
  281. // CGAffineTransform rotation;
  282. //
  283. // switch (interfaceOrientation) {
  284. // case UIInterfaceOrientationLandscapeLeft:
  285. // rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);
  286. // break;
  287. //
  288. // case UIInterfaceOrientationLandscapeRight:
  289. // rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);
  290. // break;
  291. //
  292. // case UIInterfaceOrientationPortraitUpsideDown:
  293. // rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);
  294. // break;
  295. //
  296. // default:
  297. // rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);
  298. // break;
  299. // }
  300. //
  301. // [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  302. // animations:^{
  303. // self->dialogView.transform = rotation;
  304. //
  305. // }
  306. // completion:nil
  307. // ];
  308. //
  309. //}
  310. // Rotation changed, on iOS8
  311. - (void)changeOrientationForIOS8: (NSNotification *)notification {
  312. CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
  313. CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  314. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  315. animations:^{
  316. CGSize dialogSize = [self countDialogSize];
  317. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  318. self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
  319. self->dialogView.frame = CGRectMake((screenWidth - dialogSize.width) / 2, (screenHeight - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  320. }
  321. completion:nil
  322. ];
  323. }
  324. // Handle device orientation changes
  325. - (void)deviceOrientationDidChange: (NSNotification *)notification
  326. {
  327. // If dialog is attached to the parent view, it probably wants to handle the orientation change itself
  328. if (parentView != NULL) {
  329. return;
  330. }
  331. // if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  332. // [self changeOrientationForIOS7];
  333. // } else
  334. {
  335. [self changeOrientationForIOS8:notification];
  336. }
  337. }
  338. // Handle keyboard show/hide changes
  339. - (void)keyboardWillShow: (NSNotification *)notification
  340. {
  341. CGSize screenSize = [self countScreenSize];
  342. CGSize dialogSize = [self countDialogSize];
  343. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  344. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  345. if (UIInterfaceOrientationIsLandscape(interfaceOrientation) && NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) {
  346. CGFloat tmp = keyboardSize.height;
  347. keyboardSize.height = keyboardSize.width;
  348. keyboardSize.width = tmp;
  349. }
  350. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  351. animations:^{
  352. self->dialogView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  353. }
  354. completion:nil
  355. ];
  356. }
  357. - (void)keyboardWillHide: (NSNotification *)notification
  358. {
  359. CGSize screenSize = [self countScreenSize];
  360. CGSize dialogSize = [self countDialogSize];
  361. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  362. animations:^{
  363. self->dialogView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  364. }
  365. completion:nil
  366. ];
  367. }
  368. @end