From 7b02207537d35bfa1714bf8beafc921f717d100a Mon Sep 17 00:00:00 2001
From: 单军华
Date: Wed, 11 Jul 2018 10:47:42 +0800
Subject: [PATCH] 首次上传
---
screendisplay/screendisplay/Classes/Helpers/JXTAlertController.m | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 252 insertions(+), 0 deletions(-)
diff --git a/screendisplay/screendisplay/Classes/Helpers/JXTAlertController.m b/screendisplay/screendisplay/Classes/Helpers/JXTAlertController.m
new file mode 100755
index 0000000..d232aad
--- /dev/null
+++ b/screendisplay/screendisplay/Classes/Helpers/JXTAlertController.m
@@ -0,0 +1,252 @@
+//
+// JXTAlertController.m
+// JXTAlertManager
+//
+// Created by JXT on 2016/12/22.
+// Copyright �� 2016��� JXT. All rights reserved.
+//
+
+#import "JXTAlertController.h"
+#import "UIWindow+CurrentViewController.h"
+
+//toast������������������
+static NSTimeInterval const JXTAlertShowDurationDefault = 1.0f;
+
+
+#pragma mark - I.AlertActionModel
+@interface JXTAlertActionModel : NSObject
+@property (nonatomic, copy) NSString * title;
+@property (nonatomic, assign) UIAlertActionStyle style;
+@end
+@implementation JXTAlertActionModel
+- (instancetype)init
+{
+ if (self = [super init]) {
+ self.title = @"";
+ self.style = UIAlertActionStyleDefault;
+ }
+ return self;
+}
+@end
+
+
+
+#pragma mark - II.JXTAlertController
+/**
+ AlertActions������
+
+ @param actionBlock JXTAlertActionBlock
+ */
+typedef void (^JXTAlertActionsConfig)(JXTAlertActionBlock actionBlock);
+
+
+@interface JXTAlertController ()
+//JXTAlertActionModel������
+@property (nonatomic, strong) NSMutableArray <JXTAlertActionModel *>* jxt_alertActionArray;
+//������������������
+@property (nonatomic, assign) BOOL jxt_setAlertAnimated;
+//action������
+- (JXTAlertActionsConfig)alertActionsConfig;
+@end
+
+@implementation JXTAlertController
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+}
+- (void)didReceiveMemoryWarning {
+ [super didReceiveMemoryWarning];
+}
+- (void)viewDidDisappear:(BOOL)animated
+{
+ [super viewDidDisappear:animated];
+ if (self.alertDidDismiss) {
+ self.alertDidDismiss();
+ }
+}
+- (void)dealloc
+{
+ NSLogFunc;
+}
+
+#pragma mark - Private
+//action-title������
+- (NSMutableArray<JXTAlertActionModel *> *)jxt_alertActionArray
+{
+ if (_jxt_alertActionArray == nil) {
+ _jxt_alertActionArray = [NSMutableArray array];
+ }
+ return _jxt_alertActionArray;
+}
+//action������
+- (JXTAlertActionsConfig)alertActionsConfig
+{
+ return ^(JXTAlertActionBlock actionBlock) {
+ if (self.jxt_alertActionArray.count > 0)
+ {
+ //������action
+ __weak typeof(self)weakSelf = self;
+
+ [self.jxt_alertActionArray enumerateObjectsUsingBlock:^(JXTAlertActionModel *actionModel, NSUInteger idx, BOOL * _Nonnull stop) {
+ UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionModel.title style:actionModel.style handler:^(UIAlertAction * _Nonnull action) {
+ __strong typeof(weakSelf)strongSelf = weakSelf;
+ if (actionBlock) {
+ actionBlock(idx, action, strongSelf);
+ }
+ }];
+ //������������������������������������������������������������
+// [alertAction setValue:[UIColor grayColor] forKey:@"titleTextColor"];
+ //action������self������������block������������������������������������������������������
+ [weakSelf addAction:alertAction];
+ }];
+ }
+ else
+ {
+ NSTimeInterval duration = self.toastStyleDuration > 0 ? self.toastStyleDuration : JXTAlertShowDurationDefault;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ [self dismissViewControllerAnimated:!(self.jxt_setAlertAnimated) completion:NULL];
+ });
+ }
+ };
+}
+
+#pragma mark - Public
+
+- (instancetype)initAlertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
+{
+ if (!(title.length > 0) && (message.length > 0) && (preferredStyle == UIAlertControllerStyleAlert)) {
+ title = @"";
+ }
+
+ self = [[self class] alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
+
+ if (!self) return nil;
+
+ self.jxt_setAlertAnimated = NO;
+ self.toastStyleDuration = JXTAlertShowDurationDefault;
+
+ return self;
+}
+
+
+- (void)alertAnimateDisabled
+{
+ self.jxt_setAlertAnimated = YES;
+}
+
+
+#pragma mark - addButton
+
+- (JXTAlertActionTitle)addActionDefaultTitle
+{
+ //���block���������������������������������������������������������������������������
+ return ^(NSString *title) {
+ JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
+ actionModel.title = title;
+ actionModel.style = UIAlertActionStyleDefault;
+ [self.jxt_alertActionArray addObject:actionModel];
+ return self;
+ };
+}
+
+- (JXTAlertActionTitle)addActionCancelTitle
+{
+ return ^(NSString *title) {
+ JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
+ actionModel.title = title;
+ actionModel.style = UIAlertActionStyleCancel;
+ [self.jxt_alertActionArray addObject:actionModel];
+ return self;
+ };
+}
+
+- (JXTAlertActionTitle)addActionDestructiveTitle
+{
+ return ^(NSString *title) {
+ JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
+ actionModel.title = title;
+ actionModel.style = UIAlertActionStyleDestructive;
+ [self.jxt_alertActionArray addObject:actionModel];
+ return self;
+ };
+}
+
+@end
+
+
+
+#pragma mark - III.UIViewController������
+@implementation UIViewController (JXTAlertController)
+
+- (void)jxt_showAlertWithPreferredStyle:(UIAlertControllerStyle)preferredStyle title:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
+{
+ if (appearanceProcess)
+ {
+ JXTAlertController *alertMaker = [[JXTAlertController alloc] initAlertControllerWithTitle:title message:message preferredStyle:preferredStyle];
+ //������nil
+ if (!alertMaker) {
+ return ;
+ }
+ //���������
+ !appearanceProcess ?: appearanceProcess(alertMaker);
+ //������������
+ alertMaker.alertActionsConfig(actionBlock);
+
+ [self presentViewController:alertMaker animated:!(alertMaker.jxt_setAlertAnimated) completion:^{
+ !alertMaker.alertDidShown ?: alertMaker.alertDidShown();
+ }];
+ }
+}
+
+- (void)jxt_showAlertWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
+{
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleAlert title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
+ });
+}
+
+- (void)jxt_showActionSheetWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
+{
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleActionSheet title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
+ });
+}
+
+
+@end
+
+@implementation UIAlertController (LMJ)
+
+/**
+ JXTAlertController: show-alert(iOS8)
+
+ @param title title
+ @param message message
+ @param appearanceProcess alert������������
+ @param actionBlock alert������������������
+ */
++ (void)mj_showAlertWithTitle:(nullable NSString *)title
+ message:(nullable NSString *)message
+ appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
+ actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0)
+{
+ [[UIWindow zf_currentViewController] jxt_showAlertWithTitle:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
+}
+
+/**
+ JXTAlertController: show-actionSheet(iOS8)
+
+ @param title title
+ @param message message
+ @param appearanceProcess actionSheet������������
+ @param actionBlock actionSheet������������������
+ */
++ (void)mj_showActionSheetWithTitle:(nullable NSString *)title
+ message:(nullable NSString *)message
+ appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
+ actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0)
+{
+ [[UIWindow zf_currentViewController] jxt_showActionSheetWithTitle:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
+}
+@end
+
--
Gitblit v1.8.0