CustomIOSAlertView.m 18 KB

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