SCLAlertView.m 67 KB

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