SCLAlertView.m 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  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 = SCLAlertViewShowAnimationFadeIn;
  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. [buttonTimer startTimerWithTimeLimit:duration completed:^{
  814. [self buttonTapped:btn];
  815. }];
  816. }
  817. else
  818. {
  819. durationTimer = [NSTimer scheduledTimerWithTimeInterval:duration
  820. target:self
  821. selector:@selector(hideView)
  822. userInfo:nil
  823. repeats:NO];
  824. }
  825. }
  826. if(_usingNewWindow)
  827. {
  828. [_SCLAlertWindow makeKeyAndVisible];
  829. }
  830. // Show the alert view
  831. [self showView];
  832. // Chainable objects
  833. return [[SCLAlertViewResponder alloc] init:self];
  834. }
  835. #pragma mark - Show using UIViewController
  836. - (void)showSuccess:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  837. {
  838. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleSuccess];
  839. }
  840. - (void)showError:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  841. {
  842. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleError];
  843. }
  844. - (void)showNotice:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  845. {
  846. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleNotice];
  847. }
  848. - (void)showWarning:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  849. {
  850. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWarning];
  851. }
  852. - (void)showInfo:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  853. {
  854. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleInfo];
  855. }
  856. - (void)showEdit:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  857. {
  858. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleEdit];
  859. }
  860. - (void)showTitle:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle style:(SCLAlertViewStyle)style closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  861. {
  862. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:style];
  863. }
  864. - (void)showCustom:(UIViewController *)vc image:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  865. {
  866. [self showTitle:vc image:image color:color title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleCustom];
  867. }
  868. - (void)showWaiting:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  869. {
  870. [self addActivityIndicatorView];
  871. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWaiting];
  872. }
  873. - (void)showQuestion:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  874. {
  875. [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleQuestion];
  876. }
  877. #pragma mark - Show using new window
  878. - (void)showSuccess:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  879. {
  880. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleSuccess];
  881. }
  882. - (void)showError:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  883. {
  884. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleError];
  885. }
  886. - (void)showNotice:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  887. {
  888. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleNotice];
  889. }
  890. - (void)showWarning:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  891. {
  892. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWarning];
  893. }
  894. - (void)showInfo:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  895. {
  896. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleInfo];
  897. }
  898. - (void)showEdit:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  899. {
  900. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleEdit];
  901. }
  902. - (void)showTitle:(NSString *)title subTitle:(NSString *)subTitle style:(SCLAlertViewStyle)style closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  903. {
  904. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:style];
  905. }
  906. - (void)showCustom:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  907. {
  908. [self showTitle:nil image:image color:color title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleCustom];
  909. }
  910. - (void)showWaiting:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  911. {
  912. [self addActivityIndicatorView];
  913. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleWaiting];
  914. }
  915. - (void)showQuestion:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration
  916. {
  917. [self showTitle:nil image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:SCLAlertViewStyleQuestion];
  918. }
  919. #pragma mark - Visibility
  920. - (void)removeTopCircle
  921. {
  922. [_circleViewBackground removeFromSuperview];
  923. [_circleView removeFromSuperview];
  924. }
  925. - (BOOL)isVisible
  926. {
  927. return (self.view.alpha);
  928. }
  929. - (void)alertIsDismissed:(SCLDismissBlock)dismissBlock
  930. {
  931. self.dismissBlock = dismissBlock;
  932. }
  933. - (void)alertDismissAnimationIsCompleted:(SCLDismissAnimationCompletionBlock)dismissAnimationCompletionBlock{
  934. self.dismissAnimationCompletionBlock = dismissAnimationCompletionBlock;
  935. }
  936. - (void)alertShowAnimationIsCompleted:(SCLShowAnimationCompletionBlock)showAnimationCompletionBlock{
  937. self.showAnimationCompletionBlock = showAnimationCompletionBlock;
  938. }
  939. - (SCLForceHideBlock)forceHideBlock:(SCLForceHideBlock)forceHideBlock
  940. {
  941. _forceHideBlock = forceHideBlock;
  942. if (_forceHideBlock)
  943. {
  944. [self hideView];
  945. }
  946. return _forceHideBlock;
  947. }
  948. - (CGRect)mainScreenFrame
  949. {
  950. return [self isAppExtension] ? _extensionBounds : [UIApplication sharedApplication].keyWindow.bounds;
  951. }
  952. - (BOOL)isAppExtension
  953. {
  954. return [[NSBundle mainBundle].executablePath rangeOfString:@".appex/"].location != NSNotFound;
  955. }
  956. #pragma mark - Background Effects
  957. - (void)makeShadowBackground
  958. {
  959. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  960. _backgroundView.backgroundColor = [UIColor blackColor];
  961. _backgroundView.alpha = 0.7f;
  962. _backgroundOpacity = 0.7f;
  963. }
  964. - (void)makeBlurBackground
  965. {
  966. UIView *appView = (_usingNewWindow) ? [UIApplication sharedApplication].keyWindow.subviews.lastObject : _rootViewController.view;
  967. UIImage *image = [UIImage convertViewToImage:appView];
  968. UIImage *blurSnapshotImage = [image applyBlurWithRadius:5.0f
  969. tintColor:[UIColor colorWithWhite:0.2f
  970. alpha:0.7f]
  971. saturationDeltaFactor:1.8f
  972. maskImage:nil];
  973. _backgroundView.image = blurSnapshotImage;
  974. _backgroundView.alpha = 0.0f;
  975. _backgroundOpacity = 1.0f;
  976. }
  977. - (void)makeTransparentBackground
  978. {
  979. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  980. _backgroundView.backgroundColor = [UIColor clearColor];
  981. _backgroundView.alpha = 0.0f;
  982. _backgroundOpacity = 1.0f;
  983. }
  984. - (void)setBackground
  985. {
  986. switch (_backgroundType)
  987. {
  988. case SCLAlertViewBackgroundShadow:
  989. [self makeShadowBackground];
  990. break;
  991. case SCLAlertViewBackgroundBlur:
  992. [self makeBlurBackground];
  993. break;
  994. case SCLAlertViewBackgroundTransparent:
  995. [self makeTransparentBackground];
  996. break;
  997. }
  998. }
  999. #pragma mark - Show Alert
  1000. - (void)showView
  1001. {
  1002. switch (_showAnimationType)
  1003. {
  1004. case SCLAlertViewShowAnimationFadeIn:
  1005. [self fadeIn];
  1006. break;
  1007. case SCLAlertViewShowAnimationSlideInFromBottom:
  1008. [self slideInFromBottom];
  1009. break;
  1010. case SCLAlertViewShowAnimationSlideInFromTop:
  1011. [self slideInFromTop];
  1012. break;
  1013. case SCLAlertViewShowAnimationSlideInFromLeft:
  1014. [self slideInFromLeft];
  1015. break;
  1016. case SCLAlertViewShowAnimationSlideInFromRight:
  1017. [self slideInFromRight];
  1018. break;
  1019. case SCLAlertViewShowAnimationSlideInFromCenter:
  1020. [self slideInFromCenter];
  1021. break;
  1022. case SCLAlertViewShowAnimationSlideInToCenter:
  1023. [self slideInToCenter];
  1024. break;
  1025. case SCLAlertViewShowAnimationSimplyAppear:
  1026. [self simplyAppear];
  1027. break;
  1028. }
  1029. }
  1030. #pragma mark - Hide Alert
  1031. - (void)hideView
  1032. {
  1033. switch (_hideAnimationType)
  1034. {
  1035. case SCLAlertViewHideAnimationFadeOut:
  1036. [self fadeOut];
  1037. break;
  1038. case SCLAlertViewHideAnimationSlideOutToBottom:
  1039. [self slideOutToBottom];
  1040. break;
  1041. case SCLAlertViewHideAnimationSlideOutToTop:
  1042. [self slideOutToTop];
  1043. break;
  1044. case SCLAlertViewHideAnimationSlideOutToLeft:
  1045. [self slideOutToLeft];
  1046. break;
  1047. case SCLAlertViewHideAnimationSlideOutToRight:
  1048. [self slideOutToRight];
  1049. break;
  1050. case SCLAlertViewHideAnimationSlideOutToCenter:
  1051. [self slideOutToCenter];
  1052. break;
  1053. case SCLAlertViewHideAnimationSlideOutFromCenter:
  1054. [self slideOutFromCenter];
  1055. break;
  1056. case SCLAlertViewHideAnimationSimplyDisappear:
  1057. [self simplyDisappear];
  1058. break;
  1059. }
  1060. if (_activityIndicatorView)
  1061. {
  1062. [_activityIndicatorView stopAnimating];
  1063. }
  1064. if (durationTimer)
  1065. {
  1066. [durationTimer invalidate];
  1067. }
  1068. if (self.dismissBlock)
  1069. {
  1070. self.dismissBlock();
  1071. }
  1072. if (_usingNewWindow)
  1073. {
  1074. // Restore previous window
  1075. [self.previousWindow makeKeyAndVisible];
  1076. self.previousWindow = nil;
  1077. }
  1078. for (SCLButton *btn in _buttons)
  1079. {
  1080. btn.actionBlock = nil;
  1081. btn.target = nil;
  1082. btn.selector = nil;
  1083. }
  1084. }
  1085. #pragma mark - Hide Animations
  1086. - (void)fadeOut
  1087. {
  1088. [self fadeOutWithDuration:0.3f];
  1089. }
  1090. - (void)fadeOutWithDuration:(NSTimeInterval)duration
  1091. {
  1092. [UIView animateWithDuration:duration animations:^{
  1093. self.backgroundView.alpha = 0.0f;
  1094. self.view.alpha = 0.0f;
  1095. } completion:^(BOOL completed) {
  1096. [self.backgroundView removeFromSuperview];
  1097. [self.view removeFromSuperview];
  1098. [self removeFromParentViewController];
  1099. if (self.usingNewWindow) {
  1100. // Remove current window
  1101. [self.SCLAlertWindow setHidden:YES];
  1102. self.SCLAlertWindow = nil;
  1103. }
  1104. if ( self.dismissAnimationCompletionBlock ){
  1105. self.dismissAnimationCompletionBlock();
  1106. }
  1107. }];
  1108. }
  1109. - (void)slideOutToBottom
  1110. {
  1111. [UIView animateWithDuration:0.3f animations:^{
  1112. CGRect frame = self.view.frame;
  1113. frame.origin.y += self.backgroundView.frame.size.height;
  1114. self.view.frame = frame;
  1115. } completion:^(BOOL completed) {
  1116. [self fadeOut];
  1117. }];
  1118. }
  1119. - (void)slideOutToTop
  1120. {
  1121. [UIView animateWithDuration:0.3f animations:^{
  1122. CGRect frame = self.view.frame;
  1123. frame.origin.y -= self.backgroundView.frame.size.height;
  1124. self.view.frame = frame;
  1125. } completion:^(BOOL completed) {
  1126. [self fadeOut];
  1127. }];
  1128. }
  1129. - (void)slideOutToLeft
  1130. {
  1131. [UIView animateWithDuration:0.3f animations:^{
  1132. CGRect frame = self.view.frame;
  1133. frame.origin.x -= self.backgroundView.frame.size.width;
  1134. self.view.frame = frame;
  1135. } completion:^(BOOL completed) {
  1136. [self fadeOut];
  1137. }];
  1138. }
  1139. - (void)slideOutToRight
  1140. {
  1141. [UIView animateWithDuration:0.3f animations:^{
  1142. CGRect frame = self.view.frame;
  1143. frame.origin.x += self.backgroundView.frame.size.width;
  1144. self.view.frame = frame;
  1145. } completion:^(BOOL completed) {
  1146. [self fadeOut];
  1147. }];
  1148. }
  1149. - (void)slideOutToCenter
  1150. {
  1151. [UIView animateWithDuration:0.3f animations:^{
  1152. self.view.transform =
  1153. CGAffineTransformConcat(CGAffineTransformIdentity,
  1154. CGAffineTransformMakeScale(0.1f, 0.1f));
  1155. self.view.alpha = 0.0f;
  1156. } completion:^(BOOL completed) {
  1157. [self fadeOut];
  1158. }];
  1159. }
  1160. - (void)slideOutFromCenter
  1161. {
  1162. [UIView animateWithDuration:0.3f animations:^{
  1163. self.view.transform =
  1164. CGAffineTransformConcat(CGAffineTransformIdentity,
  1165. CGAffineTransformMakeScale(3.0f, 3.0f));
  1166. self.view.alpha = 0.0f;
  1167. } completion:^(BOOL completed) {
  1168. [self fadeOut];
  1169. }];
  1170. }
  1171. - (void)simplyDisappear
  1172. {
  1173. self.backgroundView.alpha = self.backgroundOpacity;
  1174. self.view.alpha = 1.0f;
  1175. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1176. [self fadeOutWithDuration:0];
  1177. });
  1178. }
  1179. #pragma mark - Show Animations
  1180. - (void)fadeIn
  1181. {
  1182. self.backgroundView.alpha = 0.0f;
  1183. self.view.alpha = 0.0f;
  1184. [UIView animateWithDuration:0.3f
  1185. delay:0.0f
  1186. options:UIViewAnimationOptionCurveEaseIn
  1187. animations:^{
  1188. self.backgroundView.alpha = self.backgroundOpacity;
  1189. self.view.alpha = 1.0f;
  1190. }
  1191. completion:^(BOOL finished) {
  1192. if ( self.showAnimationCompletionBlock ){
  1193. self.showAnimationCompletionBlock();
  1194. }
  1195. }];
  1196. }
  1197. - (void)slideInFromTop
  1198. {
  1199. //From Frame
  1200. CGRect frame = self.backgroundView.frame;
  1201. frame.origin.y = -self.backgroundView.frame.size.height;
  1202. self.view.frame = frame;
  1203. [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.6f initialSpringVelocity:0.5f options:0 animations:^{
  1204. self.backgroundView.alpha = self.backgroundOpacity;
  1205. //To Frame
  1206. CGRect frame = self.backgroundView.frame;
  1207. frame.origin.y = 0.0f;
  1208. self.view.frame = frame;
  1209. self.view.alpha = 1.0f;
  1210. } completion:^(BOOL finished) {
  1211. if ( self.showAnimationCompletionBlock ){
  1212. self.showAnimationCompletionBlock();
  1213. }
  1214. }];
  1215. }
  1216. - (void)slideInFromBottom
  1217. {
  1218. //From Frame
  1219. CGRect frame = self.backgroundView.frame;
  1220. frame.origin.y = self.backgroundView.frame.size.height;
  1221. self.view.frame = frame;
  1222. [UIView animateWithDuration:0.3f animations:^{
  1223. self.backgroundView.alpha = self.backgroundOpacity;
  1224. //To Frame
  1225. CGRect frame = self.backgroundView.frame;
  1226. frame.origin.y = 0.0f;
  1227. self.view.frame = frame;
  1228. self.view.alpha = 1.0f;
  1229. } completion:^(BOOL completed) {
  1230. [UIView animateWithDuration:0.2f animations:^{
  1231. self.view.center = self.backgroundView.center;
  1232. } completion:^(BOOL finished) {
  1233. if ( self.showAnimationCompletionBlock ){
  1234. self.showAnimationCompletionBlock();
  1235. }
  1236. }];
  1237. }];
  1238. }
  1239. - (void)slideInFromLeft
  1240. {
  1241. //From Frame
  1242. CGRect frame = self.backgroundView.frame;
  1243. frame.origin.x = -self.backgroundView.frame.size.width;
  1244. self.view.frame = frame;
  1245. [UIView animateWithDuration:0.3f animations:^{
  1246. self.backgroundView.alpha = self.backgroundOpacity;
  1247. //To Frame
  1248. CGRect frame = self.backgroundView.frame;
  1249. frame.origin.x = 0.0f;
  1250. self.view.frame = frame;
  1251. self.view.alpha = 1.0f;
  1252. } completion:^(BOOL completed) {
  1253. [UIView animateWithDuration:0.2f animations:^{
  1254. self.view.center = self.backgroundView.center;
  1255. } completion:^(BOOL finished) {
  1256. if ( self.showAnimationCompletionBlock ){
  1257. self.showAnimationCompletionBlock();
  1258. }
  1259. }];
  1260. }];
  1261. }
  1262. - (void)slideInFromRight
  1263. {
  1264. //From Frame
  1265. CGRect frame = self.backgroundView.frame;
  1266. frame.origin.x = self.backgroundView.frame.size.width;
  1267. self.view.frame = frame;
  1268. [UIView animateWithDuration:0.3f animations:^{
  1269. self.backgroundView.alpha = self.backgroundOpacity;
  1270. //To Frame
  1271. CGRect frame = self.backgroundView.frame;
  1272. frame.origin.x = 0.0f;
  1273. self.view.frame = frame;
  1274. self.view.alpha = 1.0f;
  1275. } completion:^(BOOL completed) {
  1276. [UIView animateWithDuration:0.2f animations:^{
  1277. self.view.center = self.backgroundView.center;
  1278. } completion:^(BOOL finished) {
  1279. if ( self.showAnimationCompletionBlock ){
  1280. self.showAnimationCompletionBlock();
  1281. }
  1282. }];
  1283. }];
  1284. }
  1285. - (void)slideInFromCenter
  1286. {
  1287. //From Frame
  1288. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1289. CGAffineTransformMakeScale(3.0f, 3.0f));
  1290. self.view.alpha = 0.0f;
  1291. [UIView animateWithDuration:0.3f animations:^{
  1292. self.backgroundView.alpha = self.backgroundOpacity;
  1293. //To Frame
  1294. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1295. CGAffineTransformMakeScale(1.0f, 1.0f));
  1296. self.view.alpha = 1.0f;
  1297. } completion:^(BOOL completed) {
  1298. [UIView animateWithDuration:0.2f animations:^{
  1299. self.view.center = self.backgroundView.center;
  1300. } completion:^(BOOL finished) {
  1301. if ( self.showAnimationCompletionBlock ){
  1302. self.showAnimationCompletionBlock();
  1303. }
  1304. }];
  1305. }];
  1306. }
  1307. - (void)slideInToCenter
  1308. {
  1309. //From Frame
  1310. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1311. CGAffineTransformMakeScale(0.1f, 0.1f));
  1312. self.view.alpha = 0.0f;
  1313. [UIView animateWithDuration:0.3f animations:^{
  1314. self.backgroundView.alpha = self.backgroundOpacity;
  1315. //To Frame
  1316. self.view.transform = CGAffineTransformConcat(CGAffineTransformIdentity,
  1317. CGAffineTransformMakeScale(1.0f, 1.0f));
  1318. self.view.alpha = 1.0f;
  1319. } completion:^(BOOL completed) {
  1320. [UIView animateWithDuration:0.2f animations:^{
  1321. self.view.center = self.backgroundView.center;
  1322. } completion:^(BOOL finished) {
  1323. if ( self.showAnimationCompletionBlock ){
  1324. self.showAnimationCompletionBlock();
  1325. }
  1326. }];
  1327. }];
  1328. }
  1329. - (void)simplyAppear
  1330. {
  1331. self.backgroundView.alpha = 0.0f;
  1332. self.view.alpha = 0.0f;
  1333. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1334. self.backgroundView.alpha = self.backgroundOpacity;
  1335. self.view.alpha = 1.0f;
  1336. if ( self.showAnimationCompletionBlock ){
  1337. self.showAnimationCompletionBlock();
  1338. }
  1339. });
  1340. }
  1341. @end
  1342. @implementation SCLAlertViewBuilder__WithFluent
  1343. - (instancetype)init {
  1344. if (self = [super init]) {
  1345. [self setupFluent];
  1346. }
  1347. return self;
  1348. }
  1349. - (void)setupFluent {}
  1350. @end
  1351. @interface SCLALertViewTextFieldBuilder()
  1352. #pragma mark - Parameters
  1353. @property(copy, nonatomic) NSString *parameterTitle;
  1354. #pragma mark - Available later after adding
  1355. @property(weak, nonatomic) SCLTextView *textField;
  1356. #pragma mark - Setters
  1357. @property(copy, nonatomic) SCLALertViewTextFieldBuilder *(^title) (NSString *title);
  1358. @end
  1359. @implementation SCLALertViewTextFieldBuilder
  1360. - (void)setupFluent {
  1361. __weak __auto_type weakSelf = self;
  1362. self.title = ^(NSString *title){
  1363. weakSelf.parameterTitle = title;
  1364. return weakSelf;
  1365. };
  1366. }
  1367. @end
  1368. @interface SCLALertViewButtonBuilder()
  1369. #pragma mark - Parameters
  1370. @property(copy, nonatomic) NSString *parameterTitle;
  1371. @property(weak, nonatomic) id parameterTarget;
  1372. @property(assign, nonatomic) SEL parameterSelector;
  1373. @property(copy, nonatomic) void(^parameterActionBlock)(void);
  1374. @property(copy, nonatomic) BOOL(^parameterValidationBlock)(void);
  1375. #pragma mark - Available later after adding
  1376. @property(weak, nonatomic) SCLButton *button;
  1377. #pragma mark - Setters
  1378. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^title) (NSString *title);
  1379. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^target) (id target);
  1380. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^selector) (SEL selector);
  1381. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^actionBlock) (void(^actionBlock)(void));
  1382. @property(copy, nonatomic) SCLALertViewButtonBuilder *(^validationBlock) (BOOL(^validationBlock)(void));
  1383. @end
  1384. @implementation SCLALertViewButtonBuilder
  1385. - (void)setupFluent {
  1386. __weak __auto_type weakSelf = self;
  1387. self.title = ^(NSString *title){
  1388. weakSelf.parameterTitle = title;
  1389. return weakSelf;
  1390. };
  1391. self.target = ^(id target){
  1392. weakSelf.parameterTarget = target;
  1393. return weakSelf;
  1394. };
  1395. self.selector = ^(SEL selector){
  1396. weakSelf.parameterSelector = selector;
  1397. return weakSelf;
  1398. };
  1399. self.actionBlock = ^(void(^actionBlock)(void)){
  1400. weakSelf.parameterActionBlock = actionBlock;
  1401. return weakSelf;
  1402. };
  1403. self.validationBlock = ^(BOOL(^validationBlock)(void)){
  1404. weakSelf.parameterValidationBlock = validationBlock;
  1405. return weakSelf;
  1406. };
  1407. }
  1408. @end
  1409. @interface SCLAlertViewBuilder()
  1410. @property (strong, nonatomic) SCLAlertView *alertView;
  1411. @end
  1412. @implementation SCLAlertViewBuilder
  1413. - (void)setupFluent {
  1414. __weak __auto_type weakSelf = self;
  1415. self.cornerRadius = ^(CGFloat cornerRadius) {
  1416. weakSelf.alertView.cornerRadius = cornerRadius;
  1417. return weakSelf;
  1418. };
  1419. self.tintTopCircle = ^(BOOL tintTopCircle) {
  1420. weakSelf.alertView.tintTopCircle = tintTopCircle;
  1421. return weakSelf;
  1422. };
  1423. self.useLargerIcon = ^(BOOL useLargerIcon) {
  1424. weakSelf.alertView.useLargerIcon = useLargerIcon;
  1425. return weakSelf;
  1426. };
  1427. self.labelTitle = ^(UILabel *labelTitle) {
  1428. weakSelf.alertView.labelTitle = labelTitle;
  1429. return weakSelf;
  1430. };
  1431. self.viewText = ^(UITextView *viewText) {
  1432. weakSelf.alertView.viewText = viewText;
  1433. return weakSelf;
  1434. };
  1435. self.activityIndicatorView = ^(UIActivityIndicatorView *activityIndicatorView) {
  1436. weakSelf.alertView.activityIndicatorView = activityIndicatorView;
  1437. return weakSelf;
  1438. };
  1439. self.shouldDismissOnTapOutside = ^(BOOL shouldDismissOnTapOutside) {
  1440. weakSelf.alertView.shouldDismissOnTapOutside = shouldDismissOnTapOutside;
  1441. return weakSelf;
  1442. };
  1443. self.soundURL = ^(NSURL *soundURL) {
  1444. weakSelf.alertView.soundURL = soundURL;
  1445. return weakSelf;
  1446. };
  1447. self.attributedFormatBlock = ^(SCLAttributedFormatBlock attributedFormatBlock) {
  1448. weakSelf.alertView.attributedFormatBlock = attributedFormatBlock;
  1449. return weakSelf;
  1450. };
  1451. self.completeButtonFormatBlock = ^(CompleteButtonFormatBlock completeButtonFormatBlock) {
  1452. weakSelf.alertView.completeButtonFormatBlock = completeButtonFormatBlock;
  1453. return weakSelf;
  1454. };
  1455. self.buttonFormatBlock = ^(ButtonFormatBlock buttonFormatBlock) {
  1456. weakSelf.alertView.buttonFormatBlock = buttonFormatBlock;
  1457. return weakSelf;
  1458. };
  1459. self.forceHideBlock = ^(SCLForceHideBlock forceHideBlock) {
  1460. weakSelf.alertView.forceHideBlock = forceHideBlock;
  1461. return weakSelf;
  1462. };
  1463. self.hideAnimationType = ^(SCLAlertViewHideAnimation hideAnimationType) {
  1464. weakSelf.alertView.hideAnimationType = hideAnimationType;
  1465. return weakSelf;
  1466. };
  1467. self.showAnimationType = ^(SCLAlertViewShowAnimation showAnimationType) {
  1468. weakSelf.alertView.showAnimationType = showAnimationType;
  1469. return weakSelf;
  1470. };
  1471. self.backgroundType = ^(SCLAlertViewBackground backgroundType) {
  1472. weakSelf.alertView.backgroundType = backgroundType;
  1473. return weakSelf;
  1474. };
  1475. self.customViewColor = ^(UIColor *customViewColor) {
  1476. weakSelf.alertView.customViewColor = customViewColor;
  1477. return weakSelf;
  1478. };
  1479. self.backgroundViewColor = ^(UIColor *backgroundViewColor) {
  1480. weakSelf.alertView.backgroundViewColor = backgroundViewColor;
  1481. return weakSelf;
  1482. };
  1483. self.iconTintColor = ^(UIColor *iconTintColor) {
  1484. weakSelf.alertView.iconTintColor = iconTintColor;
  1485. return weakSelf;
  1486. };
  1487. self.circleIconHeight = ^(CGFloat circleIconHeight) {
  1488. weakSelf.alertView.circleIconHeight = circleIconHeight;
  1489. return weakSelf;
  1490. };
  1491. self.extensionBounds = ^(CGRect extensionBounds) {
  1492. weakSelf.alertView.extensionBounds = extensionBounds;
  1493. return weakSelf;
  1494. };
  1495. self.statusBarHidden = ^(BOOL statusBarHidden) {
  1496. weakSelf.alertView.statusBarHidden = statusBarHidden;
  1497. return weakSelf;
  1498. };
  1499. self.statusBarStyle = ^(UIStatusBarStyle statusBarStyle) {
  1500. weakSelf.alertView.statusBarStyle = statusBarStyle;
  1501. return weakSelf;
  1502. };
  1503. self.alertIsDismissed = ^(SCLDismissBlock dismissBlock) {
  1504. [weakSelf.alertView alertIsDismissed:dismissBlock];
  1505. return weakSelf;
  1506. };
  1507. self.alertDismissAnimationIsCompleted = ^(SCLDismissAnimationCompletionBlock dismissAnimationCompletionBlock) {
  1508. [weakSelf.alertView alertDismissAnimationIsCompleted:dismissAnimationCompletionBlock];
  1509. return weakSelf;
  1510. };
  1511. self.alertShowAnimationIsCompleted = ^(SCLShowAnimationCompletionBlock showAnimationCompletionBlock) {
  1512. [weakSelf.alertView alertShowAnimationIsCompleted:showAnimationCompletionBlock];
  1513. return weakSelf;
  1514. };
  1515. self.removeTopCircle = ^(void) {
  1516. [weakSelf.alertView removeTopCircle];
  1517. return weakSelf;
  1518. };
  1519. self.addCustomView = ^(UIView *view) {
  1520. [weakSelf.alertView addCustomView:view];
  1521. return weakSelf;
  1522. };
  1523. self.addTextField = ^(NSString *title) {
  1524. [weakSelf.alertView addTextField:title];
  1525. return weakSelf;
  1526. };
  1527. self.addCustomTextField = ^(UITextField *textField) {
  1528. [weakSelf.alertView addCustomTextField:textField];
  1529. return weakSelf;
  1530. };
  1531. self.addSwitchViewWithLabelTitle = ^(NSString *title) {
  1532. [weakSelf.alertView addSwitchViewWithLabel:title];
  1533. return weakSelf;
  1534. };
  1535. self.addTimerToButtonIndex = ^(NSInteger buttonIndex, BOOL reverse) {
  1536. [weakSelf.alertView addTimerToButtonIndex:buttonIndex reverse:reverse];
  1537. return weakSelf;
  1538. };
  1539. self.setTitleFontFamily = ^(NSString *titleFontFamily, CGFloat size) {
  1540. [weakSelf.alertView setTitleFontFamily:titleFontFamily withSize:size];
  1541. return weakSelf;
  1542. };
  1543. self.setBodyTextFontFamily = ^(NSString *bodyTextFontFamily, CGFloat size) {
  1544. [weakSelf.alertView setBodyTextFontFamily:bodyTextFontFamily withSize:size];
  1545. return weakSelf;
  1546. };
  1547. self.setButtonsTextFontFamily = ^(NSString *buttonsFontFamily, CGFloat size) {
  1548. [weakSelf.alertView setButtonsTextFontFamily:buttonsFontFamily withSize:size];
  1549. return weakSelf;
  1550. };
  1551. self.addButtonWithActionBlock = ^(NSString *title, SCLActionBlock action) {
  1552. [weakSelf.alertView addButton:title actionBlock:action];
  1553. return weakSelf;
  1554. };
  1555. self.addButtonWithValidationBlock = ^(NSString *title, SCLValidationBlock validationBlock, SCLActionBlock action) {
  1556. [weakSelf.alertView addButton:title validationBlock:validationBlock actionBlock:action];
  1557. return weakSelf;
  1558. };
  1559. self.addButtonWithTarget = ^(NSString *title, id target, SEL selector) {
  1560. [weakSelf.alertView addButton:title target:target selector:selector];
  1561. return weakSelf;
  1562. };
  1563. self.addButtonWithBuilder = ^(SCLALertViewButtonBuilder *builder){
  1564. SCLButton *button = nil;
  1565. if (builder.parameterTarget && builder.parameterSelector) {
  1566. button = [weakSelf.alertView addButton:builder.parameterTitle target:builder.parameterTarget selector:builder.parameterSelector];
  1567. }
  1568. else if (builder.parameterValidationBlock && builder.parameterActionBlock) {
  1569. button = [weakSelf.alertView addButton:builder.parameterTitle validationBlock:builder.parameterValidationBlock actionBlock:builder.parameterActionBlock];
  1570. }
  1571. else if (builder.parameterActionBlock) {
  1572. button = [weakSelf.alertView addButton:builder.parameterTitle actionBlock:builder.parameterActionBlock];
  1573. }
  1574. builder.button = button;
  1575. return weakSelf;
  1576. };
  1577. self.addTextFieldWithBuilder = ^(SCLALertViewTextFieldBuilder *builder){
  1578. builder.textField = [weakSelf.alertView addTextField:builder.parameterTitle];
  1579. return weakSelf;
  1580. };
  1581. }
  1582. #pragma mark - Init
  1583. - (instancetype)init {
  1584. self = [super init];
  1585. if (self) {
  1586. self.alertView = [[SCLAlertView alloc] init];
  1587. }
  1588. return self;
  1589. }
  1590. - (instancetype)initWithNewWindow {
  1591. self = [super init];
  1592. if (self) {
  1593. self.alertView = [[SCLAlertView alloc] initWithNewWindow];
  1594. }
  1595. return self;
  1596. }
  1597. - (instancetype)initWithNewWindowWidth:(CGFloat)width {
  1598. self = [super init];
  1599. if (self) {
  1600. self.alertView = [[SCLAlertView alloc] initWithNewWindowWidth:width];
  1601. }
  1602. return self;
  1603. }
  1604. @end
  1605. @interface SCLAlertViewShowBuilder()
  1606. @property(weak, nonatomic) UIViewController *parameterViewController;
  1607. @property(copy, nonatomic) UIImage *parameterImage;
  1608. @property(copy, nonatomic) UIColor *parameterColor;
  1609. @property(copy, nonatomic) NSString *parameterTitle;
  1610. @property(copy, nonatomic) NSString *parameterSubTitle;
  1611. @property(copy, nonatomic) NSString *parameterCompleteText;
  1612. @property(copy, nonatomic) NSString *parameterCloseButtonTitle;
  1613. @property(assign, nonatomic) SCLAlertViewStyle parameterStyle;
  1614. @property(assign, nonatomic) NSTimeInterval parameterDuration;
  1615. #pragma mark - Setters
  1616. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^viewController)(UIViewController *viewController);
  1617. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^image)(UIImage *image);
  1618. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^color)(UIColor *color);
  1619. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^title)(NSString *title);
  1620. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^subTitle)(NSString *subTitle);
  1621. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^completeText)(NSString *completeText);
  1622. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^style)(SCLAlertViewStyle style);
  1623. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^closeButtonTitle)(NSString *closeButtonTitle);
  1624. @property(copy, nonatomic) SCLAlertViewShowBuilder *(^duration)(NSTimeInterval duration);
  1625. #pragma mark - Show
  1626. @property(copy, nonatomic) void (^show)(SCLAlertView *view, UIViewController *controller);
  1627. @end
  1628. @implementation SCLAlertViewShowBuilder
  1629. - (void)setupFluent {
  1630. __weak __auto_type weakSelf = self;
  1631. self.viewController = ^(UIViewController *viewController){
  1632. weakSelf.parameterViewController = viewController;
  1633. return weakSelf;
  1634. };
  1635. self.image = ^(UIImage *image) {
  1636. weakSelf.parameterImage = image;
  1637. return weakSelf;
  1638. };
  1639. self.color = ^(UIColor *color) {
  1640. weakSelf.parameterColor = color;
  1641. return weakSelf;
  1642. };
  1643. self.title = ^(NSString *title){
  1644. weakSelf.parameterTitle = title;
  1645. return weakSelf;
  1646. };
  1647. self.subTitle = ^(NSString *subTitle){
  1648. weakSelf.parameterSubTitle = subTitle;
  1649. return weakSelf;
  1650. };
  1651. self.completeText = ^(NSString *completeText){
  1652. weakSelf.parameterCompleteText = completeText;
  1653. return weakSelf;
  1654. };
  1655. self.style = ^(SCLAlertViewStyle style){
  1656. weakSelf.parameterStyle = style;
  1657. return weakSelf;
  1658. };
  1659. self.closeButtonTitle = ^(NSString *closeButtonTitle){
  1660. weakSelf.parameterCloseButtonTitle = closeButtonTitle;
  1661. return weakSelf;
  1662. };
  1663. self.duration = ^(NSTimeInterval duration){
  1664. weakSelf.parameterDuration = duration;
  1665. return weakSelf;
  1666. };
  1667. self.show = ^(SCLAlertView *view, UIViewController *controller) {
  1668. [weakSelf showAlertView:view onViewController:controller];
  1669. };
  1670. }
  1671. #pragma mark - Setters
  1672. - (void)showAlertView:(SCLAlertView *)alertView {
  1673. [self showAlertView:alertView onViewController:self.parameterViewController];
  1674. }
  1675. - (void)showAlertView:(SCLAlertView *)alertView onViewController:(UIViewController *)controller {
  1676. UIViewController *targetController = controller ? controller : self.parameterViewController;
  1677. if (self.parameterImage || self.parameterColor) {
  1678. [alertView showTitle:targetController image:self.parameterImage color:self.parameterColor title:self.parameterTitle subTitle:self.parameterSubTitle duration:self.parameterDuration completeText:self.parameterCloseButtonTitle style:self.parameterStyle];
  1679. }
  1680. else {
  1681. [alertView showTitle:targetController title:self.parameterTitle subTitle:self.parameterSubTitle style:self.parameterStyle closeButtonTitle:self.parameterCloseButtonTitle duration:self.parameterDuration];
  1682. }
  1683. }
  1684. @end