SCLAlertView.m 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  1. //
  2. // SCLAlertView.m
  3. // SCLAlertView
  4. //
  5. // Created by Diogo Autilio on 9/26/14.
  6. // Copyright (c) 2014-2017 AnyKey Entertainment. All rights reserved.
  7. //
  8. #import "SCLAlertView.h"
  9. #import "SCLAlertViewResponder.h"
  10. #import "SCLAlertViewStyleKit.h"
  11. #import "UIImage+ImageEffects.h"
  12. #import "SCLTimerDisplay.h"
  13. #import "SCLMacros.h"
  14. #if defined(__has_feature) && __has_feature(modules)
  15. @import AVFoundation;
  16. @import AudioToolbox;
  17. #else
  18. #import <AVFoundation/AVFoundation.h>
  19. #import <AudioToolbox/AudioToolbox.h>
  20. #endif
  21. #define KEYBOARD_HEIGHT 80
  22. #define PREDICTION_BAR_HEIGHT 40
  23. #define ADD_BUTTON_PADDING 10.0f
  24. #define DEFAULT_WINDOW_WIDTH 240
  25. @interface SCLAlertView () <UITextFieldDelegate, UIGestureRecognizerDelegate>
  26. @property (strong, nonatomic) NSMutableArray *inputs;
  27. @property (strong, nonatomic) NSMutableArray *customViews;
  28. @property (strong, nonatomic) NSMutableArray *buttons;
  29. @property (strong, nonatomic) UIImageView *circleIconImageView;
  30. @property (strong, nonatomic) UIView *circleView;
  31. @property (strong, nonatomic) UIView *circleViewBackground;
  32. @property (strong, nonatomic) UIView *contentView;
  33. @property (strong, nonatomic) UIImageView *backgroundView;
  34. @property (strong, nonatomic) UITapGestureRecognizer *gestureRecognizer;
  35. @property (strong, nonatomic) NSString *titleFontFamily;
  36. @property (strong, nonatomic) NSString *bodyTextFontFamily;
  37. @property (strong, nonatomic) NSString *buttonsFontFamily;
  38. @property (strong, nonatomic) UIWindow *previousWindow;
  39. @property (strong, nonatomic) UIWindow *SCLAlertWindow;
  40. @property (copy, nonatomic) SCLDismissBlock dismissBlock;
  41. @property (copy, nonatomic) SCLDismissAnimationCompletionBlock dismissAnimationCompletionBlock;
  42. @property (copy, nonatomic) SCLShowAnimationCompletionBlock showAnimationCompletionBlock;
  43. @property (weak, nonatomic) UIViewController *rootViewController;
  44. @property (weak, nonatomic) id<UIGestureRecognizerDelegate> restoreInteractivePopGestureDelegate;
  45. @property (assign, nonatomic) SystemSoundID soundID;
  46. @property (assign, nonatomic) BOOL canAddObservers;
  47. @property (assign, nonatomic) BOOL keyboardIsVisible;
  48. @property (assign, nonatomic) BOOL usingNewWindow;
  49. @property (assign, nonatomic) BOOL restoreInteractivePopGestureEnabled;
  50. @property (nonatomic) CGFloat backgroundOpacity;
  51. @property (nonatomic) CGFloat titleFontSize;
  52. @property (nonatomic) CGFloat bodyFontSize;
  53. @property (nonatomic) CGFloat buttonsFontSize;
  54. @property (nonatomic) CGFloat windowHeight;
  55. @property (nonatomic) CGFloat windowWidth;
  56. @property (nonatomic) CGFloat titleHeight;
  57. @property (nonatomic) CGFloat subTitleHeight;
  58. @property (nonatomic) CGFloat subTitleY;
  59. @end
  60. @implementation SCLAlertView
  61. CGFloat kCircleHeight;
  62. CGFloat kCircleTopPosition;
  63. CGFloat kCircleBackgroundTopPosition;
  64. CGFloat kCircleHeightBackground;
  65. CGFloat kActivityIndicatorHeight;
  66. CGFloat kTitleTop;
  67. // Timer
  68. NSTimer *durationTimer;
  69. SCLTimerDisplay *buttonTimer;
  70. #pragma mark - Initialization
  71. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  72. {
  73. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  74. reason:@"NSCoding not supported"
  75. userInfo:nil];
  76. }
  77. - (instancetype)init
  78. {
  79. self = [super init];
  80. if (self)
  81. {
  82. [self setupViewWindowWidth:DEFAULT_WINDOW_WIDTH];
  83. }
  84. return self;
  85. }
  86. - (instancetype)initWithWindowWidth:(CGFloat)windowWidth
  87. {
  88. self = [super init];
  89. if (self)
  90. {
  91. [self setupViewWindowWidth:windowWidth];
  92. }
  93. return self;
  94. }
  95. - (instancetype)initWithNewWindow
  96. {
  97. self = [self initWithWindowWidth:DEFAULT_WINDOW_WIDTH];
  98. if(self)
  99. {
  100. [self setupNewWindow];
  101. }
  102. return self;
  103. }
  104. - (instancetype)initWithNewWindowWidth:(CGFloat)windowWidth
  105. {
  106. self = [self initWithWindowWidth:windowWidth];
  107. if(self)
  108. {
  109. [self setupNewWindow];
  110. }
  111. return self;
  112. }
  113. - (void)dealloc
  114. {
  115. [self removeObservers];
  116. [self restoreInteractivePopGesture];
  117. }
  118. - (void)addObservers
  119. {
  120. if(_canAddObservers)
  121. {
  122. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  123. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  124. _canAddObservers = NO;
  125. }
  126. }
  127. - (void)removeObservers
  128. {
  129. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  130. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  131. }
  132. #pragma mark - Setup view
  133. - (void)setupViewWindowWidth:(CGFloat)windowWidth
  134. {
  135. // Default values
  136. kCircleBackgroundTopPosition = -15.0f;
  137. kCircleHeight = 56.0f;
  138. kCircleHeightBackground = 62.0f;
  139. kActivityIndicatorHeight = 40.0f;
  140. kTitleTop = 30.0f;
  141. self.titleHeight = 40.0f;
  142. self.subTitleY = 70.0f;
  143. self.subTitleHeight = 90.0f;
  144. self.circleIconHeight = 20.0f;
  145. self.windowWidth = windowWidth;
  146. self.windowHeight = 178.0f;
  147. self.shouldDismissOnTapOutside = NO;
  148. self.usingNewWindow = NO;
  149. self.canAddObservers = YES;
  150. self.keyboardIsVisible = NO;
  151. self.hideAnimationType = SCLAlertViewHideAnimationFadeOut;
  152. self.showAnimationType = SCLAlertViewShowAnimationSlideInFromTop;
  153. self.backgroundType = SCLAlertViewBackgroundShadow;
  154. self.tintTopCircle = YES;
  155. // Font
  156. _titleFontFamily = @"HelveticaNeue";
  157. _bodyTextFontFamily = @"HelveticaNeue";
  158. _buttonsFontFamily = @"HelveticaNeue-Bold";
  159. _titleFontSize = 20.0f;
  160. _bodyFontSize = 14.0f;
  161. _buttonsFontSize = 14.0f;
  162. // Init
  163. _labelTitle = [[UILabel alloc] init];
  164. _viewText = [[UITextView alloc] init];
  165. _viewText.accessibilityTraits = UIAccessibilityTraitStaticText;
  166. _contentView = [[UIView alloc] init];
  167. _circleView = [[UIView alloc] init];
  168. _circleViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kCircleHeightBackground, kCircleHeightBackground)];
  169. _circleIconImageView = [[UIImageView alloc] init];
  170. _backgroundView = [[UIImageView alloc] initWithFrame:[self mainScreenFrame]];
  171. _buttons = [[NSMutableArray alloc] init];
  172. _inputs = [[NSMutableArray alloc] init];
  173. _customViews = [[NSMutableArray alloc] init];
  174. self.view.accessibilityViewIsModal = YES;
  175. // Add Subviews
  176. [self.view addSubview:_contentView];
  177. [self.view addSubview:_circleViewBackground];
  178. // Circle View
  179. CGFloat x = (kCircleHeightBackground - kCircleHeight) / 2;
  180. _circleView.frame = CGRectMake(x, x, kCircleHeight, kCircleHeight);
  181. _circleView.layer.cornerRadius = _circleView.frame.size.height / 2;
  182. // Circle Background View
  183. _circleViewBackground.backgroundColor = [UIColor whiteColor];
  184. _circleViewBackground.layer.cornerRadius = _circleViewBackground.frame.size.height / 2;
  185. x = (kCircleHeight - _circleIconHeight) / 2;
  186. // Circle Image View
  187. _circleIconImageView.frame = CGRectMake(x, x, _circleIconHeight, _circleIconHeight);
  188. _circleIconImageView.contentMode = UIViewContentModeScaleAspectFill;
  189. [_circleViewBackground addSubview:_circleView];
  190. [_circleView addSubview:_circleIconImageView];
  191. // Background View
  192. _backgroundView.userInteractionEnabled = YES;
  193. // Title
  194. _labelTitle.numberOfLines = 2;
  195. _labelTitle.lineBreakMode = NSLineBreakByWordWrapping;
  196. _labelTitle.textAlignment = NSTextAlignmentCenter;
  197. _labelTitle.font = [UIFont fontWithName:_titleFontFamily size:_titleFontSize];
  198. _labelTitle.frame = CGRectMake(12.0f, kTitleTop, _windowWidth - 24.0f, _titleHeight);
  199. // View text
  200. _viewText.editable = NO;
  201. _viewText.allowsEditingTextAttributes = YES;
  202. _viewText.textAlignment = NSTextAlignmentCenter;
  203. _viewText.font = [UIFont fontWithName:_bodyTextFontFamily size:_bodyFontSize];
  204. _viewText.frame = CGRectMake(12.0f, _subTitleY, _windowWidth - 24.0f, _subTitleHeight);
  205. _viewText.textContainerInset = UIEdgeInsetsZero;
  206. _viewText.textContainer.lineFragmentPadding = 0;
  207. self.automaticallyAdjustsScrollViewInsets = NO;
  208. // Content View
  209. _contentView.backgroundColor = [UIColor whiteColor];
  210. _contentView.layer.cornerRadius = 5.0f;
  211. _contentView.layer.masksToBounds = YES;
  212. _contentView.layer.borderWidth = 0.5f;
  213. [_contentView addSubview:_viewText];
  214. [_contentView addSubview:_labelTitle];
  215. // Colors
  216. self.backgroundViewColor = [UIColor whiteColor];
  217. _labelTitle.textColor = UIColorFromHEX(0x4D4D4D); //Dark Grey
  218. _viewText.textColor = UIColorFromHEX(0x4D4D4D); //Dark Grey
  219. _contentView.layer.borderColor = UIColorFromHEX(0xCCCCCC).CGColor; //Light Grey
  220. }
  221. - (void)setupNewWindow {
  222. // Save previous window
  223. self.previousWindow = [UIApplication sharedApplication].keyWindow;
  224. // Create a new one to show the alert
  225. UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[self mainScreenFrame]];
  226. alertWindow.windowLevel = UIWindowLevelAlert;
  227. alertWindow.backgroundColor = [UIColor clearColor];
  228. alertWindow.rootViewController = [UIViewController new];
  229. alertWindow.accessibilityViewIsModal = YES;
  230. self.SCLAlertWindow = alertWindow;
  231. self.usingNewWindow = YES;
  232. }
  233. #pragma mark - Modal Validation
  234. - (BOOL)isModal {
  235. return (_rootViewController != nil && _rootViewController.presentingViewController);
  236. }
  237. #pragma mark - View Cycle
  238. - (void)viewWillLayoutSubviews
  239. {
  240. [super viewWillLayoutSubviews];
  241. CGSize sz = [self mainScreenFrame].size;
  242. // Check for larger top circle icon flag
  243. if (_useLargerIcon) {
  244. // Adjust icon
  245. _circleIconHeight = 70.0f;
  246. // Adjust coordinate variables for larger sized top circle
  247. kCircleBackgroundTopPosition = -61.0f;
  248. kCircleHeight = 106.0f;
  249. kCircleHeightBackground = 122.0f;
  250. // Reposition inner circle appropriately
  251. CGFloat x = (kCircleHeightBackground - kCircleHeight) / 2;
  252. _circleView.frame = CGRectMake(x, x, kCircleHeight, kCircleHeight);
  253. if (_labelTitle.text == nil) {
  254. kTitleTop = kCircleHeightBackground / 2;
  255. }
  256. } else {
  257. kCircleBackgroundTopPosition = -(kCircleHeightBackground / 2);
  258. }
  259. // Check if the rootViewController is modal, if so we need to get the modal size not the main screen size
  260. if ([self isModal] && !_usingNewWindow) {
  261. sz = _rootViewController.view.frame.size;
  262. }
  263. // Set new main frame
  264. CGRect r;
  265. if (self.view.superview != nil) {
  266. // View is showing, position at center of screen
  267. r = CGRectMake((sz.width-_windowWidth)/2, (sz.height-_windowHeight)/2, _windowWidth, _windowHeight);
  268. } else {
  269. // View is not visible, position outside screen bounds
  270. r = CGRectMake((sz.width-_windowWidth)/2, -_windowHeight, _windowWidth, _windowHeight);
  271. }
  272. self.view.frame = r;
  273. // Set new background frame
  274. CGRect newBackgroundFrame = self.backgroundView.frame;
  275. newBackgroundFrame.size = sz;
  276. self.backgroundView.frame = newBackgroundFrame;
  277. // Set frames
  278. _contentView.frame = CGRectMake(0.0f, 0.0f, _windowWidth, _windowHeight);
  279. _circleViewBackground.frame = CGRectMake(_windowWidth / 2 - kCircleHeightBackground / 2, kCircleBackgroundTopPosition, kCircleHeightBackground, kCircleHeightBackground);
  280. _circleViewBackground.layer.cornerRadius = _circleViewBackground.frame.size.height / 2;
  281. _circleView.layer.cornerRadius = _circleView.frame.size.height / 2;
  282. _circleIconImageView.frame = CGRectMake(kCircleHeight / 2 - _circleIconHeight / 2, kCircleHeight / 2 - _circleIconHeight / 2, _circleIconHeight, _circleIconHeight);
  283. _labelTitle.frame = CGRectMake(12.0f, kTitleTop, _windowWidth - 24.0f, _titleHeight);
  284. // Text fields
  285. CGFloat y = (_labelTitle.text == nil) ? kTitleTop : (_titleHeight - 10.0f) + _labelTitle.frame.size.height;
  286. _viewText.frame = CGRectMake(12.0f, y, _windowWidth - 24.0f, _subTitleHeight);
  287. if (!_labelTitle && !_viewText) {
  288. y = 0.0f;
  289. }
  290. y += _subTitleHeight + 14.0f;
  291. for (SCLTextView *textField in _inputs) {
  292. textField.frame = CGRectMake(12.0f, y, _windowWidth - 24.0f, textField.frame.size.height);
  293. textField.layer.cornerRadius = 3.0f;
  294. y += textField.frame.size.height + 10.0f;
  295. }
  296. // Custom views
  297. for (UIView *view in _customViews) {
  298. view.frame = CGRectMake(12.0f, y, view.frame.size.width, view.frame.size.height);
  299. y += view.frame.size.height + 10.0f;
  300. }
  301. // Buttons
  302. CGFloat x = 12.0f;
  303. for (SCLButton *btn in _buttons) {
  304. btn.frame = CGRectMake(x, y, btn.frame.size.width, btn.frame.size.height);
  305. // Add horizontal or vertical offset acording on _horizontalButtons parameter
  306. if (_horizontalButtons) {
  307. x += btn.frame.size.width + 10.0f;
  308. } else {
  309. y += btn.frame.size.height + 10.0f;
  310. }
  311. }
  312. // Adapt window height according to icon size
  313. self.windowHeight = _useLargerIcon ? y : self.windowHeight;
  314. _contentView.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y, _windowWidth, _windowHeight);
  315. // Adjust corner radius, if a value has been passed
  316. _contentView.layer.cornerRadius = self.cornerRadius ? self.cornerRadius : 5.0f;
  317. }
  318. #pragma mark - UIViewController
  319. - (BOOL)prefersStatusBarHidden
  320. {
  321. return self.statusBarHidden;
  322. }
  323. - (UIStatusBarStyle)preferredStatusBarStyle
  324. {
  325. return self.statusBarStyle;
  326. }
  327. #pragma mark - Handle gesture
  328. - (void)handleTap:(UITapGestureRecognizer *)gesture
  329. {
  330. if (_shouldDismissOnTapOutside)
  331. {
  332. BOOL hide = _shouldDismissOnTapOutside;
  333. for(SCLTextView *txt in _inputs)
  334. {
  335. // Check if there is any keyboard on screen and dismiss
  336. if (txt.editing)
  337. {
  338. [txt resignFirstResponder];
  339. hide = NO;
  340. }
  341. }
  342. if(hide)
  343. {
  344. [self hideView];
  345. }
  346. }
  347. }
  348. - (void)setShouldDismissOnTapOutside:(BOOL)shouldDismissOnTapOutside
  349. {
  350. _shouldDismissOnTapOutside = shouldDismissOnTapOutside;
  351. if(_shouldDismissOnTapOutside)
  352. {
  353. self.gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
  354. [_backgroundView addGestureRecognizer:_gestureRecognizer];
  355. }
  356. }
  357. - (void)disableInteractivePopGesture
  358. {
  359. UINavigationController *navigationController;
  360. if([_rootViewController isKindOfClass:[UINavigationController class]])
  361. {
  362. navigationController = ((UINavigationController*)_rootViewController);
  363. }
  364. else
  365. {
  366. navigationController = _rootViewController.navigationController;
  367. }
  368. // Disable iOS 7 back gesture
  369. if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
  370. {
  371. _restoreInteractivePopGestureEnabled = navigationController.interactivePopGestureRecognizer.enabled;
  372. _restoreInteractivePopGestureDelegate = navigationController.interactivePopGestureRecognizer.delegate;
  373. navigationController.interactivePopGestureRecognizer.enabled = NO;
  374. navigationController.interactivePopGestureRecognizer.delegate = self;
  375. }
  376. }
  377. - (void)restoreInteractivePopGesture
  378. {
  379. UINavigationController *navigationController;
  380. if([_rootViewController isKindOfClass:[UINavigationController class]])
  381. {
  382. navigationController = ((UINavigationController*)_rootViewController);
  383. }
  384. else
  385. {
  386. navigationController = _rootViewController.navigationController;
  387. }
  388. // Restore iOS 7 back gesture
  389. if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
  390. {
  391. navigationController.interactivePopGestureRecognizer.enabled = _restoreInteractivePopGestureEnabled;
  392. navigationController.interactivePopGestureRecognizer.delegate = _restoreInteractivePopGestureDelegate;
  393. }
  394. }
  395. #pragma mark - Custom Fonts
  396. - (void)setTitleFontFamily:(NSString *)titleFontFamily withSize:(CGFloat)size
  397. {
  398. self.titleFontFamily = titleFontFamily;
  399. self.titleFontSize = size;
  400. self.labelTitle.font = [UIFont fontWithName:_titleFontFamily size:_titleFontSize];
  401. }
  402. - (void)setBodyTextFontFamily:(NSString *)bodyTextFontFamily withSize:(CGFloat)size
  403. {
  404. self.bodyTextFontFamily = bodyTextFontFamily;
  405. self.bodyFontSize = size;
  406. self.viewText.font = [UIFont fontWithName:_bodyTextFontFamily size:_bodyFontSize];
  407. }
  408. - (void)setButtonsTextFontFamily:(NSString *)buttonsFontFamily withSize:(CGFloat)size
  409. {
  410. self.buttonsFontFamily = buttonsFontFamily;
  411. self.buttonsFontSize = size;
  412. }
  413. #pragma mark - Background Color
  414. - (void)setBackgroundViewColor:(UIColor *)backgroundViewColor
  415. {
  416. _backgroundViewColor = backgroundViewColor;
  417. _circleViewBackground.backgroundColor = _backgroundViewColor;
  418. _contentView.backgroundColor = _backgroundViewColor;
  419. _viewText.backgroundColor = _backgroundViewColor;
  420. }
  421. #pragma mark - Sound
  422. - (void)setSoundURL:(NSURL *)soundURL
  423. {
  424. _soundURL = soundURL;
  425. //DisposeSound
  426. AudioServicesDisposeSystemSoundID(_soundID);
  427. AudioServicesCreateSystemSoundID((__bridge CFURLRef)_soundURL, &_soundID);
  428. //PlaySound
  429. AudioServicesPlaySystemSound(_soundID);
  430. }
  431. #pragma mark - Subtitle Height
  432. - (void)setSubTitleHeight:(CGFloat)value
  433. {
  434. _subTitleHeight = value;
  435. }
  436. #pragma mark - ActivityIndicator
  437. - (void)addActivityIndicatorView
  438. {
  439. // Add UIActivityIndicatorView
  440. _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  441. _activityIndicatorView.frame = CGRectMake(kCircleHeight / 2 - kActivityIndicatorHeight / 2, kCircleHeight / 2 - kActivityIndicatorHeight / 2, kActivityIndicatorHeight, kActivityIndicatorHeight);
  442. [_circleView addSubview:_activityIndicatorView];
  443. }
  444. #pragma mark - UICustomView
  445. - (UIView *)addCustomView:(UIView *)customView
  446. {
  447. // Update view height
  448. self.windowHeight += customView.bounds.size.height + 10.0f;
  449. [_contentView addSubview:customView];
  450. [_customViews addObject:customView];
  451. return customView;
  452. }
  453. #pragma mark - SwitchView
  454. - (SCLSwitchView *)addSwitchViewWithLabel:(NSString *)label
  455. {
  456. // Add switch view
  457. SCLSwitchView *switchView = [[SCLSwitchView alloc] initWithFrame:CGRectMake(0, 0, self.windowWidth, 31.0f)];
  458. // Update view height
  459. self.windowHeight += switchView.bounds.size.height + 10.0f;
  460. if (label != nil)
  461. {
  462. switchView.labelText = label;
  463. }
  464. [_contentView addSubview:switchView];
  465. [_inputs addObject:switchView];
  466. return switchView;
  467. }
  468. #pragma mark - TextField
  469. - (SCLTextView *)addTextField:(NSString *)title
  470. {
  471. [self addObservers];
  472. // Add text field
  473. SCLTextView *txt = [[SCLTextView alloc] init];
  474. txt.font = [UIFont fontWithName:_bodyTextFontFamily size:_bodyFontSize];
  475. txt.delegate = self;
  476. // Update view height
  477. self.windowHeight += txt.bounds.size.height + 10.0f;
  478. if (title != nil)
  479. {
  480. txt.placeholder = title;
  481. }
  482. [_contentView addSubview:txt];
  483. [_inputs addObject:txt];
  484. // If there are other fields in the inputs array, get the previous field and set the
  485. // return key type on that to next.
  486. if (_inputs.count > 1)
  487. {
  488. NSUInteger indexOfCurrentField = [_inputs indexOfObject:txt];
  489. SCLTextView *priorField = _inputs[indexOfCurrentField - 1];
  490. priorField.returnKeyType = UIReturnKeyNext;
  491. }
  492. return txt;
  493. }
  494. - (void)addCustomTextField:(UITextField *)textField
  495. {
  496. // Update view height
  497. self.windowHeight += textField.bounds.size.height + 10.0f;
  498. [_contentView addSubview:textField];
  499. [_inputs addObject:textField];
  500. // If there are other fields in the inputs array, get the previous field and set the
  501. // return key type on that to next.
  502. if (_inputs.count > 1)
  503. {
  504. NSUInteger indexOfCurrentField = [_inputs indexOfObject:textField];
  505. UITextField *priorField = _inputs[indexOfCurrentField - 1];
  506. priorField.returnKeyType = UIReturnKeyNext;
  507. }
  508. }
  509. # pragma mark - UITextFieldDelegate
  510. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  511. {
  512. // If this is the last object in the inputs array, resign first responder
  513. // as the form is at the end.
  514. if (textField == _inputs.lastObject)
  515. {
  516. [textField resignFirstResponder];
  517. }
  518. else // Otherwise find the next field and make it first responder.
  519. {
  520. NSUInteger indexOfCurrentField = [_inputs indexOfObject:textField];
  521. UITextField *nextField = _inputs[indexOfCurrentField + 1];
  522. [nextField becomeFirstResponder];
  523. }
  524. return NO;
  525. }
  526. - (void)keyboardWillShow:(NSNotification *)notification
  527. {
  528. if(_keyboardIsVisible) return;
  529. [UIView animateWithDuration:0.2f animations:^{
  530. CGRect f = self.view.frame;
  531. f.origin.y -= KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT;
  532. self.view.frame = f;
  533. }];
  534. _keyboardIsVisible = YES;
  535. }
  536. - (void)keyboardWillHide:(NSNotification *)notification
  537. {
  538. if(!_keyboardIsVisible) return;
  539. [UIView animateWithDuration:0.2f animations:^{
  540. CGRect f = self.view.frame;
  541. f.origin.y += KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT;
  542. self.view.frame = f;
  543. }];
  544. _keyboardIsVisible = NO;
  545. }
  546. #pragma mark - Buttons
  547. - (SCLButton *)addButton:(NSString *)title
  548. {
  549. // Add button
  550. SCLButton *btn = [[SCLButton alloc] initWithWindowWidth:self.windowWidth];
  551. btn.layer.masksToBounds = YES;
  552. [btn setTitle:title forState:UIControlStateNormal];
  553. btn.titleLabel.font = [UIFont fontWithName:_buttonsFontFamily size:_buttonsFontSize];
  554. [_contentView addSubview:btn];
  555. [_buttons addObject:btn];
  556. if (_horizontalButtons) {
  557. // Update buttons width according to the number of buttons
  558. for (SCLButton *bttn in _buttons) {
  559. [bttn adjustWidthWithWindowWidth:self.windowWidth numberOfButtons:[_buttons count]];
  560. }
  561. // Update view height
  562. if (!([_buttons count] > 1)) {
  563. self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING);
  564. }
  565. } else {
  566. // Update view height
  567. self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING);
  568. }
  569. return btn;
  570. }
  571. - (SCLButton *)addDoneButtonWithTitle:(NSString *)title
  572. {
  573. SCLButton *btn = [self addButton:title];
  574. if (_completeButtonFormatBlock != nil)
  575. {
  576. btn.completeButtonFormatBlock = _completeButtonFormatBlock;
  577. }
  578. [btn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
  579. return btn;
  580. }
  581. - (SCLButton *)addButton:(NSString *)title actionBlock:(SCLActionBlock)action
  582. {
  583. SCLButton *btn = [self addButton:title];
  584. if (_buttonFormatBlock != nil)
  585. {
  586. btn.buttonFormatBlock = _buttonFormatBlock;
  587. }
  588. btn.actionType = SCLBlock;
  589. btn.actionBlock = action;
  590. [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  591. return btn;
  592. }
  593. - (SCLButton *)addButton:(NSString *)title validationBlock:(SCLValidationBlock)validationBlock actionBlock:(SCLActionBlock)action
  594. {
  595. SCLButton *btn = [self addButton:title actionBlock:action];
  596. btn.validationBlock = validationBlock;
  597. return btn;
  598. }
  599. - (SCLButton *)addButton:(NSString *)title target:(id)target selector:(SEL)selector
  600. {
  601. SCLButton *btn = [self addButton:title];
  602. btn.actionType = SCLSelector;
  603. btn.target = target;
  604. btn.selector = selector;
  605. [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  606. return btn;
  607. }
  608. - (void)buttonTapped:(SCLButton *)btn
  609. {
  610. // Cancel Countdown timer
  611. [buttonTimer cancelTimer];
  612. // If the button has a validation block, and the validation block returns NO, validation
  613. // failed, so we should bail.
  614. if (btn.validationBlock && !btn.validationBlock()) {
  615. return;
  616. }
  617. if (btn.actionType == SCLBlock)
  618. {
  619. if (btn.actionBlock)
  620. btn.actionBlock();
  621. }
  622. else if (btn.actionType == SCLSelector)
  623. {
  624. UIControl *ctrl = [[UIControl alloc] init];
  625. [ctrl sendAction:btn.selector to:btn.target forEvent:nil];
  626. }
  627. else
  628. {
  629. NSLog(@"Unknown action type for button");
  630. }
  631. if([self isVisible])
  632. {
  633. [self hideView];
  634. }
  635. }
  636. #pragma mark - Button Timer
  637. - (void)addTimerToButtonIndex:(NSInteger)buttonIndex reverse:(BOOL)reverse
  638. {
  639. buttonIndex = MAX(buttonIndex, 0);
  640. buttonIndex = MIN(buttonIndex, [_buttons count]);
  641. buttonTimer = [[SCLTimerDisplay alloc] initWithOrigin:CGPointMake(5, 5) radius:13 lineWidth:4];
  642. buttonTimer.buttonIndex = buttonIndex;
  643. buttonTimer.reverse = reverse;
  644. }
  645. #pragma mark - Show Alert
  646. - (SCLAlertViewResponder *)showTitle:(UIViewController *)vc image:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle duration:(NSTimeInterval)duration completeText:(NSString *)completeText style:(SCLAlertViewStyle)style
  647. {
  648. if(_usingNewWindow) {
  649. self.backgroundView.frame = _SCLAlertWindow.bounds;
  650. // Add window subview
  651. [_SCLAlertWindow.rootViewController addChildViewController:self];
  652. [_SCLAlertWindow.rootViewController.view addSubview:_backgroundView];
  653. [_SCLAlertWindow.rootViewController.view addSubview:self.view];
  654. } else {
  655. _rootViewController = vc;
  656. [self disableInteractivePopGesture];
  657. self.backgroundView.frame = vc.view.bounds;
  658. // Add view controller subviews
  659. [_rootViewController addChildViewController:self];
  660. [_rootViewController.view addSubview:_backgroundView];
  661. [_rootViewController.view addSubview:self.view];
  662. }
  663. self.view.alpha = 0.0f;
  664. [self setBackground];
  665. // Alert color/icon
  666. UIColor *viewColor;
  667. UIImage *iconImage;
  668. // Icon style
  669. switch (style)
  670. {
  671. case SCLAlertViewStyleSuccess:
  672. viewColor = UIColorFromHEX(0x22B573);
  673. iconImage = SCLAlertViewStyleKit.imageOfCheckmark;
  674. break;
  675. case SCLAlertViewStyleError:
  676. viewColor = UIColorFromHEX(0xC1272D);
  677. iconImage = SCLAlertViewStyleKit.imageOfCross;
  678. break;
  679. case SCLAlertViewStyleNotice:
  680. viewColor = UIColorFromHEX(0x727375);
  681. iconImage = SCLAlertViewStyleKit.imageOfNotice;
  682. break;
  683. case SCLAlertViewStyleWarning:
  684. viewColor = UIColorFromHEX(0xFFD110);
  685. iconImage = SCLAlertViewStyleKit.imageOfWarning;
  686. break;
  687. case SCLAlertViewStyleInfo:
  688. viewColor = UIColorFromHEX(0x2866BF);
  689. iconImage = SCLAlertViewStyleKit.imageOfInfo;
  690. break;
  691. case SCLAlertViewStyleEdit:
  692. viewColor = UIColorFromHEX(0xA429FF);
  693. iconImage = SCLAlertViewStyleKit.imageOfEdit;
  694. break;
  695. case SCLAlertViewStyleWaiting:
  696. viewColor = UIColorFromHEX(0x6c125d);
  697. break;
  698. case SCLAlertViewStyleQuestion:
  699. viewColor = UIColorFromHEX(0x727375);
  700. iconImage = SCLAlertViewStyleKit.imageOfQuestion;
  701. break;
  702. case SCLAlertViewStyleCustom:
  703. viewColor = color;
  704. iconImage = image;
  705. self.circleIconHeight *= 2.0f;
  706. break;
  707. }
  708. // Custom Alert color
  709. if(_customViewColor)
  710. {
  711. viewColor = _customViewColor;
  712. }
  713. // Title
  714. if ([title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0) {
  715. self.labelTitle.text = title;
  716. // Adjust text view size, if necessary
  717. CGSize sz = CGSizeMake(_windowWidth - 24.0f, CGFLOAT_MAX);
  718. CGSize size = [_labelTitle sizeThatFits:sz];
  719. CGFloat ht = ceilf(size.height);
  720. if (ht > _titleHeight) {
  721. self.windowHeight += (ht - _titleHeight);
  722. self.titleHeight = ht;
  723. self.subTitleY += 20;
  724. }
  725. } else {
  726. // Title is nil, we can move the body message to center and remove it from superView
  727. self.windowHeight -= _labelTitle.frame.size.height;
  728. [_labelTitle removeFromSuperview];
  729. _labelTitle = nil;
  730. _subTitleY = kCircleHeight - 20;
  731. }
  732. // Subtitle
  733. if ([subTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0) {
  734. // No custom text
  735. if (_attributedFormatBlock == nil) {
  736. _viewText.text = subTitle;
  737. } else {
  738. self.viewText.font = [UIFont fontWithName:_bodyTextFontFamily size:_bodyFontSize];
  739. _viewText.attributedText = self.attributedFormatBlock(subTitle);
  740. }
  741. // Adjust text view size, if necessary
  742. CGSize sz = CGSizeMake(_windowWidth - 24.0f, CGFLOAT_MAX);
  743. CGSize size = [_viewText sizeThatFits:sz];
  744. CGFloat ht = ceilf(size.height);
  745. if (ht < _subTitleHeight) {
  746. self.windowHeight -= (_subTitleHeight - ht);
  747. self.subTitleHeight = ht;
  748. } else {
  749. self.windowHeight += (ht - _subTitleHeight);
  750. self.subTitleHeight = ht;
  751. }
  752. } else {
  753. // Subtitle is nil, we can move the title to center and remove it from superView
  754. self.subTitleHeight = 0.0f;
  755. self.windowHeight -= _viewText.frame.size.height;
  756. [_viewText removeFromSuperview];
  757. _viewText = nil;
  758. // Move up
  759. _labelTitle.frame = CGRectMake(12.0f, 37.0f, _windowWidth - 24.0f, _titleHeight);
  760. }
  761. if (!_labelTitle && !_viewText) {
  762. self.windowHeight -= kTitleTop;
  763. }
  764. // Add button, if necessary
  765. if(completeText != nil)
  766. {
  767. [self addDoneButtonWithTitle:completeText];
  768. }
  769. // Alert view color and images
  770. self.circleView.backgroundColor = self.tintTopCircle ? viewColor : _backgroundViewColor;
  771. if (style == SCLAlertViewStyleWaiting)
  772. {
  773. [self.activityIndicatorView startAnimating];
  774. }
  775. else
  776. {
  777. if (self.iconTintColor) {
  778. self.circleIconImageView.tintColor = self.iconTintColor;
  779. iconImage = [iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  780. }
  781. self.circleIconImageView.image = iconImage;
  782. }
  783. for (SCLTextView *textField in _inputs)
  784. {
  785. textField.layer.borderColor = viewColor.CGColor;
  786. }
  787. for (SCLButton *btn in _buttons)
  788. {
  789. if (style == SCLAlertViewStyleWarning)
  790. {
  791. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  792. }
  793. if (!btn.defaultBackgroundColor) {
  794. btn.defaultBackgroundColor = viewColor;
  795. }
  796. if (btn.completeButtonFormatBlock != nil)
  797. {
  798. [btn parseConfig:btn.completeButtonFormatBlock()];
  799. }
  800. else if (btn.buttonFormatBlock != nil)
  801. {
  802. [btn parseConfig:btn.buttonFormatBlock()];
  803. }
  804. }
  805. // Adding duration
  806. if (duration > 0)
  807. {
  808. [durationTimer invalidate];
  809. if (buttonTimer && _buttons.count > 0)
  810. {
  811. SCLButton *btn = _buttons[buttonTimer.buttonIndex];
  812. btn.timer = buttonTimer;
  813. __weak __typeof(self) weakSelf = self;
  814. [buttonTimer startTimerWithTimeLimit:duration completed:^{
  815. [weakSelf buttonTapped:btn];
  816. }];
  817. }
  818. else
  819. {
  820. durationTimer = [NSTimer scheduledTimerWithTimeInterval:duration
  821. target:self
  822. selector:@selector(hideView)
  823. userInfo:nil
  824. repeats:NO];
  825. }
  826. }
  827. if(_usingNewWindow)
  828. {
  829. [_SCLAlertWindow makeKeyAndVisible];
  830. }
  831. // Show the alert view
  832. [self showView];
  833. // Chainable objects
  834. return [[SCLAlertViewResponder alloc] init:self];
  835. }
  836. #pragma mark - Show using UIViewController
  837. - (void)showSuccess:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  838. {
  839. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleSuccess];
  840. }
  841. - (void)showError:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  842. {
  843. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleError];
  844. }
  845. - (void)showNotice:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  846. {
  847. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleNotice];
  848. }
  849. - (void)showWarning:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  850. {
  851. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWarning];
  852. }
  853. - (void)showInfo:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  854. {
  855. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleInfo];
  856. }
  857. - (void)showEdit:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  858. {
  859. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleEdit];
  860. }
  861. - (void)showTitle:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle style:(SCLAlertViewStyle)style closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  862. {
  863. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:style];
  864. }
  865. - (void)showCustom:(UIViewController *)vc image:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  866. {
  867. [self showTitle:vc image:image color:color title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleCustom];
  868. }
  869. - (void)showWaiting:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  870. {
  871. [self addActivityIndicatorView];
  872. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWaiting];
  873. }
  874. - (void)showQuestion:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  875. {
  876. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleQuestion];
  877. }
  878. #pragma mark - Show using new window
  879. - (void)showSuccess:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  880. {
  881. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleSuccess];
  882. }
  883. - (void)showError:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  884. {
  885. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleError];
  886. }
  887. - (void)showNotice:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  888. {
  889. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleNotice];
  890. }
  891. - (void)showWarning:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  892. {
  893. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWarning];
  894. }
  895. - (void)showInfo:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  896. {
  897. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleInfo];
  898. }
  899. - (void)showEdit:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  900. {
  901. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleEdit];
  902. }
  903. - (void)showTitle:(NSString *)title subTitle:(NSString *)subTitle style:(SCLAlertViewStyle)style closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  904. {
  905. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:style];
  906. }
  907. - (void)showCustom:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  908. {
  909. [self showTitle:nil image:image color:color title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleCustom];
  910. }
  911. - (void)showWaiting:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  912. {
  913. [self addActivityIndicatorView];
  914. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWaiting];
  915. }
  916. - (void)showQuestion:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  917. {
  918. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleQuestion];
  919. }
  920. #pragma mark - Visibility
  921. - (void)removeTopCircle
  922. {
  923. [_circleViewBackground removeFromSuperview];
  924. [_circleView removeFromSuperview];
  925. }
  926. - (BOOL)isVisible
  927. {
  928. return (self.view.alpha);
  929. }
  930. - (void)alertIsDismissed:(SCLDismissBlock)dismissBlock
  931. {
  932. self.dismissBlock = dismissBlock;
  933. }
  934. - (void)alertDismissAnimationIsCompleted:(SCLDismissAnimationCompletionBlock)dismissAnimationCompletionBlock{
  935. self.dismissAnimationCompletionBlock = dismissAnimationCompletionBlock;
  936. }
  937. - (void)alertShowAnimationIsCompleted:(SCLShowAnimationCompletionBlock)showAnimationCompletionBlock{
  938. self.showAnimationCompletionBlock = showAnimationCompletionBlock;
  939. }
  940. - (SCLForceHideBlock)forceHideBlock:(SCLForceHideBlock)forceHideBlock
  941. {
  942. _forceHideBlock = forceHideBlock;
  943. if (_forceHideBlock)
  944. {
  945. [self hideView];
  946. }
  947. return _forceHideBlock;
  948. }
  949. - (CGRect)mainScreenFrame
  950. {
  951. return [self isAppExtension] ? _extensionBounds : [UIApplication sharedApplication].keyWindow.bounds;
  952. }
  953. - (BOOL)isAppExtension
  954. {
  955. return [[NSBundle mainBundle].executablePath rangeOfString:@".appex/"].location != NSNotFound;
  956. }
  957. #pragma mark - Background Effects
  958. - (void)makeShadowBackground
  959. {
  960. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  961. _backgroundView.backgroundColor = [UIColor blackColor];
  962. _backgroundView.alpha = 0.7f;
  963. _backgroundOpacity = 0.7f;
  964. }
  965. - (void)makeBlurBackground
  966. {
  967. UIView *appView = (_usingNewWindow) ? [UIApplication sharedApplication].keyWindow.subviews.lastObject : _rootViewController.view;
  968. UIImage *image = [UIImage convertViewToImage:appView];
  969. UIImage *blurSnapshotImage = [image applyBlurWithRadius:5.0f
  970. tintColor:[UIColor colorWithWhite:0.2f
  971. alpha:0.7f]
  972. saturationDeltaFactor:1.8f
  973. maskImage:nil];
  974. _backgroundView.image = blurSnapshotImage;
  975. _backgroundView.alpha = 0.0f;
  976. _backgroundOpacity = 1.0f;
  977. }
  978. - (void)makeTransparentBackground
  979. {
  980. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  981. _backgroundView.backgroundColor = [UIColor clearColor];
  982. _backgroundView.alpha = 0.0f;
  983. _backgroundOpacity = 1.0f;
  984. }
  985. - (void)setBackground
  986. {
  987. switch (_backgroundType)
  988. {
  989. case SCLAlertViewBackgroundShadow:
  990. [self makeShadowBackground];
  991. break;
  992. case SCLAlertViewBackgroundBlur:
  993. [self makeBlurBackground];
  994. break;
  995. case SCLAlertViewBackgroundTransparent:
  996. [self makeTransparentBackground];
  997. break;
  998. }
  999. }
  1000. #pragma mark - Show Alert
  1001. - (void)showView
  1002. {
  1003. switch (_showAnimationType)
  1004. {
  1005. case SCLAlertViewShowAnimationFadeIn:
  1006. [self fadeIn];
  1007. break;
  1008. case SCLAlertViewShowAnimationSlideInFromBottom:
  1009. [self slideInFromBottom];
  1010. break;
  1011. case SCLAlertViewShowAnimationSlideInFromTop:
  1012. [self slideInFromTop];
  1013. break;
  1014. case SCLAlertViewShowAnimationSlideInFromLeft:
  1015. [self slideInFromLeft];
  1016. break;
  1017. case SCLAlertViewShowAnimationSlideInFromRight:
  1018. [self slideInFromRight];
  1019. break;
  1020. case SCLAlertViewShowAnimationSlideInFromCenter:
  1021. [self slideInFromCenter];
  1022. break;
  1023. case SCLAlertViewShowAnimationSlideInToCenter:
  1024. [self slideInToCenter];
  1025. break;
  1026. case SCLAlertViewShowAnimationSimplyAppear:
  1027. [self simplyAppear];
  1028. break;
  1029. }
  1030. }
  1031. #pragma mark - Hide Alert
  1032. - (void)hideView
  1033. {
  1034. switch (_hideAnimationType)
  1035. {
  1036. case SCLAlertViewHideAnimationFadeOut:
  1037. [self fadeOut];
  1038. break;
  1039. case SCLAlertViewHideAnimationSlideOutToBottom:
  1040. [self slideOutToBottom];
  1041. break;
  1042. case SCLAlertViewHideAnimationSlideOutToTop:
  1043. [self slideOutToTop];
  1044. break;
  1045. case SCLAlertViewHideAnimationSlideOutToLeft:
  1046. [self slideOutToLeft];
  1047. break;
  1048. case SCLAlertViewHideAnimationSlideOutToRight:
  1049. [self slideOutToRight];
  1050. break;
  1051. case SCLAlertViewHideAnimationSlideOutToCenter:
  1052. [self slideOutToCenter];
  1053. break;
  1054. case SCLAlertViewHideAnimationSlideOutFromCenter:
  1055. [self slideOutFromCenter];
  1056. break;
  1057. case SCLAlertViewHideAnimationSimplyDisappear:
  1058. [self simplyDisappear];
  1059. break;
  1060. }
  1061. if (_activityIndicatorView)
  1062. {
  1063. [_activityIndicatorView stopAnimating];
  1064. }
  1065. if (durationTimer)
  1066. {
  1067. [durationTimer invalidate];
  1068. }
  1069. if (self.dismissBlock)
  1070. {
  1071. self.dismissBlock();
  1072. }
  1073. if (_usingNewWindow)
  1074. {
  1075. // Restore previous window
  1076. [self.previousWindow makeKeyAndVisible];
  1077. self.previousWindow = nil;
  1078. }
  1079. for (SCLButton *btn in _buttons)
  1080. {
  1081. btn.actionBlock = nil;
  1082. btn.target = nil;
  1083. btn.selector = nil;
  1084. }
  1085. }
  1086. #pragma mark - Hide Animations
  1087. - (void)fadeOut
  1088. {
  1089. [self fadeOutWithDuration:0.3f];
  1090. }
  1091. - (void)fadeOutWithDuration:(NSTimeInterval)duration
  1092. {
  1093. [UIView animateWithDuration:duration animations:^{
  1094. self.backgroundView.alpha = 0.0f;
  1095. self.view.alpha = 0.0f;
  1096. } completion:^(BOOL completed) {
  1097. [self.backgroundView removeFromSuperview];
  1098. [self.view removeFromSuperview];
  1099. [self removeFromParentViewController];
  1100. if (self.usingNewWindow) {
  1101. // Remove current window
  1102. [self.SCLAlertWindow setHidden:YES];
  1103. self.SCLAlertWindow = nil;
  1104. }
  1105. if ( self.dismissAnimationCompletionBlock ){
  1106. self.dismissAnimationCompletionBlock();
  1107. }
  1108. }];
  1109. }
  1110. - (void)slideOutToBottom
  1111. {
  1112. [UIView animateWithDuration:0.3f animations:^{
  1113. CGRect frame = self.view.frame;
  1114. frame.origin.y += self.backgroundView.frame.size.height;
  1115. self.view.frame = frame;
  1116. } completion:^(BOOL completed) {
  1117. [self fadeOut];
  1118. }];
  1119. }
  1120. - (void)slideOutToTop
  1121. {
  1122. [UIView animateWithDuration:0.3f animations:^{
  1123. CGRect frame = self.view.frame;
  1124. frame.origin.y -= self.backgroundView.frame.size.height;
  1125. self.view.frame = frame;
  1126. } completion:^(BOOL completed) {
  1127. [self fadeOut];
  1128. }];
  1129. }
  1130. - (void)slideOutToLeft
  1131. {
  1132. [UIView animateWithDuration:0.3f animations:^{
  1133. CGRect frame = self.view.frame;
  1134. frame.origin.x -= self.backgroundView.frame.size.width;
  1135. self.view.frame = frame;
  1136. } completion:^(BOOL completed) {
  1137. [self fadeOut];
  1138. }];
  1139. }
  1140. - (void)slideOutToRight
  1141. {
  1142. [UIView animateWithDuration:0.3f animations:^{
  1143. CGRect frame = self.view.frame;
  1144. frame.origin.x += self.backgroundView.frame.size.width;
  1145. self.view.frame = frame;
  1146. } completion:^(BOOL completed) {
  1147. [self fadeOut];
  1148. }];
  1149. }
  1150. - (void)slideOutToCenter
  1151. {
  1152. [UIView animateWithDuration:0.3f animations:^{
  1153. self.view.transform =
  1154. CGAffineTransformConcat(CGAffineTransformIdentity,
  1155. CGAffineTransformMakeScale(0.1f, 0.1f));
  1156. self.view.alpha = 0.0f;
  1157. } completion:^(BOOL completed) {
  1158. [self fadeOut];
  1159. }];
  1160. }
  1161. - (void)slideOutFromCenter
  1162. {
  1163. [UIView animateWithDuration:0.3f animations:^{
  1164. self.view.transform =
  1165. CGAffineTransformConcat(CGAffineTransformIdentity,
  1166. CGAffineTransformMakeScale(3.0f, 3.0f));
  1167. self.view.alpha = 0.0f;
  1168. } completion:^(BOOL completed) {
  1169. [self fadeOut];
  1170. }];
  1171. }
  1172. - (void)simplyDisappear
  1173. {
  1174. self.backgroundView.alpha = self.backgroundOpacity;
  1175. self.view.alpha = 1.0f;
  1176. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1177. [self fadeOutWithDuration:0];
  1178. });
  1179. }
  1180. #pragma mark - Show Animations
  1181. - (void)fadeIn
  1182. {
  1183. self.backgroundView.alpha = 0.0f;
  1184. self.view.alpha = 0.0f;
  1185. [UIView animateWithDuration:0.3f
  1186. delay:0.0f
  1187. options:UIViewAnimationOptionCurveEaseIn
  1188. animations:^{
  1189. self.backgroundView.alpha = self.backgroundOpacity;
  1190. self.view.alpha = 1.0f;
  1191. }
  1192. completion:^(BOOL finished) {
  1193. if ( self.showAnimationCompletionBlock ){
  1194. self.showAnimationCompletionBlock();
  1195. }
  1196. }];
  1197. }
  1198. - (void)slideInFromTop
  1199. {
  1200. //From Frame
  1201. CGRect frame = self.backgroundView.frame;
  1202. frame.origin.y = -self.backgroundView.frame.size.height;
  1203. self.view.frame = frame;
  1204. [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.6f initialSpringVelocity:0.5f options:0 animations:^{
  1205. self.backgroundView.alpha = self.backgroundOpacity;
  1206. //To Frame
  1207. CGRect frame = self.backgroundView.frame;
  1208. frame.origin.y = 0.0f;
  1209. self.view.frame = frame;
  1210. self.view.alpha = 1.0f;
  1211. } completion:^(BOOL finished) {
  1212. if ( self.showAnimationCompletionBlock ){
  1213. self.showAnimationCompletionBlock();
  1214. }
  1215. }];
  1216. }
  1217. - (void)slideInFromBottom
  1218. {
  1219. //From Frame
  1220. CGRect frame = self.backgroundView.frame;
  1221. frame.origin.y = self.backgroundView.frame.size.height;
  1222. self.view.frame = frame;
  1223. [UIView animateWithDuration:0.3f animations:^{
  1224. self.backgroundView.alpha = self.backgroundOpacity;
  1225. //To Frame
  1226. CGRect frame = self.backgroundView.frame;
  1227. frame.origin.y = 0.0f;
  1228. self.view.frame = frame;
  1229. self.view.alpha = 1.0f;
  1230. } completion:^(BOOL completed) {
  1231. [UIView animateWithDuration:0.2f animations:^{
  1232. self.view.center = self.backgroundView.center;
  1233. } completion:^(BOOL finished) {
  1234. if ( self.showAnimationCompletionBlock ){
  1235. self.showAnimationCompletionBlock();
  1236. }
  1237. }];
  1238. }];
  1239. }
  1240. - (void)slideInFromLeft
  1241. {
  1242. //From Frame
  1243. CGRect frame = self.backgroundView.frame;
  1244. frame.origin.x = -self.backgroundView.frame.size.width;
  1245. self.view.frame = frame;
  1246. [UIView animateWithDuration:0.3f animations:^{
  1247. self.backgroundView.alpha = self.backgroundOpacity;
  1248. //To Frame
  1249. CGRect frame = self.backgroundView.frame;
  1250. frame.origin.x = 0.0f;
  1251. self.view.frame = frame;
  1252. self.view.alpha = 1.0f;
  1253. } completion:^(BOOL completed) {
  1254. [UIView animateWithDuration:0.2f animations:^{
  1255. self.view.center = self.backgroundView.center;
  1256. } completion:^(BOOL finished) {
  1257. if ( self.showAnimationCompletionBlock ){
  1258. self.showAnimationCompletionBlock();
  1259. }
  1260. }];
  1261. }];
  1262. }
  1263. - (void)slideInFromRight
  1264. {
  1265. //From Frame
  1266. CGRect frame = self.backgroundView.frame;
  1267. frame.origin.x = self.backgroundView.frame.size.width;
  1268. self.view.frame = frame;
  1269. [UIView animateWithDuration:0.3f animations:^{
  1270. self.backgroundView.alpha = self.backgroundOpacity;
  1271. //To Frame
  1272. CGRect frame = self.backgroundView.frame;
  1273. frame.origin.x = 0.0f;
  1274. self.view.frame = frame;
  1275. self.view.alpha = 1.0f;
  1276. } completion:^(BOOL completed) {
  1277. [UIView animateWithDuration:0.2f animations:^{
  1278. self.view.center = self.backgroundView.center;
  1279. } completion:^(BOOL finished) {
  1280. if ( self.showAnimationCompletionBlock ){
  1281. self.showAnimationCompletionBlock();
  1282. }
  1283. }];
  1284. }];
  1285. }
  1286. - (void)slideInFromCenter
  1287. {
  1288. //From Frame
  1289. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1290. CGAffineTransformMakeScale(3.0f, 3.0f));
  1291. self.view.alpha = 0.0f;
  1292. [UIView animateWithDuration:0.3f animations:^{
  1293. self.backgroundView.alpha = self.backgroundOpacity;
  1294. //To Frame
  1295. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1296. CGAffineTransformMakeScale(1.0f, 1.0f));
  1297. self.view.alpha = 1.0f;
  1298. } completion:^(BOOL completed) {
  1299. [UIView animateWithDuration:0.2f animations:^{
  1300. self.view.center = self.backgroundView.center;
  1301. } completion:^(BOOL finished) {
  1302. if ( self.showAnimationCompletionBlock ){
  1303. self.showAnimationCompletionBlock();
  1304. }
  1305. }];
  1306. }];
  1307. }
  1308. - (void)slideInToCenter
  1309. {
  1310. //From Frame
  1311. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1312. CGAffineTransformMakeScale(0.1f, 0.1f));
  1313. self.view.alpha = 0.0f;
  1314. [UIView animateWithDuration:0.3f animations:^{
  1315. self.backgroundView.alpha = self.backgroundOpacity;
  1316. //To Frame
  1317. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1318. CGAffineTransformMakeScale(1.0f, 1.0f));
  1319. self.view.alpha = 1.0f;
  1320. } completion:^(BOOL completed) {
  1321. [UIView animateWithDuration:0.2f animations:^{
  1322. self.view.center = self.backgroundView.center;
  1323. } completion:^(BOOL finished) {
  1324. if ( self.showAnimationCompletionBlock ){
  1325. self.showAnimationCompletionBlock();
  1326. }
  1327. }];
  1328. }];
  1329. }
  1330. - (void)simplyAppear
  1331. {
  1332. self.backgroundView.alpha = 0.0f;
  1333. self.view.alpha = 0.0f;
  1334. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1335. self.backgroundView.alpha = self.backgroundOpacity;
  1336. self.view.alpha = 1.0f;
  1337. if ( self.showAnimationCompletionBlock ){
  1338. self.showAnimationCompletionBlock();
  1339. }
  1340. });
  1341. }
  1342. @end
  1343. @implementation SCLAlertViewBuilder__WithFluent
  1344. - (instancetype)init {
  1345. if (self = [super init]) {
  1346. [self setupFluent];
  1347. }
  1348. return self;
  1349. }
  1350. - (void)setupFluent {}
  1351. @end
  1352. @interface SCLALertViewTextFieldBuilder()
  1353. #pragma mark - Parameters
  1354. @property(copy, nonatomic) NSString *parameterTitle;
  1355. #pragma mark - Available later after adding
  1356. @property(weak, nonatomic) SCLTextView *textField;
  1357. #pragma mark - Setters
  1358. @property(copy, nonatomic) SCLALertViewTextFieldBuilder *(^title) (NSString *title);
  1359. @end
  1360. @implementation SCLALertViewTextFieldBuilder
  1361. - (void)setupFluent {
  1362. __weak __auto_type weakSelf = self;
  1363. self.title = ^(NSString *title){
  1364. weakSelf.parameterTitle = title;
  1365. return weakSelf;
  1366. };
  1367. }
  1368. @end
  1369. @interface SCLALertViewButtonBuilder()
  1370. #pragma mark - Parameters
  1371. @property(copy, nonatomic) NSString *parameterTitle;
  1372. @property(weak, nonatomic) id parameterTarget;
  1373. @property(assign, nonatomic) SEL parameterSelector;
  1374. @property(copy, nonatomic) void(^parameterActionBlock)(void);
  1375. @property(copy, nonatomic) BOOL(^parameterValidationBlock)(void);
  1376. #pragma mark - Available later after adding
  1377. @property(weak, nonatomic) SCLButton *button;
  1378. #pragma mark - Setters
  1379. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^title) (NSString *title);
  1380. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^target) (id target);
  1381. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^selector) (SEL selector);
  1382. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^actionBlock) (void(^actionBlock)(void));
  1383. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^validationBlock) (BOOL(^validationBlock)(void));
  1384. @end
  1385. @implementation SCLALertViewButtonBuilder
  1386. - (void)setupFluent {
  1387. __weak __auto_type weakSelf = self;
  1388. self.title = ^(NSString *title){
  1389. weakSelf.parameterTitle = title;
  1390. return weakSelf;
  1391. };
  1392. self.target = ^(id target){
  1393. weakSelf.parameterTarget = target;
  1394. return weakSelf;
  1395. };
  1396. self.selector = ^(SEL selector){
  1397. weakSelf.parameterSelector = selector;
  1398. return weakSelf;
  1399. };
  1400. self.actionBlock = ^(void(^actionBlock)(void)){
  1401. weakSelf.parameterActionBlock = actionBlock;
  1402. return weakSelf;
  1403. };
  1404. self.validationBlock = ^(BOOL(^validationBlock)(void)){
  1405. weakSelf.parameterValidationBlock = validationBlock;
  1406. return weakSelf;
  1407. };
  1408. }
  1409. @end
  1410. @interface SCLAlertViewBuilder()
  1411. @property (strong, nonatomic) SCLAlertView *alertView;
  1412. @end
  1413. @implementation SCLAlertViewBuilder
  1414. - (void)setupFluent {
  1415. __weak __auto_type weakSelf = self;
  1416. self.cornerRadius = ^(CGFloat cornerRadius) {
  1417. weakSelf.alertView.cornerRadius = cornerRadius;
  1418. return weakSelf;
  1419. };
  1420. self.tintTopCircle = ^(BOOL tintTopCircle) {
  1421. weakSelf.alertView.tintTopCircle = tintTopCircle;
  1422. return weakSelf;
  1423. };
  1424. self.useLargerIcon = ^(BOOL useLargerIcon) {
  1425. weakSelf.alertView.useLargerIcon = useLargerIcon;
  1426. return weakSelf;
  1427. };
  1428. self.labelTitle = ^(UILabel *labelTitle) {
  1429. weakSelf.alertView.labelTitle = labelTitle;
  1430. return weakSelf;
  1431. };
  1432. self.viewText = ^(UITextView *viewText) {
  1433. weakSelf.alertView.viewText = viewText;
  1434. return weakSelf;
  1435. };
  1436. self.activityIndicatorView = ^(UIActivityIndicatorView *activityIndicatorView) {
  1437. weakSelf.alertView.activityIndicatorView = activityIndicatorView;
  1438. return weakSelf;
  1439. };
  1440. self.shouldDismissOnTapOutside = ^(BOOL shouldDismissOnTapOutside) {
  1441. weakSelf.alertView.shouldDismissOnTapOutside = shouldDismissOnTapOutside;
  1442. return weakSelf;
  1443. };
  1444. self.soundURL = ^(NSURL *soundURL) {
  1445. weakSelf.alertView.soundURL = soundURL;
  1446. return weakSelf;
  1447. };
  1448. self.attributedFormatBlock = ^(SCLAttributedFormatBlock attributedFormatBlock) {
  1449. weakSelf.alertView.attributedFormatBlock = attributedFormatBlock;
  1450. return weakSelf;
  1451. };
  1452. self.completeButtonFormatBlock = ^(CompleteButtonFormatBlock completeButtonFormatBlock) {
  1453. weakSelf.alertView.completeButtonFormatBlock = completeButtonFormatBlock;
  1454. return weakSelf;
  1455. };
  1456. self.buttonFormatBlock = ^(ButtonFormatBlock buttonFormatBlock) {
  1457. weakSelf.alertView.buttonFormatBlock = buttonFormatBlock;
  1458. return weakSelf;
  1459. };
  1460. self.forceHideBlock = ^(SCLForceHideBlock forceHideBlock) {
  1461. weakSelf.alertView.forceHideBlock = forceHideBlock;
  1462. return weakSelf;
  1463. };
  1464. self.hideAnimationType = ^(SCLAlertViewHideAnimation hideAnimationType) {
  1465. weakSelf.alertView.hideAnimationType = hideAnimationType;
  1466. return weakSelf;
  1467. };
  1468. self.showAnimationType = ^(SCLAlertViewShowAnimation showAnimationType) {
  1469. weakSelf.alertView.showAnimationType = showAnimationType;
  1470. return weakSelf;
  1471. };
  1472. self.backgroundType = ^(SCLAlertViewBackground backgroundType) {
  1473. weakSelf.alertView.backgroundType = backgroundType;
  1474. return weakSelf;
  1475. };
  1476. self.customViewColor = ^(UIColor *customViewColor) {
  1477. weakSelf.alertView.customViewColor = customViewColor;
  1478. return weakSelf;
  1479. };
  1480. self.backgroundViewColor = ^(UIColor *backgroundViewColor) {
  1481. weakSelf.alertView.backgroundViewColor = backgroundViewColor;
  1482. return weakSelf;
  1483. };
  1484. self.iconTintColor = ^(UIColor *iconTintColor) {
  1485. weakSelf.alertView.iconTintColor = iconTintColor;
  1486. return weakSelf;
  1487. };
  1488. self.circleIconHeight = ^(CGFloat circleIconHeight) {
  1489. weakSelf.alertView.circleIconHeight = circleIconHeight;
  1490. return weakSelf;
  1491. };
  1492. self.extensionBounds = ^(CGRect extensionBounds) {
  1493. weakSelf.alertView.extensionBounds = extensionBounds;
  1494. return weakSelf;
  1495. };
  1496. self.statusBarHidden = ^(BOOL statusBarHidden) {
  1497. weakSelf.alertView.statusBarHidden = statusBarHidden;
  1498. return weakSelf;
  1499. };
  1500. self.statusBarStyle = ^(UIStatusBarStyle statusBarStyle) {
  1501. weakSelf.alertView.statusBarStyle = statusBarStyle;
  1502. return weakSelf;
  1503. };
  1504. self.alertIsDismissed = ^(SCLDismissBlock dismissBlock) {
  1505. [weakSelf.alertView alertIsDismissed:dismissBlock];
  1506. return weakSelf;
  1507. };
  1508. self.alertDismissAnimationIsCompleted = ^(SCLDismissAnimationCompletionBlock dismissAnimationCompletionBlock) {
  1509. [weakSelf.alertView alertDismissAnimationIsCompleted:dismissAnimationCompletionBlock];
  1510. return weakSelf;
  1511. };
  1512. self.alertShowAnimationIsCompleted = ^(SCLShowAnimationCompletionBlock showAnimationCompletionBlock) {
  1513. [weakSelf.alertView alertShowAnimationIsCompleted:showAnimationCompletionBlock];
  1514. return weakSelf;
  1515. };
  1516. self.removeTopCircle = ^(void) {
  1517. [weakSelf.alertView removeTopCircle];
  1518. return weakSelf;
  1519. };
  1520. self.addCustomView = ^(UIView *view) {
  1521. [weakSelf.alertView addCustomView:view];
  1522. return weakSelf;
  1523. };
  1524. self.addTextField = ^(NSString *title) {
  1525. [weakSelf.alertView addTextField:title];
  1526. return weakSelf;
  1527. };
  1528. self.addCustomTextField = ^(UITextField *textField) {
  1529. [weakSelf.alertView addCustomTextField:textField];
  1530. return weakSelf;
  1531. };
  1532. self.addSwitchViewWithLabelTitle = ^(NSString *title) {
  1533. [weakSelf.alertView addSwitchViewWithLabel:title];
  1534. return weakSelf;
  1535. };
  1536. self.addTimerToButtonIndex = ^(NSInteger buttonIndex, BOOL reverse) {
  1537. [weakSelf.alertView addTimerToButtonIndex:buttonIndex reverse:reverse];
  1538. return weakSelf;
  1539. };
  1540. self.setTitleFontFamily = ^(NSString *titleFontFamily, CGFloat size) {
  1541. [weakSelf.alertView setTitleFontFamily:titleFontFamily withSize:size];
  1542. return weakSelf;
  1543. };
  1544. self.setBodyTextFontFamily = ^(NSString *bodyTextFontFamily, CGFloat size) {
  1545. [weakSelf.alertView setBodyTextFontFamily:bodyTextFontFamily withSize:size];
  1546. return weakSelf;
  1547. };
  1548. self.setButtonsTextFontFamily = ^(NSString *buttonsFontFamily, CGFloat size) {
  1549. [weakSelf.alertView setButtonsTextFontFamily:buttonsFontFamily withSize:size];
  1550. return weakSelf;
  1551. };
  1552. self.addButtonWithActionBlock = ^(NSString *title, SCLActionBlock action) {
  1553. [weakSelf.alertView addButton:title actionBlock:action];
  1554. return weakSelf;
  1555. };
  1556. self.addButtonWithValidationBlock = ^(NSString *title, SCLValidationBlock validationBlock, SCLActionBlock action) {
  1557. [weakSelf.alertView addButton:title validationBlock:validationBlock actionBlock:action];
  1558. return weakSelf;
  1559. };
  1560. self.addButtonWithTarget = ^(NSString *title, id target, SEL selector) {
  1561. [weakSelf.alertView addButton:title target:target selector:selector];
  1562. return weakSelf;
  1563. };
  1564. self.addButtonWithBuilder = ^(SCLALertViewButtonBuilder *builder){
  1565. SCLButton *button = nil;
  1566. if (builder.parameterTarget && builder.parameterSelector) {
  1567. button = [weakSelf.alertView addButton:builder.parameterTitle target:builder.parameterTarget selector:builder.parameterSelector];
  1568. }
  1569. else if (builder.parameterValidationBlock && builder.parameterActionBlock) {
  1570. button = [weakSelf.alertView addButton:builder.parameterTitle validationBlock:builder.parameterValidationBlock actionBlock:builder.parameterActionBlock];
  1571. }
  1572. else if (builder.parameterActionBlock) {
  1573. button = [weakSelf.alertView addButton:builder.parameterTitle actionBlock:builder.parameterActionBlock];
  1574. }
  1575. builder.button = button;
  1576. return weakSelf;
  1577. };
  1578. self.addTextFieldWithBuilder = ^(SCLALertViewTextFieldBuilder *builder){
  1579. builder.textField = [weakSelf.alertView addTextField:builder.parameterTitle];
  1580. return weakSelf;
  1581. };
  1582. }
  1583. #pragma mark - Init
  1584. - (instancetype)init {
  1585. self = [super init];
  1586. if (self) {
  1587. self.alertView = [[SCLAlertView alloc] init];
  1588. }
  1589. return self;
  1590. }
  1591. - (instancetype)initWithNewWindow {
  1592. self = [super init];
  1593. if (self) {
  1594. self.alertView = [[SCLAlertView alloc] initWithNewWindow];
  1595. }
  1596. return self;
  1597. }
  1598. - (instancetype)initWithNewWindowWidth:(CGFloat)width {
  1599. self = [super init];
  1600. if (self) {
  1601. self.alertView = [[SCLAlertView alloc] initWithNewWindowWidth:width];
  1602. }
  1603. return self;
  1604. }
  1605. @end
  1606. @interface SCLAlertViewShowBuilder()
  1607. @property(weak, nonatomic) UIViewController *parameterViewController;
  1608. @property(copy, nonatomic) UIImage *parameterImage;
  1609. @property(copy, nonatomic) UIColor *parameterColor;
  1610. @property(copy, nonatomic) NSString *parameterTitle;
  1611. @property(copy, nonatomic) NSString *parameterSubTitle;
  1612. @property(copy, nonatomic) NSString *parameterCompleteText;
  1613. @property(copy, nonatomic) NSString *parameterCloseButtonTitle;
  1614. @property(assign, nonatomic) SCLAlertViewStyle parameterStyle;
  1615. @property(assign, nonatomic) NSTimeInterval parameterDuration;
  1616. #pragma mark - Setters
  1617. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^viewController)(UIViewController *viewController);
  1618. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^image)(UIImage *image);
  1619. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^color)(UIColor *color);
  1620. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^title)(NSString *title);
  1621. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^subTitle)(NSString *subTitle);
  1622. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^completeText)(NSString *completeText);
  1623. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^style)(SCLAlertViewStyle style);
  1624. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^closeButtonTitle)(NSString *closeButtonTitle);
  1625. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^duration)(NSTimeInterval duration);
  1626. #pragma mark - Show
  1627. @property(copy, nonatomic) void (^show)(SCLAlertView *view, UIViewController *controller);
  1628. @end
  1629. @implementation SCLAlertViewShowBuilder
  1630. - (void)setupFluent {
  1631. __weak __auto_type weakSelf = self;
  1632. self.viewController = ^(UIViewController *viewController){
  1633. weakSelf.parameterViewController = viewController;
  1634. return weakSelf;
  1635. };
  1636. self.image = ^(UIImage *image) {
  1637. weakSelf.parameterImage = image;
  1638. return weakSelf;
  1639. };
  1640. self.color = ^(UIColor *color) {
  1641. weakSelf.parameterColor = color;
  1642. return weakSelf;
  1643. };
  1644. self.title = ^(NSString *title){
  1645. weakSelf.parameterTitle = title;
  1646. return weakSelf;
  1647. };
  1648. self.subTitle = ^(NSString *subTitle){
  1649. weakSelf.parameterSubTitle = subTitle;
  1650. return weakSelf;
  1651. };
  1652. self.completeText = ^(NSString *completeText){
  1653. weakSelf.parameterCompleteText = completeText;
  1654. return weakSelf;
  1655. };
  1656. self.style = ^(SCLAlertViewStyle style){
  1657. weakSelf.parameterStyle = style;
  1658. return weakSelf;
  1659. };
  1660. self.closeButtonTitle = ^(NSString *closeButtonTitle){
  1661. weakSelf.parameterCloseButtonTitle = closeButtonTitle;
  1662. return weakSelf;
  1663. };
  1664. self.duration = ^(NSTimeInterval duration){
  1665. weakSelf.parameterDuration = duration;
  1666. return weakSelf;
  1667. };
  1668. self.show = ^(SCLAlertView *view, UIViewController *controller) {
  1669. [weakSelf showAlertView:view onViewController:controller];
  1670. };
  1671. }
  1672. #pragma mark - Setters
  1673. - (void)showAlertView:(SCLAlertView *)alertView {
  1674. [self showAlertView:alertView onViewController:self.parameterViewController];
  1675. }
  1676. - (void)showAlertView:(SCLAlertView *)alertView onViewController:(UIViewController *)controller {
  1677. UIViewController *targetController = controller ? controller : self.parameterViewController;
  1678. if (self.parameterImage || self.parameterColor) {
  1679. [alertView showTitle:targetController image:self.parameterImage color:self.parameterColor title:self.parameterTitle subTitle:self.parameterSubTitle duration:self.parameterDuration completeText:self.parameterCloseButtonTitle style:self.parameterStyle];
  1680. }
  1681. else {
  1682. [alertView showTitle:targetController title:self.parameterTitle subTitle:self.parameterSubTitle style:self.parameterStyle closeButtonTitle:self.parameterCloseButtonTitle duration:self.parameterDuration];
  1683. }
  1684. }
  1685. @end