├── SLAlertViewDemo
├── SLAlertViewDemo.xcodeproj
│ ├── project.xcworkspace
│ │ └── contents.xcworkspacedata
│ └── project.pbxproj
├── SLAlertViewDemo
│ ├── ViewController.h
│ ├── AppDelegate.h
│ ├── main.m
│ ├── SLAlertView
│ │ ├── UIView+SLAutolayout.h
│ │ ├── SLActionSheet.h
│ │ ├── UIView+SLAutolayout.m
│ │ ├── SLAlert.h
│ │ ├── SLAlertView.m
│ │ ├── SLAlertView.h
│ │ ├── SLAlert.m
│ │ └── SLActionSheet.m
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── AppDelegate.m
│ └── ViewController.m
├── SLAlertViewDemoTests
│ ├── Info.plist
│ └── SLAlertViewDemoTests.m
└── SLAlertViewDemoUITests
│ ├── Info.plist
│ └── SLAlertViewDemoUITests.m
├── .gitignore
├── README.md
└── LICENSE
/SLAlertViewDemo/SLAlertViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // SLAlertViewDemo
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // SLAlertViewDemo
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // SLAlertViewDemo
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemoTests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemoUITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/UIView+SLAutolayout.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+SLAutolayout.h
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/20.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface UIView (SLAutolayout)
12 |
13 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to toAttribute:(NSLayoutAttribute)toAttribute offset:(CGFloat)offset;
14 |
15 | -(void)addConstraint:(NSLayoutAttribute)attribute value:(CGFloat)value;
16 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to offset:(CGFloat)offset;
17 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to multiplier:(CGFloat)multiplier;
18 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to fromConstraint:(NSLayoutAttribute)fromAttribute offset:(CGFloat)offset;
19 | - (void)removeAutoLayout:(NSLayoutConstraint *)constraint;
20 | - (void)removeAllAutoLayout;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemoTests/SLAlertViewDemoTests.m:
--------------------------------------------------------------------------------
1 | //
2 | // SLAlertViewDemoTests.m
3 | // SLAlertViewDemoTests
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface SLAlertViewDemoTests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation SLAlertViewDemoTests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 | // Put setup code here. This method is called before the invocation of each test method in the class.
20 | }
21 |
22 | - (void)tearDown {
23 | // Put teardown code here. This method is called after the invocation of each test method in the class.
24 | [super tearDown];
25 | }
26 |
27 | - (void)testExample {
28 | // This is an example of a functional test case.
29 | // Use XCTAssert and related functions to verify your tests produce the correct results.
30 | }
31 |
32 | - (void)testPerformanceExample {
33 | // This is an example of a performance test case.
34 | [self measureBlock:^{
35 | // Put the code you want to measure the time of here.
36 | }];
37 | }
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "29x29",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "29x29",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "40x40",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "40x40",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "60x60",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "60x60",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "ipad",
35 | "size" : "29x29",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "ipad",
40 | "size" : "29x29",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "40x40",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "40x40",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "76x76",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "76x76",
61 | "scale" : "2x"
62 | }
63 | ],
64 | "info" : {
65 | "version" : 1,
66 | "author" : "xcode"
67 | }
68 | }
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemoUITests/SLAlertViewDemoUITests.m:
--------------------------------------------------------------------------------
1 | //
2 | // SLAlertViewDemoUITests.m
3 | // SLAlertViewDemoUITests
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface SLAlertViewDemoUITests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation SLAlertViewDemoUITests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 |
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 |
22 | // In UI tests it is usually best to stop immediately when a failure occurs.
23 | self.continueAfterFailure = NO;
24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
25 | [[[XCUIApplication alloc] init] launch];
26 |
27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
28 | }
29 |
30 | - (void)tearDown {
31 | // Put teardown code here. This method is called after the invocation of each test method in the class.
32 | [super tearDown];
33 | }
34 |
35 | - (void)testExample {
36 | // Use recording to get started writing UI tests.
37 | // Use XCTAssert and related functions to verify your tests produce the correct results.
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData/
8 |
9 | ## Various settings
10 | *.pbxuser
11 | !default.pbxuser
12 | *.mode1v3
13 | !default.mode1v3
14 | *.mode2v3
15 | !default.mode2v3
16 | *.perspectivev3
17 | !default.perspectivev3
18 | xcuserdata/
19 |
20 | ## Other
21 | *.moved-aside
22 | *.xcuserstate
23 |
24 | ## Obj-C/Swift specific
25 | *.hmap
26 | *.ipa
27 | *.dSYM.zip
28 | *.dSYM
29 |
30 | # CocoaPods
31 | #
32 | # We recommend against adding the Pods directory to your .gitignore. However
33 | # you should judge for yourself, the pros and cons are mentioned at:
34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
35 | #
36 | # Pods/
37 |
38 | # Carthage
39 | #
40 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
41 | # Carthage/Checkouts
42 |
43 | Carthage/Build
44 |
45 | # fastlane
46 | #
47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
48 | # screenshots whenever they are needed.
49 | # For more information about the recommended setup visit:
50 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md
51 |
52 | fastlane/report.xml
53 | fastlane/screenshots
54 |
55 | #Code Injection
56 | #
57 | # After new code Injection tools there's a generated folder /iOSInjectionProject
58 | # https://github.com/johnno1962/injectionforxcode
59 |
60 | iOSInjectionProject/
61 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // SLAlertViewDemo
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/SLActionSheet.h:
--------------------------------------------------------------------------------
1 | //
2 | // SLActionSheet.h
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class SLActionSheet;
12 | @protocol SLActionSheetProtocol
13 |
14 | @optional
15 | - (void)actionSheet:(SLActionSheet * _Nonnull)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
16 |
17 | @end
18 |
19 | @interface SLActionSheet : UIView
20 |
21 | @property (nonatomic, weak) _Nullable id delegate;
22 |
23 | @property (nonatomic, strong) UIColor * _Nullable tintColor;
24 | @property (nonatomic, strong) UIColor * _Nullable titleColor;
25 | @property (nonatomic, strong) UIColor * _Nullable messageColor;
26 | @property (nonatomic, strong) UIColor * _Nullable titleBackgroundColor;
27 | @property (nonatomic, strong) UIColor * _Nullable messageBackgroundColor;
28 | @property (nonatomic, strong) UIColor * _Nullable cancelTitleColor;
29 | @property (nonatomic, strong) UIColor * _Nullable cancelBackgroundColor;
30 | @property (nonatomic, strong) UIColor * _Nullable otherTitleColor;
31 | @property (nonatomic, strong) UIColor * _Nullable otherBackgroundColor;
32 | @property (nonatomic, strong) UIColor * _Nullable separatorColor;
33 | @property (nonatomic, strong) UIFont * _Nullable titleFont;
34 | @property (nonatomic, strong) UIFont * _Nullable messageFont;
35 | @property (nonatomic, strong) UIFont * _Nullable cancelTitleFont;
36 | @property (nonatomic, strong) UIFont * _Nullable otherTitleFont;
37 |
38 |
39 | + (void)actionSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles clickHandler:(nullable void (^)(SLActionSheet * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
40 |
41 | + (void)actionSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles settingHandler:(nullable void (^)( SLActionSheet * _Nonnull actionSheet))settingHandler clickHandler:(nullable void (^)(SLActionSheet * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
42 |
43 | @end
44 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/UIView+SLAutolayout.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+SLAutolayout.m
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/20.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import "UIView+SLAutolayout.h"
10 |
11 | @implementation UIView (SLAutolayout)
12 |
13 | -(void)addConstraint:(NSLayoutAttribute)attribute value:(CGFloat)value
14 | {
15 | [self setTranslatesAutoresizingMaskIntoConstraints:NO];
16 | [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:value]];
17 | }
18 |
19 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to toAttribute:(NSLayoutAttribute)toAttribute offset:(CGFloat)offset
20 | {
21 | [self setTranslatesAutoresizingMaskIntoConstraints:NO];
22 | [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:to attribute:toAttribute multiplier:1.0 constant:offset]];
23 | }
24 |
25 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to offset:(CGFloat)offset
26 | {
27 | [self setTranslatesAutoresizingMaskIntoConstraints:NO];
28 | [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:to attribute:attribute multiplier:1.0 constant:offset]];
29 | }
30 |
31 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to multiplier:(CGFloat)multiplier
32 | {
33 | [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:to attribute:attribute multiplier:multiplier constant:0]];
34 | }
35 |
36 | -(void)addConstraint:(NSLayoutAttribute)attribute equalTo:(UIView *)to fromConstraint:(NSLayoutAttribute)fromAttribute offset:(CGFloat)offset
37 | {
38 | [self setTranslatesAutoresizingMaskIntoConstraints:NO];
39 | [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:to attribute:fromAttribute multiplier:1.0 constant:offset]];
40 | }
41 |
42 | - (void)removeAutoLayout:(NSLayoutConstraint *)constraint
43 | {
44 | for (NSLayoutConstraint *con in self.superview.constraints) {
45 | if ([con isEqual:constraint]) {
46 | [self.superview removeConstraint:con];
47 | }
48 | }
49 | }
50 |
51 | - (void)removeAllAutoLayout
52 | {
53 | for (NSLayoutConstraint *con in self.constraints)
54 | {
55 | [self removeConstraint:con];
56 | }
57 | }
58 |
59 |
60 |
61 | @end
62 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/SLAlert.h:
--------------------------------------------------------------------------------
1 | //
2 | // SLAlert.h
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class SLAlert;
12 | @protocol SLAlertProtocol
13 |
14 | @optional
15 | - (void)alertView:(SLAlert * _Nonnull)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
16 |
17 | @end
18 |
19 |
20 | @interface SLAlert : UIView
21 |
22 | @property (nonatomic, weak) _Nullable id delegate;
23 |
24 | @property (nonatomic, strong) UIColor * _Nullable tintColor;
25 | @property (nonatomic, strong) UIColor * _Nullable titleColor;
26 | @property (nonatomic, strong) UIColor * _Nullable messageColor;
27 | @property (nonatomic, strong) UIColor * _Nullable titleBackgroundColor;
28 | @property (nonatomic, strong) UIColor * _Nullable messageBackgroundColor;
29 | @property (nonatomic, strong) UIColor * _Nullable cancelTitleColor;
30 | @property (nonatomic, strong) UIColor * _Nullable cancelBackgroundColor;
31 | @property (nonatomic, strong) UIColor * _Nullable otherTitleColor;
32 | @property (nonatomic, strong) UIColor * _Nullable otherBackgroundColor;
33 | @property (nonatomic, strong) UIColor * _Nullable separatorColor;
34 | @property (nonatomic, strong) UIFont * _Nullable titleFont;
35 | @property (nonatomic, strong) UIFont * _Nullable messageFont;
36 | @property (nonatomic, strong) UIFont * _Nullable cancelTitleFont;
37 | @property (nonatomic, strong) UIFont * _Nullable otherTitleFont;
38 |
39 | // clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler
40 |
41 | + (void)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id )delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles;
42 |
43 | + (void)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles clickHandler:(nullable void (^)(SLAlert * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
44 |
45 | + (void)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id )delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles settingHandler:(nullable void (^)( SLAlert * _Nonnull alert))settingHandler clickHandler:(nullable void (^)(SLAlert * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
46 |
47 |
48 |
49 | @end
50 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/SLAlertView.m:
--------------------------------------------------------------------------------
1 | //
2 | // SLAlertView.m
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/20.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import "SLAlertView.h"
10 | #import "SLAlert.h"
11 | #import "SLActionSheet.h"
12 |
13 | @interface SLAlertView()
14 |
15 | @end
16 |
17 | @implementation SLAlertView
18 |
19 |
20 | + (void)alertViewWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate preferredStyle:(SLAlertViewStyle)preferredStyle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles {
21 |
22 | if (preferredStyle == SLAlertViewStyleAlert) {
23 |
24 | id temDelegate = (id)delegate;
25 | [SLAlert alertWithTitle:title message:message delegate:temDelegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles];
26 |
27 | } else {
28 |
29 | id temDelegate = (id)delegate;
30 | [SLActionSheet actionSheetWithTitle:title message:message delegate:temDelegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles clickHandler:nil];
31 |
32 | }
33 | }
34 |
35 | + (void)alertViewWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(SLAlertViewStyle)preferredStyle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles clickHandler:(void (^)(SLAlertView * _Nonnull, NSInteger, NSString * _Nullable))clickHandler{
36 |
37 | if (preferredStyle == SLAlertViewStyleAlert) {
38 |
39 | void(^clickHandlerTemp)(SLAlert * _Nonnull, NSInteger , NSString * _Nullable) = (void(^)(SLAlert * _Nonnull , NSInteger , NSString * _Nullable))clickHandler;
40 |
41 | [SLAlert alertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles clickHandler:clickHandlerTemp];
42 |
43 | } else {
44 |
45 | void(^clickHandlerTemp)(SLActionSheet * _Nonnull, NSInteger , NSString * _Nullable) = (void(^)(SLActionSheet * _Nonnull , NSInteger , NSString * _Nullable))clickHandler;
46 |
47 | [SLActionSheet actionSheetWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles clickHandler:clickHandlerTemp];
48 |
49 | }
50 |
51 | }
52 |
53 | + (void)alertViewWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate preferredStyle:(SLAlertViewStyle)preferredStyle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles settingHandler:(void (^)(SLAlertView * _Nonnull))settingHandler {
54 | if (preferredStyle == SLAlertViewStyleAlert) {
55 |
56 | id temDelegate = (id)delegate;
57 |
58 | void(^tempSettingHandler)(SLAlert * _Nonnull) = (void (^)(SLAlert * _Nonnull))settingHandler;
59 |
60 | [SLAlert alertWithTitle:title message:message delegate:temDelegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles settingHandler:tempSettingHandler clickHandler:nil];
61 |
62 | } else {
63 |
64 | id temDelegate = (id)delegate;
65 |
66 | void(^tempSettingHandler)(SLActionSheet * _Nonnull) = (void (^)(SLActionSheet * _Nonnull))settingHandler;
67 |
68 | [SLActionSheet actionSheetWithTitle:title message:message delegate:temDelegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles settingHandler:tempSettingHandler clickHandler:nil];
69 |
70 | }
71 | }
72 |
73 | + (void)alertViewWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(SLAlertViewStyle)preferredStyle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles settingHandler:(void (^)(SLAlertView * _Nonnull))settingHandler clickHandler:(void (^)(SLAlertView * _Nonnull, NSInteger, NSString * _Nullable))clickHandler {
74 |
75 | if (preferredStyle == SLAlertViewStyleAlert) {
76 |
77 | void(^tempSettingHandler)(SLAlert * _Nonnull) = (void (^)(SLAlert * _Nonnull))settingHandler;
78 |
79 | void(^clickHandlerTemp)(SLAlert * _Nonnull, NSInteger , NSString * _Nullable) = (void(^)(SLAlert * _Nonnull , NSInteger , NSString * _Nullable))clickHandler;
80 |
81 | [SLAlert alertWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles settingHandler:tempSettingHandler clickHandler:clickHandlerTemp];
82 |
83 | } else {
84 |
85 | void(^tempSettingHandler)(SLActionSheet * _Nonnull) = (void (^)(SLActionSheet * _Nonnull))settingHandler;
86 |
87 | void(^clickHandlerTemp)(SLActionSheet * _Nonnull, NSInteger , NSString * _Nullable) = (void(^)(SLActionSheet * _Nonnull , NSInteger , NSString * _Nullable))clickHandler;
88 |
89 | [SLActionSheet actionSheetWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles settingHandler:tempSettingHandler clickHandler:clickHandlerTemp];
90 |
91 | }
92 | }
93 |
94 |
95 | @end
96 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/SLAlertView.h:
--------------------------------------------------------------------------------
1 | //
2 | // SLAlertView.h
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/20.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | typedef NS_ENUM(NSInteger, SLAlertViewStyle){
13 | SLAlertViewStyleAlert = 0,
14 | SLAlertViewStyleActionSheet = 1,
15 | };
16 |
17 |
18 | @class SLAlertView;
19 | @class SLAlert;
20 | @class SLActionSheet;
21 |
22 | @protocol SLAlertViewProtocol
23 |
24 | @optional
25 | - (void)alertView:(SLAlert * _Nonnull)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
26 | - (void)actionSheet:(SLActionSheet * _Nonnull)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
27 |
28 | @end
29 |
30 | @interface SLAlertView : UIView
31 | /** 主题颜色(暂时没用) */
32 | @property (nonatomic, strong) UIColor * _Nullable tintColor; // 暂时没用
33 | /** title字体颜色 */
34 | @property (nonatomic, strong) UIColor * _Nullable titleColor;
35 | /** message字体颜色 */
36 | @property (nonatomic, strong) UIColor * _Nullable messageColor;
37 | /** title背景颜色 */
38 | @property (nonatomic, strong) UIColor * _Nullable titleBackgroundColor;
39 | /** message背景颜色 */
40 | @property (nonatomic, strong) UIColor * _Nullable messageBackgroundColor;
41 | /** 取消按钮的字体颜色 */
42 | @property (nonatomic, strong) UIColor * _Nullable cancelTitleColor;
43 | /** 取消按钮的背景颜色 */
44 | @property (nonatomic, strong) UIColor * _Nullable cancelBackgroundColor;
45 | /** 其他按钮的字体颜色 */
46 | @property (nonatomic, strong) UIColor * _Nullable otherTitleColor;
47 | /** 其他按钮的背景颜色 */
48 | @property (nonatomic, strong) UIColor * _Nullable otherBackgroundColor;
49 | /** 分割线的颜色 默认是透明的 */
50 | @property (nonatomic, strong) UIColor * _Nullable separatorColor;
51 | /** title字体 */
52 | @property (nonatomic, strong) UIFont * _Nullable titleFont;
53 | /** message字体 */
54 | @property (nonatomic, strong) UIFont * _Nullable messageFont;
55 | /** 取消按钮字体 */
56 | @property (nonatomic, strong) UIFont * _Nullable cancelTitleFont;
57 | /** 其他按钮字体 */
58 | @property (nonatomic, strong) UIFont * _Nullable otherTitleFont;
59 |
60 |
61 |
62 | /**
63 | * 创建并弹出使用代理监听点击事件的默认的alertView
64 | * param title 标题
65 | * param message 提示文字内容
66 | * param delegate 代理 可通过设置代理监听按钮的点击
67 | * param preferredStyle 弹出样式
68 | * param cancelButtonTitle 取消按钮文字
69 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
70 | */
71 | + (void)alertViewWithTitle:(nullable NSString *)title
72 | message:(nullable NSString *)message
73 | delegate:(nullable id)delegate
74 | preferredStyle:(SLAlertViewStyle)preferredStyle
75 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
76 | otherButtonTitles:(nullable NSArray *)otherButtonTitles;
77 |
78 | /**
79 | * 创建并弹出使用block监听点击事件的默认的alertView
80 | * param title 标题
81 | * param message 提示文字内容
82 | * param preferredStyle 弹出样式
83 | * param cancelButtonTitle 取消按钮文字
84 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
85 | * param clickHandler 点击按钮的回调
86 | */
87 | + (void)alertViewWithTitle:(nullable NSString *)title
88 | message:(nullable NSString *)message
89 | preferredStyle:(SLAlertViewStyle)preferredStyle
90 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
91 | otherButtonTitles:(nullable NSArray *)otherButtonTitles
92 | clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
93 |
94 |
95 | /**
96 | * 创建并弹出使用代理监听点击事件的可自定义的alertView
97 | * param title 标题
98 | * param message 提示文字内容
99 | * param delegate 代理 可通过设置代理监听按钮的点击
100 | * param preferredStyle 弹出样式
101 | * param cancelButtonTitle 取消按钮文字
102 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
103 | * param settingHandler 自定义设置的回调 可通过此block设置各种背景颜色、字体颜色和字体。
104 | 自定义设置方式:
105 | alertView.titleColor = [UIColor blackColor];
106 | alertView.messageColor = [UIColor blackColor];
107 | ...
108 | */
109 | + (void)alertViewWithTitle:(nullable NSString *)title
110 | message:(nullable NSString *)message
111 | delegate:(nullable id)delegate
112 | preferredStyle:(SLAlertViewStyle)preferredStyle
113 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
114 | otherButtonTitles:(nullable NSArray *)otherButtonTitles
115 | settingHandler:(nullable void (^)( SLAlertView * _Nonnull alertView))settingHandler;
116 |
117 | /**
118 | * 创建并弹出使用block监听点击事件的可自定义的alertView
119 | * param title 标题
120 | * param message 提示文字内容
121 | * param preferredStyle 弹出样式
122 | * param cancelButtonTitle 取消按钮文字
123 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
124 | * param settingHandler 自定义设置的回调 可通过此block设置各种背景颜色、字体颜色和字体。
125 | 自定义设置方式:
126 | alertView.titleColor = [UIColor blackColor];
127 | alertView.messageColor = [UIColor blackColor];
128 | ...
129 | * param clickHandler 点击按钮的回调
130 | */
131 | + (void)alertViewWithTitle:(nullable NSString *)title
132 | message:(nullable NSString *)message
133 | preferredStyle:(SLAlertViewStyle)preferredStyle
134 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
135 | otherButtonTitles:(nullable NSArray *)otherButtonTitles
136 | settingHandler:(nullable void (^)( SLAlertView * _Nonnull alertView))settingHandler
137 | clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
138 |
139 | @end
140 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SLAlertView
2 | 简约系统风格自由定制的弹窗Alert&ActionSheet
3 |
4 | # 效果图
5 |
6 | 
7 | 
8 | 
9 | 
10 | 
11 | 
12 | 
13 | 
14 | 
15 |
16 |
17 |
18 |
19 | # 项目介绍
20 | * 这是一款简约系统风格的弹窗,开发者可通过一句代码创建并弹出。
21 | * 开发者可以选择通过代理的方式或者block的方式来监听按钮的点击事件。
22 | * 开发者可通过settingHandler设置弹窗的背景颜色、字体颜色、字体以及分割线的颜色以满足不同风格的app的需求
23 | * 更加详尽的使用情况请下载demo
24 |
25 | # 使用说明
26 | ### 安装
27 | 将demo中的SLAlertView文件夹拖入项目
28 |
29 | 
30 | ### 接口说明
31 | ```
32 | /**
33 | * 创建并弹出使用代理监听点击事件的默认的alertView
34 | * param title 标题
35 | * param message 提示文字内容
36 | * param delegate 代理 可通过设置代理监听按钮的点击
37 | * param preferredStyle 弹出样式
38 | * param cancelButtonTitle 取消按钮文字
39 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
40 | */
41 | + (void)alertViewWithTitle:(nullable NSString *)title
42 | message:(nullable NSString *)message
43 | delegate:(nullable id)delegate
44 | preferredStyle:(SLAlertViewStyle)preferredStyle
45 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
46 | otherButtonTitles:(nullable NSArray *)otherButtonTitles;
47 |
48 |
49 |
50 |
51 | /**
52 | * 创建并弹出使用block监听点击事件的默认的alertView
53 | * param title 标题
54 | * param message 提示文字内容
55 | * param preferredStyle 弹出样式
56 | * param cancelButtonTitle 取消按钮文字
57 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
58 | * param clickHandler 点击按钮的回调
59 | */
60 | + (void)alertViewWithTitle:(nullable NSString *)title
61 | message:(nullable NSString *)message
62 | preferredStyle:(SLAlertViewStyle)preferredStyle
63 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
64 | otherButtonTitles:(nullable NSArray *)otherButtonTitles
65 | clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
66 |
67 |
68 |
69 |
70 |
71 | /**
72 | * 创建并弹出使用代理监听点击事件的可自定义的alertView
73 | * param title 标题
74 | * param message 提示文字内容
75 | * param delegate 代理 可通过设置代理监听按钮的点击
76 | * param preferredStyle 弹出样式
77 | * param cancelButtonTitle 取消按钮文字
78 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
79 | * param settingHandler 自定义设置的回调 可通过此block设置各种背景颜色、字体颜色和字体。
80 | 自定义设置方式:
81 | alertView.titleColor = [UIColor blackColor];
82 | alertView.messageColor = [UIColor blackColor];
83 | ...
84 | */
85 | + (void)alertViewWithTitle:(nullable NSString *)title
86 | message:(nullable NSString *)message
87 | delegate:(nullable id)delegate
88 | preferredStyle:(SLAlertViewStyle)preferredStyle
89 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
90 | otherButtonTitles:(nullable NSArray *)otherButtonTitles
91 | settingHandler:(nullable void (^)( SLAlertView * _Nonnull alertView))settingHandler;
92 |
93 |
94 |
95 |
96 | /**
97 | * 创建并弹出使用block监听点击事件的可自定义的alertView
98 | * param title 标题
99 | * param message 提示文字内容
100 | * param preferredStyle 弹出样式
101 | * param cancelButtonTitle 取消按钮文字
102 | * param otherButtonTitles 其他按钮文字(数组)@[@"other1",@"other2"]
103 | * param settingHandler 自定义设置的回调 可通过此block设置各种背景颜色、字体颜色和字体。
104 | 自定义设置方式:
105 | alertView.titleColor = [UIColor blackColor];
106 | alertView.messageColor = [UIColor blackColor];
107 | ...
108 | * param clickHandler 点击按钮的回调
109 | */
110 | + (void)alertViewWithTitle:(nullable NSString *)title
111 | message:(nullable NSString *)message
112 | preferredStyle:(SLAlertViewStyle)preferredStyle
113 | cancelButtonTitle:(nullable NSString *)cancelButtonTitle
114 | otherButtonTitles:(nullable NSArray *)otherButtonTitles
115 | settingHandler:(nullable void (^)( SLAlertView * _Nonnull alertView))settingHandler
116 | clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;
117 |
118 |
119 | // 代理方法
120 | - (void)alertView:(SLAlert * _Nonnull)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
121 | - (void)actionSheet:(SLActionSheet * _Nonnull)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
122 |
123 | ```
124 | ### 调用
125 | * 导入头文件
126 | ```
127 | #import "SLAlertView.h"
128 | ```
129 | * 在需要弹窗的地方调用接口
130 | 1> 创建并弹出使用代理监听点击事件的默认的alert / actionSheet
131 | ```
132 | // alert
133 | [SLAlertView alertViewWithTitle:@"title"
134 | message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage"
135 | delegate:self
136 | preferredStyle:SLAlertViewStyleAlert
137 | cancelButtonTitle:@"cancel"
138 | otherButtonTitles:nil];
139 |
140 | // action sheet
141 | [SLAlertView alertViewWithTitle:@"title"
142 | message:@"this is message"
143 | delegate:self
144 | preferredStyle:SLAlertViewStyleActionSheet
145 | cancelButtonTitle:@"cancel"
146 | otherButtonTitles:@[@"other"]];
147 | ```
148 | 2> 创建并弹出使用block监听点击事件的默认的alert / actionSheet
149 | ```
150 | // 通过block回调方式创建alert
151 | [SLAlertView alertViewWithTitle:@"title"
152 | message:@"message"
153 | preferredStyle:SLAlertViewStyleAlert
154 | cancelButtonTitle:@"cancel"
155 | otherButtonTitles:@[@"other"]
156 | clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
157 |
158 | NSLog(@"block %ld-%@",buttonIndex,buttonTitle);
159 |
160 | }];
161 | // 通过block回调方式创建action sheet
162 | [SLAlertView alertViewWithTitle:@"actionSheet"
163 | message:@"sheet_default"
164 | preferredStyle:SLAlertViewStyleActionSheet
165 | cancelButtonTitle:@"cancel"
166 | otherButtonTitles:@[@"other1",@"other2",@"other3"]
167 | clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
168 | NSLog(@"block sheet_default %ld-%@",buttonIndex,buttonTitle);
169 | }];
170 | ```
171 | 3> 自定义的情况(以actionSheet为例,alert用法完全一样)
172 | ```
173 | [SLAlertView alertViewWithTitle:@"title"
174 | message:@"message"
175 | preferredStyle:SLAlertViewStyleActionSheet
176 | cancelButtonTitle:@"cancel"
177 | otherButtonTitles:@[@"other1",@"other2"]
178 | settingHandler:^(SLAlertView * _Nonnull alertView) {
179 |
180 | alertView.titleBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
181 | alertView.messageBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
182 | alertView.otherBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
183 | alertView.cancelBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
184 | alertView.titleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
185 | alertView.messageColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
186 | alertView.otherTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
187 | alertView.cancelTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
188 | alertView.separatorColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
189 | alertView.titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
190 | alertView.messageFont = [UIFont fontWithName:@"AmericanTypewriter" size:16];
191 | alertView.otherTitleFont = [UIFont fontWithName:@"DBLCDTempBlack" size:16];
192 | alertView.cancelTitleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
193 | }
194 | clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
195 |
196 | NSLog(@"block sheet_customBackgroundColor %ld-%@",buttonIndex,buttonTitle);
197 |
198 | }];
199 | ```
200 | 4> 代理方法。遵守协议
201 | ```
202 | - (void)actionSheet:(SLActionSheet *)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString *)buttonTitle {
203 | NSLog(@"actionSheet:didSelectedButtonWithButtonIndex:%ld buttonTitle:%@",index,buttonTitle);
204 | }
205 | - (void)alertView:(SLAlert *)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString *)buttonTitle {
206 | NSLog(@"alertView:didSelectedButtonWithButtonIndex:%ld buttonTitle:%@",index,buttonTitle);
207 | }
208 | ```
209 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // SLAlertViewDemo
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 Pandai. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import "SLAlertView.h"
11 |
12 | @interface ViewController ()
13 |
14 | @end
15 |
16 | @implementation ViewController
17 |
18 | - (void)viewDidLoad {
19 | [super viewDidLoad];
20 |
21 | }
22 |
23 | #pragma mark - delegate method
24 | - (void)actionSheet:(SLActionSheet *)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString *)buttonTitle {
25 | NSLog(@"actionSheet:didSelectedButtonWithButtonIndex:%ld buttonTitle:%@",index,buttonTitle);
26 | }
27 | - (void)alertView:(SLAlert *)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString *)buttonTitle {
28 | NSLog(@"alertView:didSelectedButtonWithButtonIndex:%ld buttonTitle:%@",index,buttonTitle);
29 | }
30 |
31 | #pragma mark - actionSheet
32 | - (IBAction)sheet_default:(id)sender {
33 | // 通过block回调方式创建action sheet
34 | [SLAlertView alertViewWithTitle:@"actionSheet"
35 | message:@"sheet_default"
36 | preferredStyle:SLAlertViewStyleActionSheet
37 | cancelButtonTitle:@"cancel"
38 | otherButtonTitles:@[@"other1",@"other2",@"other3"]
39 | clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
40 | NSLog(@"block sheet_default %ld-%@",buttonIndex,buttonTitle);
41 | }];
42 | }
43 |
44 | - (IBAction)sheet_longTitleAndLongMessage:(id)sender {
45 | [SLAlertView alertViewWithTitle:@"sheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheet" message:@"sheet_longTitleAndLongMessage sheet_longTitleAndLongMessage sheet_longTitleAndLongMessage sheet_longTitleAndLongMessage sheet_longTitleAndLongMessage sheet_longTitleAndLongMessage sheet_longTitleAndLongMessagesheet_longTitleAndLongMessagesheet_longTitleAndLongMessagesheet_longTitleAndLongMessagesheet_longTitleAndLongMessagesheet_longTitleAndLongMessagesheet_longTitleAndLongMessage" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other1",@"other2"]];
46 | }
47 |
48 | - (IBAction)sheet_noTitle:(id)sender {
49 | // action sheet
50 | [SLAlertView alertViewWithTitle:nil
51 | message:@"this is message"
52 | delegate:self
53 | preferredStyle:SLAlertViewStyleActionSheet
54 | cancelButtonTitle:@"cancel"
55 | otherButtonTitles:@[@"other"]];
56 | }
57 |
58 | - (IBAction)sheet_noMessage:(id)sender {
59 | [SLAlertView alertViewWithTitle:@"this is title" message:nil delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other"]];
60 | }
61 |
62 | - (IBAction)sheet_noOtherBtns:(id)sender {
63 | [SLAlertView alertViewWithTitle:@"actionSheet" message:@"sheet_noOtherBtns" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:nil];
64 | }
65 |
66 | - (IBAction)sheet_noCancelBtn:(id)sender {
67 | [SLAlertView alertViewWithTitle:@"actionSheet" message:@"sheet_noCancelBtn" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:nil otherButtonTitles:@[@"other"]];
68 | }
69 |
70 | - (IBAction)sheet_onlyTitle:(id)sender {
71 | [SLAlertView alertViewWithTitle:@"sheet_onlyTitle" message:nil delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:nil otherButtonTitles:nil];
72 | }
73 |
74 | - (IBAction)sheet_onlyMessage:(id)sender {
75 | [SLAlertView alertViewWithTitle:nil message:@"sheet_onlyMessage" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:nil otherButtonTitles:nil];
76 | }
77 |
78 | - (IBAction)sheet_onlyOtherBtns:(id)sender {
79 | [SLAlertView alertViewWithTitle:nil message:nil delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:nil otherButtonTitles:@[@"other1",@"other2",@"other3"]];
80 | }
81 |
82 | - (IBAction)sheet_onlyCancelBtn:(id)sender {
83 | [SLAlertView alertViewWithTitle:nil message:nil delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:nil];
84 | }
85 |
86 | - (IBAction)sheet_noTitleAndNoMsg:(id)sender {
87 | [SLAlertView alertViewWithTitle:nil message:nil delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other1",@"other2"]];
88 | }
89 | - (IBAction)sheet_noButton:(id)sender {
90 | [SLAlertView alertViewWithTitle:@"actionSheet" message:@"sheet_noButton sheet_noButtonsheet_noButtonsheet_noButtonsheet_noButtonsheet_noButtonsheet_noButtonsheet_noButtonsheet_noButtonsheet_noButton" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:nil otherButtonTitles:nil];
91 | }
92 | - (IBAction)sheet_customBackgroundColor:(id)sender {
93 | // 通过block回调方式创建
94 | [SLAlertView alertViewWithTitle:@"sheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheetsheet" message:@"sheet_customBackgroundColor sheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColorsheet_customBackgroundColor"preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other1",@"other2"] settingHandler:^(SLAlertView * _Nonnull alertView) {
95 |
96 | alertView.titleBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
97 | alertView.messageBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
98 | alertView.otherBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
99 | alertView.cancelBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
100 |
101 | } clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
102 |
103 | NSLog(@"block sheet_customBackgroundColor %ld-%@",buttonIndex,buttonTitle);
104 |
105 | }];
106 | }
107 | - (IBAction)sheet_customTextColor:(id)sender {
108 | [SLAlertView alertViewWithTitle:@"actionSheet" message:@"sheet_customTextColor sheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColorsheet_customTextColor" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other1",@"other2",@"other3"] settingHandler:^(SLAlertView * _Nonnull alertView) {
109 |
110 | alertView.titleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
111 | alertView.messageColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
112 | alertView.otherTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
113 | alertView.cancelTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
114 |
115 | }];
116 | }
117 | - (IBAction)sheet_customTextFont:(id)sender {
118 | [SLAlertView alertViewWithTitle:@"actionSheet" message:@"sheet_customTextFont sheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFontsheet_customTextFont" delegate:self preferredStyle:SLAlertViewStyleActionSheet cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other1",@"other2",@"other3"] settingHandler:^(SLAlertView * _Nonnull alertView) {
119 |
120 | alertView.separatorColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
121 | alertView.titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
122 | alertView.messageFont = [UIFont fontWithName:@"AmericanTypewriter" size:16];
123 | alertView.otherTitleFont = [UIFont fontWithName:@"DBLCDTempBlack" size:16];
124 | alertView.cancelTitleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
125 |
126 | }];
127 | }
128 |
129 | #pragma mark - alert
130 | - (IBAction)alert_default:(id)sender {
131 | // 通过block回调方式创建alert
132 | [SLAlertView alertViewWithTitle:@"title"
133 | message:@"message"
134 | preferredStyle:SLAlertViewStyleAlert
135 | cancelButtonTitle:@"cancel"
136 | otherButtonTitles:@[@"other"]
137 | clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
138 |
139 | NSLog(@"block %ld-%@",buttonIndex,buttonTitle);
140 |
141 | }];
142 |
143 | }
144 | - (IBAction)alert_onlyCancel:(id)sender {
145 | // 使用代理
146 | // alert
147 | [SLAlertView alertViewWithTitle:@"title"
148 | message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage"
149 | delegate:self
150 | preferredStyle:SLAlertViewStyleAlert
151 | cancelButtonTitle:@"cancel"
152 | otherButtonTitles:nil];
153 | }
154 | - (IBAction)alert_onlyOneOther:(id)sender {
155 |
156 | [SLAlertView alertViewWithTitle:@"title" message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage" delegate:self preferredStyle:SLAlertViewStyleAlert cancelButtonTitle:nil otherButtonTitles:@[@"other"]];
157 | }
158 | - (IBAction)alert_moreOthers:(id)sender {
159 | [SLAlertView alertViewWithTitle:@"title" message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage" delegate:self preferredStyle:SLAlertViewStyleAlert cancelButtonTitle:nil otherButtonTitles:@[@"other1",@"other2",@"other3"]];
160 | }
161 | - (IBAction)alert_cancelAndMoreOtherbtns:(id)sender {
162 | [SLAlertView alertViewWithTitle:@"title" message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage" delegate:self preferredStyle:SLAlertViewStyleAlert cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other1",@"other2",@"other3"]];
163 | }
164 | - (IBAction)alert_customBackgrondColor:(id)sender {
165 | // block
166 | [SLAlertView alertViewWithTitle:@"title" message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage" preferredStyle:SLAlertViewStyleAlert cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other"] settingHandler:^(SLAlertView * _Nonnull alertView) {
167 | alertView.titleBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
168 | alertView.messageBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
169 | alertView.otherBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
170 | alertView.cancelBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
171 | } clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
172 | NSLog(@"block %ld-%@",buttonIndex,buttonTitle);
173 | }];
174 | }
175 | - (IBAction)alert_customTextColor:(id)sender {
176 | [SLAlertView alertViewWithTitle:@"title" message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage" delegate:self preferredStyle:SLAlertViewStyleAlert cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other"] settingHandler:^(SLAlertView * _Nonnull alertView) {
177 | alertView.titleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
178 | alertView.messageColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
179 | alertView.otherTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
180 | alertView.cancelTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
181 | }];
182 | }
183 | - (IBAction)alert_customTextFont:(id)sender {
184 | [SLAlertView alertViewWithTitle:@"title" message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage" delegate:self preferredStyle:SLAlertViewStyleAlert cancelButtonTitle:@"cancel" otherButtonTitles:@[@"other"] settingHandler:^(SLAlertView * _Nonnull alertView) {
185 | alertView.separatorColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
186 | alertView.titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
187 | alertView.messageFont = [UIFont fontWithName:@"AmericanTypewriter" size:16];
188 | alertView.otherTitleFont = [UIFont fontWithName:@"DBLCDTempBlack" size:16];
189 | alertView.cancelTitleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
190 | }];
191 | }
192 |
193 |
194 | @end
195 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/SLAlert.m:
--------------------------------------------------------------------------------
1 | //
2 | // SLAlert.m
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import "SLAlert.h"
10 | #import "UIView+SLAutolayout.h"
11 |
12 | #define screenW [UIScreen mainScreen].bounds.size.width
13 | #define screenH [UIScreen mainScreen].bounds.size.height
14 | #define alertX 50
15 | #define alertH 400
16 | #define margin_viewToview 1
17 | #define buttonHeight 40
18 | #define titleViewHeight 40
19 | #define margin_btnTobtn 1
20 |
21 | #define DefaultTranslucenceColor [UIColor colorWithRed:255 / 255.0f green:255 / 255.0 blue:255 / 255.0 alpha:0.9]
22 | #define DefaultSeparatorColor [UIColor clearColor]
23 | #define DefaultBackgroundColor DefaultTranslucenceColor
24 | #define DefaultTextColor [UIColor darkGrayColor]
25 | #define DefaultOtherBtnTextColor [UIColor darkGrayColor]
26 | #define DefaultCancelBtnBtnTextColor [UIColor darkGrayColor]
27 |
28 | @interface SLAlert()
29 |
30 | @property (nonatomic, strong) UIView *alertView;
31 | @property (nonatomic, strong) UIView *shadowView;
32 |
33 | @property (nonatomic, copy) void(^clickHandler)(SLAlert * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle);
34 |
35 | @end
36 |
37 | @implementation SLAlert
38 |
39 | + (void)alertWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles settingHandler:(void (^)(SLAlert * _Nonnull))settingHandler clickHandler:(void (^)(SLAlert * _Nonnull, NSInteger, NSString * _Nullable))clickHandler {
40 | UIView *currentView = [SLAlert getCurrentVc].view;
41 |
42 | SLAlert *alert = [[SLAlert alloc] initWithFrame:currentView.frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles handler:settingHandler clickHandler:clickHandler];
43 |
44 | [currentView addSubview:alert];
45 | }
46 |
47 | + (void)alertWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles clickHandler:(void (^)(SLAlert * _Nonnull, NSInteger, NSString * _Nullable))clickHandler {
48 |
49 | UIView *currentView = [SLAlert getCurrentVc].view;
50 |
51 | SLAlert *alert = [[SLAlert alloc] initWithFrame:currentView.frame title:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles handler:nil clickHandler:clickHandler];
52 |
53 | [currentView addSubview:alert];
54 |
55 | }
56 |
57 | + (void)alertWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles settingHandler:(void (^)(SLAlert * _Nonnull))settingHandler {
58 |
59 | UIView *currentView = [SLAlert getCurrentVc].view;
60 |
61 | SLAlert *alert = [[SLAlert alloc] initWithFrame:currentView.frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles handler:settingHandler clickHandler:nil];
62 |
63 | [currentView addSubview:alert];
64 |
65 | }
66 |
67 | + (void)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles {
68 | UIView *currentView = [SLAlert getCurrentVc].view;
69 | SLAlert *alert = [[SLAlert alloc] initWithFrame:currentView.frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles handler:nil clickHandler:nil];
70 | [currentView addSubview:alert];
71 | }
72 |
73 |
74 |
75 | - (instancetype)initWithFrame:(CGRect)frame title:(nullable NSString *)title message:(nullable NSString *)message delegate:(id)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles handler:(void (^)(SLAlert * _Nonnull alert))handler clickHandler:(nullable void (^)(SLAlert * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler
76 | {
77 | self = [super initWithFrame:frame];
78 | if (self) {
79 |
80 | if (handler) {
81 | handler(self);
82 | }
83 |
84 | if (clickHandler) {
85 | _clickHandler = clickHandler;
86 | }
87 |
88 | _delegate = delegate;
89 | UIView *shadowView = [[UIView alloc] initWithFrame:frame];
90 | shadowView.backgroundColor = [UIColor colorWithRed:10 / 255.0 green:10 / 255.0 blue:10 / 255.0 alpha:0.0];
91 | [self addSubview:shadowView];
92 | _shadowView = shadowView;
93 |
94 |
95 |
96 | [self prepareAlertWithFrame:frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles];
97 | // TODO: 记得删掉alert样式的shadow的手势
98 | // [[UIApplication sharedApplication].keyWindow addSubview:self];
99 | // UITapGestureRecognizer *backtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenAlertView)];
100 | // [shadowView addGestureRecognizer:backtap];
101 | }
102 | return self;
103 | }
104 |
105 | - (void)prepareAlertWithFrame:(CGRect)frame title:(nullable NSString *)title message:(nullable NSString *)message delegate:(id)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles {
106 | // alert view
107 | UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(alertX, (screenH - alertH) / 2, screenW - 2 * alertX , alertH)];
108 | alert.backgroundColor = [UIColor colorWithRed:10 / 255.0 green:10 / 255.0 blue:10 / 255.0 alpha:0.0];
109 | alert.layer.cornerRadius = 15.0f;
110 | alert.layer.masksToBounds = YES;
111 | [self addSubview:alert];
112 | _alertView = alert;
113 |
114 | // title view
115 | UIView *titleView = [[UIView alloc] init];
116 | [alert addSubview:titleView];
117 |
118 | [titleView addConstraint:NSLayoutAttributeLeft equalTo:alert offset:0];
119 | [titleView addConstraint:NSLayoutAttributeRight equalTo:alert offset:0];
120 | [titleView addConstraint:NSLayoutAttributeTop equalTo:alert offset:0];
121 | [titleView addConstraint:NSLayoutAttributeHeight equalTo:nil offset:titleViewHeight];
122 |
123 | [titleView layoutIfNeeded];
124 |
125 | // title label
126 | UILabel *titleLabel = [[UILabel alloc] init];
127 | titleLabel.textAlignment = NSTextAlignmentCenter;
128 | titleLabel.text = title;
129 | if (_titleFont) {
130 | titleLabel.font = _titleFont;
131 | } else {
132 | titleLabel.font = [UIFont boldSystemFontOfSize:16];
133 | }
134 |
135 | [titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
136 | titleLabel.numberOfLines = 0;
137 |
138 | [titleView addSubview:titleLabel];
139 |
140 | [titleLabel addConstraint:NSLayoutAttributeLeft equalTo:titleView offset:0];
141 | [titleLabel addConstraint:NSLayoutAttributeRight equalTo:titleView offset:0];
142 | [titleLabel addConstraint:NSLayoutAttributeCenterY equalTo:titleView offset:0];
143 |
144 | UIView *line_title_msg =[[UIView alloc] init];
145 | if (_separatorColor) {
146 | line_title_msg.backgroundColor = _separatorColor;
147 | } else {
148 | line_title_msg.backgroundColor = DefaultSeparatorColor;
149 | }
150 | [alert addSubview:line_title_msg];
151 | [line_title_msg addConstraint:NSLayoutAttributeHeight equalTo:nil offset:1];
152 | [line_title_msg addConstraint:NSLayoutAttributeTop equalTo:titleView toAttribute:NSLayoutAttributeBottom offset:0];
153 | [line_title_msg addConstraint:NSLayoutAttributeLeft equalTo:alert offset:0];
154 | [line_title_msg addConstraint:NSLayoutAttributeRight equalTo:alert offset:0];
155 |
156 | // message view
157 | UIView *messageView = [[UIView alloc] init];
158 | messageView.backgroundColor = [UIColor whiteColor];
159 | [alert addSubview:messageView];
160 |
161 | [messageView addConstraint:NSLayoutAttributeLeft equalTo:alert offset:0];
162 | [messageView addConstraint:NSLayoutAttributeRight equalTo:alert offset:0];
163 | [messageView addConstraint:NSLayoutAttributeTop equalTo:titleView toAttribute:NSLayoutAttributeBottom offset:margin_viewToview];
164 |
165 | // message label
166 | UILabel *messageLabel = [[UILabel alloc] init];
167 | messageLabel.textAlignment = NSTextAlignmentCenter;
168 | if (_messageFont) {
169 | messageLabel.font = _messageFont;
170 | } else {
171 | messageLabel.font = [UIFont systemFontOfSize:14];
172 | }
173 |
174 | messageLabel.text = message;
175 | [messageLabel setLineBreakMode:NSLineBreakByWordWrapping];
176 | messageLabel.numberOfLines = 0;
177 | [alert addSubview:messageLabel];
178 |
179 | [messageLabel addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeTop offset:10];
180 | [messageLabel addConstraint:NSLayoutAttributeLeft equalTo:alert offset:20];
181 | [messageLabel addConstraint:NSLayoutAttributeRight equalTo:alert offset:-20];
182 |
183 | [messageView addConstraint:NSLayoutAttributeHeight equalTo:messageLabel offset:20];
184 |
185 | [messageLabel layoutIfNeeded];
186 | [messageView layoutIfNeeded];
187 |
188 |
189 | // cancel button
190 | UIButton *cancelBtn = [[UIButton alloc] init];
191 | if (cancelButtonTitle) {
192 | cancelBtn.tag = 0;
193 | if (_cancelTitleFont) {
194 | cancelBtn.titleLabel.font = _cancelTitleFont;
195 | } else {
196 | cancelBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
197 | }
198 |
199 | if (_cancelBackgroundColor) {
200 | [cancelBtn setBackgroundColor:_cancelBackgroundColor];
201 | } else {
202 | [cancelBtn setBackgroundColor:[UIColor whiteColor]];
203 | }
204 | if (_cancelBackgroundColor) {
205 | [cancelBtn setBackgroundColor:_cancelBackgroundColor];
206 | } else {
207 | [cancelBtn setBackgroundColor:DefaultBackgroundColor];
208 | }
209 | [cancelBtn setTitle:cancelButtonTitle forState:UIControlStateNormal];
210 | if (_cancelTitleColor) {
211 | [cancelBtn setTitleColor:_cancelTitleColor forState:UIControlStateNormal];
212 | } else {
213 | [cancelBtn setTitleColor:DefaultCancelBtnBtnTextColor forState:UIControlStateNormal];
214 | }
215 | [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
216 | [alert addSubview:cancelBtn];
217 |
218 | [cancelBtn addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeBottom offset:margin_viewToview];
219 | [cancelBtn addConstraint:NSLayoutAttributeLeft equalTo:alert toAttribute:NSLayoutAttributeLeft offset:0];
220 | [cancelBtn addConstraint:NSLayoutAttributeHeight equalTo:nil offset:buttonHeight];
221 | if (otherButtonTitles.count) {
222 | [cancelBtn addConstraint:NSLayoutAttributeWidth equalTo:nil offset:(screenW - 2 * alertX) / (otherButtonTitles.count + 1)];
223 | } else {
224 | [cancelBtn addConstraint:NSLayoutAttributeWidth equalTo:nil offset:(screenW - 2 * alertX) / 1];
225 | }
226 |
227 | UIView *line_msg_btn =[[UIView alloc] init];
228 | if (_separatorColor) {
229 | line_msg_btn.backgroundColor = _separatorColor;
230 | } else {
231 | line_title_msg.backgroundColor = DefaultSeparatorColor;
232 | }
233 |
234 | [alert addSubview:line_msg_btn];
235 |
236 | [line_msg_btn addConstraint:NSLayoutAttributeHeight equalTo:nil offset:1];
237 | [line_msg_btn addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeBottom offset:0];
238 | [line_msg_btn addConstraint:NSLayoutAttributeLeft equalTo:alert offset:0];
239 | [line_msg_btn addConstraint:NSLayoutAttributeRight equalTo:alert offset:0];
240 |
241 | [cancelBtn layoutIfNeeded];
242 | }
243 |
244 | // other button
245 | NSMutableArray *otherBtns = [NSMutableArray array];
246 | if (otherButtonTitles.count) {
247 |
248 | [otherButtonTitles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
249 | UIButton *otherBtn = [[UIButton alloc] init];
250 | if (_otherTitleFont) {
251 | otherBtn.titleLabel.font = _otherTitleFont;
252 | } else {
253 | otherBtn.titleLabel.font = [UIFont systemFontOfSize:16];
254 | }
255 |
256 | [otherBtn setBackgroundColor:[UIColor whiteColor]];
257 | [otherBtns addObject:otherBtn];
258 | if (_otherTitleColor) {
259 | [otherBtn setTitleColor:_otherTitleColor forState:UIControlStateNormal];
260 | } else {
261 | [otherBtn setTitleColor:DefaultOtherBtnTextColor forState:UIControlStateNormal];
262 | }
263 | if (_otherBackgroundColor) {
264 | [otherBtn setBackgroundColor:_otherBackgroundColor];
265 | } else {
266 | [otherBtn setBackgroundColor:DefaultBackgroundColor];
267 | }
268 | [otherBtn setTitle:obj forState:UIControlStateNormal];
269 | [otherBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
270 | [alert addSubview:otherBtn];
271 |
272 | [otherBtn addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeBottom offset:margin_viewToview];
273 | [otherBtn addConstraint:NSLayoutAttributeHeight equalTo:nil offset:buttonHeight];
274 |
275 | // separator
276 | UIView *line_otherBtn =[[UIView alloc] init];
277 | if (_separatorColor) {
278 | line_otherBtn.backgroundColor = _separatorColor;
279 | } else {
280 | line_otherBtn.backgroundColor = DefaultSeparatorColor;
281 | }
282 | [alert addSubview:line_otherBtn];
283 |
284 | [line_otherBtn addConstraint:NSLayoutAttributeWidth equalTo:nil offset:1];
285 | [line_otherBtn addConstraint:NSLayoutAttributeHeight equalTo:otherBtn offset:0];
286 | [line_otherBtn addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeBottom offset:margin_viewToview];
287 |
288 | if (cancelButtonTitle) {
289 | otherBtn.tag = idx + 1;
290 | [otherBtn addConstraint:NSLayoutAttributeWidth equalTo:nil offset:(screenW - 2 * alertX) / (otherButtonTitles.count + 1)];
291 |
292 | if (idx == 0) {
293 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:cancelBtn toAttribute:NSLayoutAttributeRight offset:margin_btnTobtn];
294 | [line_otherBtn addConstraint:NSLayoutAttributeLeft equalTo:cancelBtn toAttribute:NSLayoutAttributeRight offset:0];
295 | } else {
296 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeRight offset:margin_btnTobtn];
297 | [line_otherBtn addConstraint:NSLayoutAttributeLeft equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeRight offset:0];
298 |
299 | }
300 | } else {
301 | otherBtn.tag = idx;
302 | [otherBtn addConstraint:NSLayoutAttributeWidth equalTo:nil offset:(screenW - 2 * alertX) / otherButtonTitles.count];
303 | if (idx == 0) {
304 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:alert toAttribute:NSLayoutAttributeLeft offset:0];
305 | [line_otherBtn addConstraint:NSLayoutAttributeLeft equalTo:otherBtn toAttribute:NSLayoutAttributeRight offset:0];
306 | } else {
307 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeRight offset:margin_btnTobtn];
308 | [line_otherBtn addConstraint:NSLayoutAttributeLeft equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeRight offset:0];
309 | }
310 | }
311 | [otherBtn layoutIfNeeded];
312 |
313 | }];
314 | }
315 |
316 | [alert layoutIfNeeded];
317 |
318 | [self setupColorsForTitleView:titleView titleLabel:titleLabel messageView:messageView messageLabel:messageLabel];
319 |
320 | // animate show
321 | // [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.4 options:0 animations:^{
322 | [UIView animateWithDuration:0.25 animations:^{
323 | _shadowView.backgroundColor = [UIColor colorWithRed:10 / 255.0 green:10 / 255.0 blue:10 / 255.0 alpha:0.4];
324 | }];
325 |
326 | // 根据内容更新高度
327 | CGRect alertFrame = alert.frame;
328 | alertFrame.origin.x = alertX;
329 | alertFrame.size.width = screenW - 2 * alertX;
330 | if (cancelButtonTitle && otherButtonTitles.count == 0) {
331 | alertFrame.size.height = CGRectGetMaxY(cancelBtn.frame);
332 | } else {
333 | alertFrame.size.height = CGRectGetMaxY(alert.subviews.lastObject.frame);
334 | }
335 | alertFrame.origin.y = (screenH - alertFrame.size.height) / 2;
336 |
337 | alert.frame = alertFrame;
338 |
339 | // } completion:^(BOOL finished) {
340 | //
341 | // }];
342 |
343 | CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
344 |
345 | animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1)],
346 | [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.01, 1.01, 1)],
347 | [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1)]];
348 | animation.keyTimes = @[ @0, @0.5, @1 ];
349 | animation.fillMode = kCAFillModeForwards;
350 | animation.removedOnCompletion = NO;
351 | animation.duration = 0.4;
352 |
353 | [self.alertView.layer addAnimation:animation forKey:@"showAlert"];
354 |
355 |
356 | }
357 |
358 | - (void)setupColorsForTitleView:(UIView *)titleView titleLabel:(UILabel *)titleLabel messageView:(UIView *)messageView messageLabel:(UILabel *)messageLabel {
359 | if (_titleBackgroundColor) {
360 | titleView.backgroundColor = _titleBackgroundColor;
361 | } else {
362 | titleView.backgroundColor = DefaultBackgroundColor;
363 | }
364 | if (_titleColor) {
365 | titleLabel.textColor = _titleColor;
366 | } else {
367 | titleLabel.textColor = DefaultTextColor;
368 | }
369 | if (_messageBackgroundColor) {
370 | messageView.backgroundColor = _messageBackgroundColor;
371 | } else {
372 | messageView.backgroundColor = DefaultBackgroundColor;
373 | }
374 | if (_messageColor) {
375 | messageLabel.textColor = _messageColor;
376 | } else {
377 | messageLabel.textColor = DefaultTextColor;
378 | }
379 | }
380 |
381 | - (void)buttonClick:(UIButton *)sender {
382 | [self hidenAlertView];
383 | if ([self.delegate respondsToSelector:@selector(alertView:didSelectedButtonWithButtonIndex:buttonTitle:)]) {
384 | [self.delegate alertView:self didSelectedButtonWithButtonIndex:sender.tag buttonTitle:sender.titleLabel.text];
385 | }
386 |
387 | if (_clickHandler) {
388 | _clickHandler(self,sender.tag,sender.titleLabel.text);
389 | }
390 |
391 | }
392 |
393 | - (void)hidenAlertView {
394 | [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:0 animations:^{
395 |
396 | _shadowView.alpha = 0.0f;
397 | _alertView.alpha = 0.0f;
398 |
399 | } completion:^(BOOL finished) {
400 | [self removeFromSuperview];
401 | }];
402 |
403 | }
404 |
405 |
406 |
407 | // 获取当前控制器的view
408 | - (UIView *)getCurrentView {
409 | return [self getCurrentVC].view;
410 | }
411 |
412 | //获取当前屏幕显示的viewcontroller
413 | - (UIViewController *)getCurrentVC
414 | {
415 | UIViewController *result = nil;
416 |
417 | UIWindow * window = [[UIApplication sharedApplication] keyWindow];
418 | if (window.windowLevel != UIWindowLevelNormal)
419 | {
420 | NSArray *windows = [[UIApplication sharedApplication] windows];
421 | for(UIWindow * tmpWin in windows)
422 | {
423 | if (tmpWin.windowLevel == UIWindowLevelNormal)
424 | {
425 | window = tmpWin;
426 | break;
427 | }
428 | }
429 | }
430 |
431 | UIView *frontView = [[window subviews] objectAtIndex:0];
432 | id nextResponder = [frontView nextResponder];
433 |
434 | if ([nextResponder isKindOfClass:[UIViewController class]])
435 | result = nextResponder;
436 | else
437 | result = window.rootViewController;
438 |
439 | return result;
440 | }
441 |
442 | + (UIViewController *)getCurrentVc
443 | {
444 | UIViewController *result = nil;
445 |
446 | UIWindow * window = [[UIApplication sharedApplication] keyWindow];
447 | if (window.windowLevel != UIWindowLevelNormal)
448 | {
449 | NSArray *windows = [[UIApplication sharedApplication] windows];
450 | for(UIWindow * tmpWin in windows)
451 | {
452 | if (tmpWin.windowLevel == UIWindowLevelNormal)
453 | {
454 | window = tmpWin;
455 | break;
456 | }
457 | }
458 | }
459 |
460 | UIView *frontView = [[window subviews] objectAtIndex:0];
461 | id nextResponder = [frontView nextResponder];
462 |
463 | if ([nextResponder isKindOfClass:[UIViewController class]])
464 | result = nextResponder;
465 | else
466 | result = window.rootViewController;
467 |
468 | return result;
469 | }
470 |
471 |
472 | @end
473 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | DA68DD491E83A8D100ADB8FE /* SLActionSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = DA68DD421E83A8D100ADB8FE /* SLActionSheet.m */; };
11 | DA68DD4A1E83A8D100ADB8FE /* SLAlert.m in Sources */ = {isa = PBXBuildFile; fileRef = DA68DD441E83A8D100ADB8FE /* SLAlert.m */; };
12 | DA68DD4B1E83A8D100ADB8FE /* SLAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = DA68DD461E83A8D100ADB8FE /* SLAlertView.m */; };
13 | DA68DD4C1E83A8D100ADB8FE /* UIView+SLAutolayout.m in Sources */ = {isa = PBXBuildFile; fileRef = DA68DD481E83A8D100ADB8FE /* UIView+SLAutolayout.m */; };
14 | DAA0A68E1E81363600568859 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA0A68D1E81363600568859 /* main.m */; };
15 | DAA0A6911E81363600568859 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA0A6901E81363600568859 /* AppDelegate.m */; };
16 | DAA0A6941E81363600568859 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA0A6931E81363600568859 /* ViewController.m */; };
17 | DAA0A6971E81363600568859 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAA0A6951E81363600568859 /* Main.storyboard */; };
18 | DAA0A6991E81363600568859 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DAA0A6981E81363600568859 /* Assets.xcassets */; };
19 | DAA0A69C1E81363600568859 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAA0A69A1E81363600568859 /* LaunchScreen.storyboard */; };
20 | DAA0A6A71E81363700568859 /* SLAlertViewDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA0A6A61E81363700568859 /* SLAlertViewDemoTests.m */; };
21 | DAA0A6B21E81363700568859 /* SLAlertViewDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA0A6B11E81363700568859 /* SLAlertViewDemoUITests.m */; };
22 | /* End PBXBuildFile section */
23 |
24 | /* Begin PBXContainerItemProxy section */
25 | DAA0A6A31E81363700568859 /* PBXContainerItemProxy */ = {
26 | isa = PBXContainerItemProxy;
27 | containerPortal = DAA0A6811E81363600568859 /* Project object */;
28 | proxyType = 1;
29 | remoteGlobalIDString = DAA0A6881E81363600568859;
30 | remoteInfo = SLAlertViewDemo;
31 | };
32 | DAA0A6AE1E81363700568859 /* PBXContainerItemProxy */ = {
33 | isa = PBXContainerItemProxy;
34 | containerPortal = DAA0A6811E81363600568859 /* Project object */;
35 | proxyType = 1;
36 | remoteGlobalIDString = DAA0A6881E81363600568859;
37 | remoteInfo = SLAlertViewDemo;
38 | };
39 | /* End PBXContainerItemProxy section */
40 |
41 | /* Begin PBXFileReference section */
42 | DA68DD411E83A8D100ADB8FE /* SLActionSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SLActionSheet.h; sourceTree = ""; };
43 | DA68DD421E83A8D100ADB8FE /* SLActionSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SLActionSheet.m; sourceTree = ""; };
44 | DA68DD431E83A8D100ADB8FE /* SLAlert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SLAlert.h; sourceTree = ""; };
45 | DA68DD441E83A8D100ADB8FE /* SLAlert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SLAlert.m; sourceTree = ""; };
46 | DA68DD451E83A8D100ADB8FE /* SLAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SLAlertView.h; sourceTree = ""; };
47 | DA68DD461E83A8D100ADB8FE /* SLAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SLAlertView.m; sourceTree = ""; };
48 | DA68DD471E83A8D100ADB8FE /* UIView+SLAutolayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+SLAutolayout.h"; sourceTree = ""; };
49 | DA68DD481E83A8D100ADB8FE /* UIView+SLAutolayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+SLAutolayout.m"; sourceTree = ""; };
50 | DAA0A6891E81363600568859 /* SLAlertViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SLAlertViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
51 | DAA0A68D1E81363600568859 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; };
52 | DAA0A68F1E81363600568859 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; };
53 | DAA0A6901E81363600568859 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; };
54 | DAA0A6921E81363600568859 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; };
55 | DAA0A6931E81363600568859 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; };
56 | DAA0A6961E81363600568859 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
57 | DAA0A6981E81363600568859 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
58 | DAA0A69B1E81363600568859 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
59 | DAA0A69D1E81363600568859 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
60 | DAA0A6A21E81363700568859 /* SLAlertViewDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SLAlertViewDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
61 | DAA0A6A61E81363700568859 /* SLAlertViewDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SLAlertViewDemoTests.m; sourceTree = ""; };
62 | DAA0A6A81E81363700568859 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
63 | DAA0A6AD1E81363700568859 /* SLAlertViewDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SLAlertViewDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
64 | DAA0A6B11E81363700568859 /* SLAlertViewDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SLAlertViewDemoUITests.m; sourceTree = ""; };
65 | DAA0A6B31E81363700568859 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
66 | /* End PBXFileReference section */
67 |
68 | /* Begin PBXFrameworksBuildPhase section */
69 | DAA0A6861E81363600568859 /* Frameworks */ = {
70 | isa = PBXFrameworksBuildPhase;
71 | buildActionMask = 2147483647;
72 | files = (
73 | );
74 | runOnlyForDeploymentPostprocessing = 0;
75 | };
76 | DAA0A69F1E81363700568859 /* Frameworks */ = {
77 | isa = PBXFrameworksBuildPhase;
78 | buildActionMask = 2147483647;
79 | files = (
80 | );
81 | runOnlyForDeploymentPostprocessing = 0;
82 | };
83 | DAA0A6AA1E81363700568859 /* Frameworks */ = {
84 | isa = PBXFrameworksBuildPhase;
85 | buildActionMask = 2147483647;
86 | files = (
87 | );
88 | runOnlyForDeploymentPostprocessing = 0;
89 | };
90 | /* End PBXFrameworksBuildPhase section */
91 |
92 | /* Begin PBXGroup section */
93 | DA68DD401E83A8D100ADB8FE /* SLAlertView */ = {
94 | isa = PBXGroup;
95 | children = (
96 | DA68DD411E83A8D100ADB8FE /* SLActionSheet.h */,
97 | DA68DD421E83A8D100ADB8FE /* SLActionSheet.m */,
98 | DA68DD431E83A8D100ADB8FE /* SLAlert.h */,
99 | DA68DD441E83A8D100ADB8FE /* SLAlert.m */,
100 | DA68DD451E83A8D100ADB8FE /* SLAlertView.h */,
101 | DA68DD461E83A8D100ADB8FE /* SLAlertView.m */,
102 | DA68DD471E83A8D100ADB8FE /* UIView+SLAutolayout.h */,
103 | DA68DD481E83A8D100ADB8FE /* UIView+SLAutolayout.m */,
104 | );
105 | path = SLAlertView;
106 | sourceTree = "";
107 | };
108 | DAA0A6801E81363600568859 = {
109 | isa = PBXGroup;
110 | children = (
111 | DAA0A68B1E81363600568859 /* SLAlertViewDemo */,
112 | DAA0A6A51E81363700568859 /* SLAlertViewDemoTests */,
113 | DAA0A6B01E81363700568859 /* SLAlertViewDemoUITests */,
114 | DAA0A68A1E81363600568859 /* Products */,
115 | );
116 | sourceTree = "";
117 | };
118 | DAA0A68A1E81363600568859 /* Products */ = {
119 | isa = PBXGroup;
120 | children = (
121 | DAA0A6891E81363600568859 /* SLAlertViewDemo.app */,
122 | DAA0A6A21E81363700568859 /* SLAlertViewDemoTests.xctest */,
123 | DAA0A6AD1E81363700568859 /* SLAlertViewDemoUITests.xctest */,
124 | );
125 | name = Products;
126 | sourceTree = "";
127 | };
128 | DAA0A68B1E81363600568859 /* SLAlertViewDemo */ = {
129 | isa = PBXGroup;
130 | children = (
131 | DA68DD401E83A8D100ADB8FE /* SLAlertView */,
132 | DAA0A68F1E81363600568859 /* AppDelegate.h */,
133 | DAA0A6901E81363600568859 /* AppDelegate.m */,
134 | DAA0A6921E81363600568859 /* ViewController.h */,
135 | DAA0A6931E81363600568859 /* ViewController.m */,
136 | DAA0A6951E81363600568859 /* Main.storyboard */,
137 | DAA0A6981E81363600568859 /* Assets.xcassets */,
138 | DAA0A69A1E81363600568859 /* LaunchScreen.storyboard */,
139 | DAA0A69D1E81363600568859 /* Info.plist */,
140 | DAA0A68C1E81363600568859 /* Supporting Files */,
141 | );
142 | path = SLAlertViewDemo;
143 | sourceTree = "";
144 | };
145 | DAA0A68C1E81363600568859 /* Supporting Files */ = {
146 | isa = PBXGroup;
147 | children = (
148 | DAA0A68D1E81363600568859 /* main.m */,
149 | );
150 | name = "Supporting Files";
151 | sourceTree = "";
152 | };
153 | DAA0A6A51E81363700568859 /* SLAlertViewDemoTests */ = {
154 | isa = PBXGroup;
155 | children = (
156 | DAA0A6A61E81363700568859 /* SLAlertViewDemoTests.m */,
157 | DAA0A6A81E81363700568859 /* Info.plist */,
158 | );
159 | path = SLAlertViewDemoTests;
160 | sourceTree = "";
161 | };
162 | DAA0A6B01E81363700568859 /* SLAlertViewDemoUITests */ = {
163 | isa = PBXGroup;
164 | children = (
165 | DAA0A6B11E81363700568859 /* SLAlertViewDemoUITests.m */,
166 | DAA0A6B31E81363700568859 /* Info.plist */,
167 | );
168 | path = SLAlertViewDemoUITests;
169 | sourceTree = "";
170 | };
171 | /* End PBXGroup section */
172 |
173 | /* Begin PBXNativeTarget section */
174 | DAA0A6881E81363600568859 /* SLAlertViewDemo */ = {
175 | isa = PBXNativeTarget;
176 | buildConfigurationList = DAA0A6B61E81363700568859 /* Build configuration list for PBXNativeTarget "SLAlertViewDemo" */;
177 | buildPhases = (
178 | DAA0A6851E81363600568859 /* Sources */,
179 | DAA0A6861E81363600568859 /* Frameworks */,
180 | DAA0A6871E81363600568859 /* Resources */,
181 | );
182 | buildRules = (
183 | );
184 | dependencies = (
185 | );
186 | name = SLAlertViewDemo;
187 | productName = SLAlertViewDemo;
188 | productReference = DAA0A6891E81363600568859 /* SLAlertViewDemo.app */;
189 | productType = "com.apple.product-type.application";
190 | };
191 | DAA0A6A11E81363700568859 /* SLAlertViewDemoTests */ = {
192 | isa = PBXNativeTarget;
193 | buildConfigurationList = DAA0A6B91E81363700568859 /* Build configuration list for PBXNativeTarget "SLAlertViewDemoTests" */;
194 | buildPhases = (
195 | DAA0A69E1E81363700568859 /* Sources */,
196 | DAA0A69F1E81363700568859 /* Frameworks */,
197 | DAA0A6A01E81363700568859 /* Resources */,
198 | );
199 | buildRules = (
200 | );
201 | dependencies = (
202 | DAA0A6A41E81363700568859 /* PBXTargetDependency */,
203 | );
204 | name = SLAlertViewDemoTests;
205 | productName = SLAlertViewDemoTests;
206 | productReference = DAA0A6A21E81363700568859 /* SLAlertViewDemoTests.xctest */;
207 | productType = "com.apple.product-type.bundle.unit-test";
208 | };
209 | DAA0A6AC1E81363700568859 /* SLAlertViewDemoUITests */ = {
210 | isa = PBXNativeTarget;
211 | buildConfigurationList = DAA0A6BC1E81363700568859 /* Build configuration list for PBXNativeTarget "SLAlertViewDemoUITests" */;
212 | buildPhases = (
213 | DAA0A6A91E81363700568859 /* Sources */,
214 | DAA0A6AA1E81363700568859 /* Frameworks */,
215 | DAA0A6AB1E81363700568859 /* Resources */,
216 | );
217 | buildRules = (
218 | );
219 | dependencies = (
220 | DAA0A6AF1E81363700568859 /* PBXTargetDependency */,
221 | );
222 | name = SLAlertViewDemoUITests;
223 | productName = SLAlertViewDemoUITests;
224 | productReference = DAA0A6AD1E81363700568859 /* SLAlertViewDemoUITests.xctest */;
225 | productType = "com.apple.product-type.bundle.ui-testing";
226 | };
227 | /* End PBXNativeTarget section */
228 |
229 | /* Begin PBXProject section */
230 | DAA0A6811E81363600568859 /* Project object */ = {
231 | isa = PBXProject;
232 | attributes = {
233 | LastUpgradeCheck = 0810;
234 | ORGANIZATIONNAME = Pandai;
235 | TargetAttributes = {
236 | DAA0A6881E81363600568859 = {
237 | CreatedOnToolsVersion = 8.1;
238 | DevelopmentTeam = WE2ULLEE7K;
239 | ProvisioningStyle = Automatic;
240 | };
241 | DAA0A6A11E81363700568859 = {
242 | CreatedOnToolsVersion = 8.1;
243 | DevelopmentTeam = WE2ULLEE7K;
244 | ProvisioningStyle = Automatic;
245 | TestTargetID = DAA0A6881E81363600568859;
246 | };
247 | DAA0A6AC1E81363700568859 = {
248 | CreatedOnToolsVersion = 8.1;
249 | DevelopmentTeam = WE2ULLEE7K;
250 | ProvisioningStyle = Automatic;
251 | TestTargetID = DAA0A6881E81363600568859;
252 | };
253 | };
254 | };
255 | buildConfigurationList = DAA0A6841E81363600568859 /* Build configuration list for PBXProject "SLAlertViewDemo" */;
256 | compatibilityVersion = "Xcode 3.2";
257 | developmentRegion = English;
258 | hasScannedForEncodings = 0;
259 | knownRegions = (
260 | en,
261 | Base,
262 | );
263 | mainGroup = DAA0A6801E81363600568859;
264 | productRefGroup = DAA0A68A1E81363600568859 /* Products */;
265 | projectDirPath = "";
266 | projectRoot = "";
267 | targets = (
268 | DAA0A6881E81363600568859 /* SLAlertViewDemo */,
269 | DAA0A6A11E81363700568859 /* SLAlertViewDemoTests */,
270 | DAA0A6AC1E81363700568859 /* SLAlertViewDemoUITests */,
271 | );
272 | };
273 | /* End PBXProject section */
274 |
275 | /* Begin PBXResourcesBuildPhase section */
276 | DAA0A6871E81363600568859 /* Resources */ = {
277 | isa = PBXResourcesBuildPhase;
278 | buildActionMask = 2147483647;
279 | files = (
280 | DAA0A69C1E81363600568859 /* LaunchScreen.storyboard in Resources */,
281 | DAA0A6991E81363600568859 /* Assets.xcassets in Resources */,
282 | DAA0A6971E81363600568859 /* Main.storyboard in Resources */,
283 | );
284 | runOnlyForDeploymentPostprocessing = 0;
285 | };
286 | DAA0A6A01E81363700568859 /* Resources */ = {
287 | isa = PBXResourcesBuildPhase;
288 | buildActionMask = 2147483647;
289 | files = (
290 | );
291 | runOnlyForDeploymentPostprocessing = 0;
292 | };
293 | DAA0A6AB1E81363700568859 /* Resources */ = {
294 | isa = PBXResourcesBuildPhase;
295 | buildActionMask = 2147483647;
296 | files = (
297 | );
298 | runOnlyForDeploymentPostprocessing = 0;
299 | };
300 | /* End PBXResourcesBuildPhase section */
301 |
302 | /* Begin PBXSourcesBuildPhase section */
303 | DAA0A6851E81363600568859 /* Sources */ = {
304 | isa = PBXSourcesBuildPhase;
305 | buildActionMask = 2147483647;
306 | files = (
307 | DA68DD4C1E83A8D100ADB8FE /* UIView+SLAutolayout.m in Sources */,
308 | DA68DD491E83A8D100ADB8FE /* SLActionSheet.m in Sources */,
309 | DAA0A6941E81363600568859 /* ViewController.m in Sources */,
310 | DAA0A6911E81363600568859 /* AppDelegate.m in Sources */,
311 | DAA0A68E1E81363600568859 /* main.m in Sources */,
312 | DA68DD4B1E83A8D100ADB8FE /* SLAlertView.m in Sources */,
313 | DA68DD4A1E83A8D100ADB8FE /* SLAlert.m in Sources */,
314 | );
315 | runOnlyForDeploymentPostprocessing = 0;
316 | };
317 | DAA0A69E1E81363700568859 /* Sources */ = {
318 | isa = PBXSourcesBuildPhase;
319 | buildActionMask = 2147483647;
320 | files = (
321 | DAA0A6A71E81363700568859 /* SLAlertViewDemoTests.m in Sources */,
322 | );
323 | runOnlyForDeploymentPostprocessing = 0;
324 | };
325 | DAA0A6A91E81363700568859 /* Sources */ = {
326 | isa = PBXSourcesBuildPhase;
327 | buildActionMask = 2147483647;
328 | files = (
329 | DAA0A6B21E81363700568859 /* SLAlertViewDemoUITests.m in Sources */,
330 | );
331 | runOnlyForDeploymentPostprocessing = 0;
332 | };
333 | /* End PBXSourcesBuildPhase section */
334 |
335 | /* Begin PBXTargetDependency section */
336 | DAA0A6A41E81363700568859 /* PBXTargetDependency */ = {
337 | isa = PBXTargetDependency;
338 | target = DAA0A6881E81363600568859 /* SLAlertViewDemo */;
339 | targetProxy = DAA0A6A31E81363700568859 /* PBXContainerItemProxy */;
340 | };
341 | DAA0A6AF1E81363700568859 /* PBXTargetDependency */ = {
342 | isa = PBXTargetDependency;
343 | target = DAA0A6881E81363600568859 /* SLAlertViewDemo */;
344 | targetProxy = DAA0A6AE1E81363700568859 /* PBXContainerItemProxy */;
345 | };
346 | /* End PBXTargetDependency section */
347 |
348 | /* Begin PBXVariantGroup section */
349 | DAA0A6951E81363600568859 /* Main.storyboard */ = {
350 | isa = PBXVariantGroup;
351 | children = (
352 | DAA0A6961E81363600568859 /* Base */,
353 | );
354 | name = Main.storyboard;
355 | sourceTree = "";
356 | };
357 | DAA0A69A1E81363600568859 /* LaunchScreen.storyboard */ = {
358 | isa = PBXVariantGroup;
359 | children = (
360 | DAA0A69B1E81363600568859 /* Base */,
361 | );
362 | name = LaunchScreen.storyboard;
363 | sourceTree = "";
364 | };
365 | /* End PBXVariantGroup section */
366 |
367 | /* Begin XCBuildConfiguration section */
368 | DAA0A6B41E81363700568859 /* Debug */ = {
369 | isa = XCBuildConfiguration;
370 | buildSettings = {
371 | ALWAYS_SEARCH_USER_PATHS = NO;
372 | CLANG_ANALYZER_NONNULL = YES;
373 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
374 | CLANG_CXX_LIBRARY = "libc++";
375 | CLANG_ENABLE_MODULES = YES;
376 | CLANG_ENABLE_OBJC_ARC = YES;
377 | CLANG_WARN_BOOL_CONVERSION = YES;
378 | CLANG_WARN_CONSTANT_CONVERSION = YES;
379 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
380 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
381 | CLANG_WARN_EMPTY_BODY = YES;
382 | CLANG_WARN_ENUM_CONVERSION = YES;
383 | CLANG_WARN_INFINITE_RECURSION = YES;
384 | CLANG_WARN_INT_CONVERSION = YES;
385 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
386 | CLANG_WARN_SUSPICIOUS_MOVES = YES;
387 | CLANG_WARN_UNREACHABLE_CODE = YES;
388 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
389 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
390 | COPY_PHASE_STRIP = NO;
391 | DEBUG_INFORMATION_FORMAT = dwarf;
392 | ENABLE_STRICT_OBJC_MSGSEND = YES;
393 | ENABLE_TESTABILITY = YES;
394 | GCC_C_LANGUAGE_STANDARD = gnu99;
395 | GCC_DYNAMIC_NO_PIC = NO;
396 | GCC_NO_COMMON_BLOCKS = YES;
397 | GCC_OPTIMIZATION_LEVEL = 0;
398 | GCC_PREPROCESSOR_DEFINITIONS = (
399 | "DEBUG=1",
400 | "$(inherited)",
401 | );
402 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
403 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
404 | GCC_WARN_UNDECLARED_SELECTOR = YES;
405 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
406 | GCC_WARN_UNUSED_FUNCTION = YES;
407 | GCC_WARN_UNUSED_VARIABLE = YES;
408 | IPHONEOS_DEPLOYMENT_TARGET = 10.1;
409 | MTL_ENABLE_DEBUG_INFO = YES;
410 | ONLY_ACTIVE_ARCH = YES;
411 | SDKROOT = iphoneos;
412 | TARGETED_DEVICE_FAMILY = "1,2";
413 | };
414 | name = Debug;
415 | };
416 | DAA0A6B51E81363700568859 /* Release */ = {
417 | isa = XCBuildConfiguration;
418 | buildSettings = {
419 | ALWAYS_SEARCH_USER_PATHS = NO;
420 | CLANG_ANALYZER_NONNULL = YES;
421 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
422 | CLANG_CXX_LIBRARY = "libc++";
423 | CLANG_ENABLE_MODULES = YES;
424 | CLANG_ENABLE_OBJC_ARC = YES;
425 | CLANG_WARN_BOOL_CONVERSION = YES;
426 | CLANG_WARN_CONSTANT_CONVERSION = YES;
427 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
428 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
429 | CLANG_WARN_EMPTY_BODY = YES;
430 | CLANG_WARN_ENUM_CONVERSION = YES;
431 | CLANG_WARN_INFINITE_RECURSION = YES;
432 | CLANG_WARN_INT_CONVERSION = YES;
433 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
434 | CLANG_WARN_SUSPICIOUS_MOVES = YES;
435 | CLANG_WARN_UNREACHABLE_CODE = YES;
436 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
437 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
438 | COPY_PHASE_STRIP = NO;
439 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
440 | ENABLE_NS_ASSERTIONS = NO;
441 | ENABLE_STRICT_OBJC_MSGSEND = YES;
442 | GCC_C_LANGUAGE_STANDARD = gnu99;
443 | GCC_NO_COMMON_BLOCKS = YES;
444 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
445 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
446 | GCC_WARN_UNDECLARED_SELECTOR = YES;
447 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
448 | GCC_WARN_UNUSED_FUNCTION = YES;
449 | GCC_WARN_UNUSED_VARIABLE = YES;
450 | IPHONEOS_DEPLOYMENT_TARGET = 10.1;
451 | MTL_ENABLE_DEBUG_INFO = NO;
452 | SDKROOT = iphoneos;
453 | TARGETED_DEVICE_FAMILY = "1,2";
454 | VALIDATE_PRODUCT = YES;
455 | };
456 | name = Release;
457 | };
458 | DAA0A6B71E81363700568859 /* Debug */ = {
459 | isa = XCBuildConfiguration;
460 | buildSettings = {
461 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
462 | DEVELOPMENT_TEAM = WE2ULLEE7K;
463 | INFOPLIST_FILE = SLAlertViewDemo/Info.plist;
464 | IPHONEOS_DEPLOYMENT_TARGET = 9.0;
465 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
466 | PRODUCT_BUNDLE_IDENTIFIER = cn.pandai.SLAlertViewDemo;
467 | PRODUCT_NAME = "$(TARGET_NAME)";
468 | };
469 | name = Debug;
470 | };
471 | DAA0A6B81E81363700568859 /* Release */ = {
472 | isa = XCBuildConfiguration;
473 | buildSettings = {
474 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
475 | DEVELOPMENT_TEAM = WE2ULLEE7K;
476 | INFOPLIST_FILE = SLAlertViewDemo/Info.plist;
477 | IPHONEOS_DEPLOYMENT_TARGET = 9.0;
478 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
479 | PRODUCT_BUNDLE_IDENTIFIER = cn.pandai.SLAlertViewDemo;
480 | PRODUCT_NAME = "$(TARGET_NAME)";
481 | };
482 | name = Release;
483 | };
484 | DAA0A6BA1E81363700568859 /* Debug */ = {
485 | isa = XCBuildConfiguration;
486 | buildSettings = {
487 | BUNDLE_LOADER = "$(TEST_HOST)";
488 | DEVELOPMENT_TEAM = WE2ULLEE7K;
489 | INFOPLIST_FILE = SLAlertViewDemoTests/Info.plist;
490 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
491 | PRODUCT_BUNDLE_IDENTIFIER = cn.pandai.SLAlertViewDemoTests;
492 | PRODUCT_NAME = "$(TARGET_NAME)";
493 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SLAlertViewDemo.app/SLAlertViewDemo";
494 | };
495 | name = Debug;
496 | };
497 | DAA0A6BB1E81363700568859 /* Release */ = {
498 | isa = XCBuildConfiguration;
499 | buildSettings = {
500 | BUNDLE_LOADER = "$(TEST_HOST)";
501 | DEVELOPMENT_TEAM = WE2ULLEE7K;
502 | INFOPLIST_FILE = SLAlertViewDemoTests/Info.plist;
503 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
504 | PRODUCT_BUNDLE_IDENTIFIER = cn.pandai.SLAlertViewDemoTests;
505 | PRODUCT_NAME = "$(TARGET_NAME)";
506 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SLAlertViewDemo.app/SLAlertViewDemo";
507 | };
508 | name = Release;
509 | };
510 | DAA0A6BD1E81363700568859 /* Debug */ = {
511 | isa = XCBuildConfiguration;
512 | buildSettings = {
513 | DEVELOPMENT_TEAM = WE2ULLEE7K;
514 | INFOPLIST_FILE = SLAlertViewDemoUITests/Info.plist;
515 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
516 | PRODUCT_BUNDLE_IDENTIFIER = cn.pandai.SLAlertViewDemoUITests;
517 | PRODUCT_NAME = "$(TARGET_NAME)";
518 | TEST_TARGET_NAME = SLAlertViewDemo;
519 | };
520 | name = Debug;
521 | };
522 | DAA0A6BE1E81363700568859 /* Release */ = {
523 | isa = XCBuildConfiguration;
524 | buildSettings = {
525 | DEVELOPMENT_TEAM = WE2ULLEE7K;
526 | INFOPLIST_FILE = SLAlertViewDemoUITests/Info.plist;
527 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
528 | PRODUCT_BUNDLE_IDENTIFIER = cn.pandai.SLAlertViewDemoUITests;
529 | PRODUCT_NAME = "$(TARGET_NAME)";
530 | TEST_TARGET_NAME = SLAlertViewDemo;
531 | };
532 | name = Release;
533 | };
534 | /* End XCBuildConfiguration section */
535 |
536 | /* Begin XCConfigurationList section */
537 | DAA0A6841E81363600568859 /* Build configuration list for PBXProject "SLAlertViewDemo" */ = {
538 | isa = XCConfigurationList;
539 | buildConfigurations = (
540 | DAA0A6B41E81363700568859 /* Debug */,
541 | DAA0A6B51E81363700568859 /* Release */,
542 | );
543 | defaultConfigurationIsVisible = 0;
544 | defaultConfigurationName = Release;
545 | };
546 | DAA0A6B61E81363700568859 /* Build configuration list for PBXNativeTarget "SLAlertViewDemo" */ = {
547 | isa = XCConfigurationList;
548 | buildConfigurations = (
549 | DAA0A6B71E81363700568859 /* Debug */,
550 | DAA0A6B81E81363700568859 /* Release */,
551 | );
552 | defaultConfigurationIsVisible = 0;
553 | defaultConfigurationName = Release;
554 | };
555 | DAA0A6B91E81363700568859 /* Build configuration list for PBXNativeTarget "SLAlertViewDemoTests" */ = {
556 | isa = XCConfigurationList;
557 | buildConfigurations = (
558 | DAA0A6BA1E81363700568859 /* Debug */,
559 | DAA0A6BB1E81363700568859 /* Release */,
560 | );
561 | defaultConfigurationIsVisible = 0;
562 | defaultConfigurationName = Release;
563 | };
564 | DAA0A6BC1E81363700568859 /* Build configuration list for PBXNativeTarget "SLAlertViewDemoUITests" */ = {
565 | isa = XCConfigurationList;
566 | buildConfigurations = (
567 | DAA0A6BD1E81363700568859 /* Debug */,
568 | DAA0A6BE1E81363700568859 /* Release */,
569 | );
570 | defaultConfigurationIsVisible = 0;
571 | defaultConfigurationName = Release;
572 | };
573 | /* End XCConfigurationList section */
574 | };
575 | rootObject = DAA0A6811E81363600568859 /* Project object */;
576 | }
577 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/SLAlertView/SLActionSheet.m:
--------------------------------------------------------------------------------
1 | //
2 | // SLActionSheet.m
3 | // SLAlertView
4 | //
5 | // Created by Mac-pro on 17/3/21.
6 | // Copyright © 2017年 SongLazy All rights reserved.
7 | //
8 |
9 | #import "SLActionSheet.h"
10 | #import "UIView+SLAutolayout.h"
11 |
12 | #define screenW [UIScreen mainScreen].bounds.size.width
13 | #define screenH [UIScreen mainScreen].bounds.size.height
14 | #define shadowW self.frame.size.width
15 | #define shadowH self.frame.size.height
16 | #define animationTime 0.4
17 |
18 | #define margin_partTopart 10
19 | #define margin_partTobottom 10
20 | #define margin_leading_trailling 10
21 | #define margin_edge 20
22 |
23 |
24 |
25 | #define margin_viewToview 1
26 | #define buttonHeight 50.0f
27 | #define titleViewHeight 40
28 | #define margin_btnTobtn 1.0f
29 | #define separatorHeight 1
30 |
31 | #define DefaultCornerRadius 15
32 |
33 | #define DefaultTranslucenceColor [UIColor colorWithRed:255 / 255.0f green:255 / 255.0 blue:255 / 255.0 alpha:0.9]
34 | #define DefaultSeparatorColor [UIColor clearColor]
35 | #define DefaultBackgroundColor DefaultTranslucenceColor
36 | #define DefaultTextColor [UIColor lightGrayColor]
37 | #define DefaultOtherBtnTextColor [UIColor darkGrayColor]
38 | #define DefaultCancelBtnBtnTextColor [UIColor darkGrayColor]
39 |
40 | @interface SLActionSheet()
41 |
42 | @property (nonatomic, strong) UIView *shadowView;
43 | @property (nonatomic, strong) UIView *sheet;
44 |
45 | @property (nonatomic, copy) void(^clickHandler)(SLActionSheet * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle);
46 |
47 | @end
48 |
49 | @implementation SLActionSheet
50 |
51 | + (void)actionSheetWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles settingHandler:(void (^)(SLActionSheet * _Nonnull))settingHandler clickHandler:(nullable void (^)(SLActionSheet * _Nonnull, NSInteger, NSString * _Nullable))clickHandler{
52 | UIView *currentView = [SLActionSheet getCurrentVc].view;
53 | SLActionSheet *sheet = [[SLActionSheet alloc] initWithFrame:currentView.frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles settingHandler:settingHandler clickHandler:clickHandler];
54 | [currentView addSubview:sheet];
55 |
56 | }
57 |
58 | + (void)actionSheetWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles clickHandler:(nullable void (^)(SLActionSheet * _Nonnull, NSInteger, NSString * _Nullable))clickHandler{
59 |
60 | UIView *currentView = [SLActionSheet getCurrentVc].view;
61 | SLActionSheet *sheet = [[SLActionSheet alloc] initWithFrame:currentView.frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles settingHandler:nil clickHandler:clickHandler];
62 | [currentView addSubview:sheet];
63 | }
64 |
65 | - (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles settingHandler:(void (^)(SLActionSheet * _Nonnull))settingHandler clickHandler:(nullable void (^)(SLActionSheet * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler{
66 |
67 | self = [self initWithFrame:frame];
68 | if (self) {
69 |
70 | if (settingHandler) {
71 | settingHandler(self);
72 | }
73 |
74 | if (clickHandler) {
75 | _clickHandler = clickHandler;
76 | }
77 |
78 | _delegate = delegate;
79 |
80 | UIView *shadowView = [[UIView alloc] initWithFrame:frame];
81 | shadowView.backgroundColor = [UIColor colorWithRed:10 / 255.0 green:10 / 255.0 blue:10 / 255.0 alpha:0.0];
82 | [self addSubview:shadowView];
83 | _shadowView = shadowView;
84 |
85 | [[UIApplication sharedApplication].keyWindow addSubview:self];
86 | UITapGestureRecognizer *backtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenSheetView)];
87 | [shadowView addGestureRecognizer:backtap];
88 |
89 | CGFloat x = 0;
90 | CGFloat y = shadowH;
91 | CGFloat w = shadowW;
92 | CGFloat h = 300;
93 |
94 | UIView *sheet = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
95 | sheet.backgroundColor = [UIColor clearColor];
96 | self.sheet = sheet;
97 | [self addSubview:sheet];
98 |
99 | [self prepareSheetWithFrame:frame title:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles];
100 |
101 | }
102 | return self;
103 | }
104 |
105 | - (void)prepareSheetWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles {
106 | // 下半部分
107 | UIView *underPart = [[UIView alloc] init];
108 | underPart.backgroundColor = [UIColor whiteColor];
109 | underPart.layer.cornerRadius = DefaultCornerRadius;
110 | underPart.layer.masksToBounds = YES;
111 |
112 | if (cancelButtonTitle) {
113 |
114 | [_sheet addSubview:underPart];
115 | [underPart addConstraint:NSLayoutAttributeLeft equalTo:_sheet offset:margin_leading_trailling];
116 | [underPart addConstraint:NSLayoutAttributeRight equalTo:_sheet offset:-margin_leading_trailling];
117 | [underPart addConstraint:NSLayoutAttributeBottom equalTo:_sheet offset:-margin_partTobottom];
118 | [underPart addConstraint:NSLayoutAttributeHeight equalTo:nil offset:buttonHeight];
119 | // cancel button
120 | UIButton *cancelBtn = [[UIButton alloc] init];
121 | cancelBtn.tag = 0;
122 | if (_cancelBackgroundColor) {
123 | [cancelBtn setBackgroundColor:_cancelBackgroundColor];
124 | } else {
125 | [cancelBtn setBackgroundColor:[UIColor whiteColor]];
126 | }
127 | [cancelBtn setTitle:cancelButtonTitle forState:UIControlStateNormal];
128 | if (_cancelTitleColor) {
129 | [cancelBtn setTitleColor:_cancelTitleColor forState:UIControlStateNormal];
130 | } else {
131 | [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
132 | }
133 |
134 | if (_cancelTitleFont) {
135 | cancelBtn.titleLabel.font = _cancelTitleFont;
136 | } else {
137 | cancelBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
138 | }
139 |
140 | [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
141 | [underPart addSubview:cancelBtn];
142 | [cancelBtn addConstraint:NSLayoutAttributeTop equalTo:underPart offset:0];
143 | [cancelBtn addConstraint:NSLayoutAttributeLeft equalTo:underPart offset:0];
144 | [cancelBtn addConstraint:NSLayoutAttributeRight equalTo:underPart offset:0];
145 | [cancelBtn addConstraint:NSLayoutAttributeBottom equalTo:underPart offset:0];
146 |
147 | [cancelBtn layoutIfNeeded];
148 | [underPart layoutIfNeeded];
149 | }
150 |
151 |
152 |
153 | // 上半部分
154 | UIView *upperPart = [[UIView alloc] init];
155 | upperPart.backgroundColor = [UIColor clearColor];
156 | upperPart.layer.cornerRadius = DefaultCornerRadius;
157 | upperPart.layer.masksToBounds = YES;
158 | [_sheet addSubview:upperPart];
159 |
160 | if (cancelButtonTitle) {
161 | [upperPart addConstraint:NSLayoutAttributeBottom equalTo:underPart toAttribute:NSLayoutAttributeTop offset:-margin_partTopart];
162 | } else {
163 | [upperPart addConstraint:NSLayoutAttributeBottom equalTo:_sheet toAttribute:NSLayoutAttributeBottom offset:-margin_partTopart];
164 | }
165 |
166 | [upperPart addConstraint:NSLayoutAttributeLeft equalTo:_sheet offset:margin_leading_trailling];
167 | [upperPart addConstraint:NSLayoutAttributeRight equalTo:_sheet offset:-margin_leading_trailling];
168 | [upperPart addConstraint:NSLayoutAttributeTop equalTo:_sheet offset:0];
169 |
170 | UIView *titleView = [[UIView alloc] init];
171 | UILabel *titleLabel = [[UILabel alloc] init];
172 | if (title) {
173 | // title view
174 |
175 | [upperPart addSubview:titleView];
176 |
177 | [titleView addConstraint:NSLayoutAttributeLeft equalTo:upperPart offset:0];
178 | [titleView addConstraint:NSLayoutAttributeRight equalTo:upperPart offset:0];
179 | [titleView addConstraint:NSLayoutAttributeTop equalTo:upperPart offset:0];
180 |
181 | // title label
182 |
183 | titleLabel.textAlignment = NSTextAlignmentCenter;
184 | titleLabel.text = title;
185 | titleLabel.backgroundColor = [UIColor clearColor];
186 | if (_titleFont) {
187 | titleLabel.font = _titleFont;
188 | } else {
189 | titleLabel.font = [UIFont boldSystemFontOfSize:14];
190 | }
191 | [titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
192 | titleLabel.numberOfLines = 0;
193 | [titleView addSubview:titleLabel];
194 |
195 | [titleLabel addConstraint:NSLayoutAttributeLeft equalTo:titleView offset:margin_edge];
196 | [titleLabel addConstraint:NSLayoutAttributeRight equalTo:titleView offset:-margin_edge];
197 | [titleLabel addConstraint:NSLayoutAttributeTop equalTo:titleView offset:margin_edge];
198 | [titleLabel addConstraint:NSLayoutAttributeBottom equalTo:titleView offset:-margin_edge];
199 |
200 | UIView *line_title_msg =[[UIView alloc] init];
201 | if (_separatorColor) {
202 | line_title_msg.backgroundColor = _separatorColor;
203 | } else {
204 | line_title_msg.backgroundColor = DefaultSeparatorColor;
205 | }
206 | [_sheet addSubview:line_title_msg];
207 | [line_title_msg addConstraint:NSLayoutAttributeHeight equalTo:nil offset:1];
208 | [line_title_msg addConstraint:NSLayoutAttributeTop equalTo:titleView toAttribute:NSLayoutAttributeBottom offset:0];
209 | [line_title_msg addConstraint:NSLayoutAttributeLeft equalTo:titleView offset:0];
210 | [line_title_msg addConstraint:NSLayoutAttributeRight equalTo:titleView offset:0];
211 |
212 |
213 | }
214 |
215 |
216 |
217 | //---
218 | // message view
219 | UIView *messageView = [[UIView alloc] init];
220 | UILabel *messageLabel = [[UILabel alloc] init];
221 | if (message) {
222 | [upperPart addSubview:messageView];
223 |
224 | [messageView addConstraint:NSLayoutAttributeLeft equalTo:upperPart offset:0];
225 | [messageView addConstraint:NSLayoutAttributeRight equalTo:upperPart offset:0];
226 | if (title) {
227 | [messageView addConstraint:NSLayoutAttributeTop equalTo:titleView toAttribute:NSLayoutAttributeBottom offset:separatorHeight];
228 | } else {
229 | [messageView addConstraint:NSLayoutAttributeTop equalTo:upperPart toAttribute:NSLayoutAttributeTop offset:0];
230 | }
231 |
232 |
233 | // message label
234 |
235 | messageLabel.textAlignment = NSTextAlignmentCenter;
236 | messageLabel.text = message;
237 | messageLabel.backgroundColor = [UIColor clearColor];
238 | [messageLabel setLineBreakMode:NSLineBreakByWordWrapping];
239 | messageLabel.numberOfLines = 0;
240 | if (_messageFont) {
241 | messageLabel.font = _messageFont;
242 | } else {
243 | messageLabel.font = [UIFont systemFontOfSize:13];
244 | }
245 |
246 | [messageView addSubview:messageLabel];
247 |
248 | [messageLabel addConstraint:NSLayoutAttributeLeft equalTo:messageView offset:margin_edge];
249 | [messageLabel addConstraint:NSLayoutAttributeRight equalTo:messageView offset:-margin_edge];
250 | [messageLabel addConstraint:NSLayoutAttributeTop equalTo:messageView offset:margin_edge];
251 | [messageLabel addConstraint:NSLayoutAttributeBottom equalTo:messageView offset:-margin_edge];
252 |
253 | }
254 |
255 |
256 |
257 |
258 |
259 |
260 | [titleLabel layoutIfNeeded];
261 | [titleView layoutIfNeeded];
262 | [messageLabel layoutIfNeeded];
263 |
264 | CGRect messageViewFrame = messageView.frame;
265 | messageViewFrame.size.height = messageLabel.frame.size.height + 2 * margin_edge;
266 | messageView.frame = messageViewFrame;
267 | [messageView layoutIfNeeded];
268 | [upperPart layoutIfNeeded];
269 |
270 | [_sheet layoutIfNeeded];
271 |
272 | // other button
273 |
274 | __block CGFloat otherButtonsHeight = 0;
275 |
276 | NSMutableArray *otherBtns = [NSMutableArray array];
277 | if (otherButtonTitles.count) {
278 |
279 | [otherButtonTitles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
280 | UIButton *otherBtn = [[UIButton alloc] init];
281 | if (_otherTitleFont) {
282 | otherBtn.titleLabel.font = _otherTitleFont;
283 | } else {
284 | otherBtn.titleLabel.font = [UIFont systemFontOfSize:16];
285 | }
286 |
287 | [otherBtn setBackgroundColor:[UIColor whiteColor]];
288 |
289 | [otherBtns addObject:otherBtn];
290 | if (_otherTitleColor) {
291 | [otherBtn setTitleColor:_otherTitleColor forState:UIControlStateNormal];
292 | } else {
293 | [otherBtn setTitleColor:DefaultOtherBtnTextColor forState:UIControlStateNormal];
294 | }
295 | if (_otherBackgroundColor) {
296 | [otherBtn setBackgroundColor:_otherBackgroundColor];
297 | } else {
298 | [otherBtn setBackgroundColor:DefaultBackgroundColor];
299 | }
300 | [otherBtn setTitle:obj forState:UIControlStateNormal];
301 | [otherBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
302 |
303 | [_sheet addSubview:otherBtn];
304 |
305 | [otherBtn addConstraint:NSLayoutAttributeHeight equalTo:nil offset:buttonHeight];
306 |
307 | if (title) {
308 | if (message) { // 有title 有message
309 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:messageView offset:0];
310 | [otherBtn addConstraint:NSLayoutAttributeRight equalTo:messageView offset:0];
311 | } else { // 有title 没有message
312 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:titleView offset:0];
313 | [otherBtn addConstraint:NSLayoutAttributeRight equalTo:titleView offset:0];
314 | }
315 | } else {
316 | if (message) { // 没有title 有message
317 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:messageView offset:0];
318 | [otherBtn addConstraint:NSLayoutAttributeRight equalTo:messageView offset:0];
319 | } else { // 没有title 没有message
320 | [otherBtn addConstraint:NSLayoutAttributeLeft equalTo:upperPart offset:0];
321 | [otherBtn addConstraint:NSLayoutAttributeRight equalTo:upperPart offset:0];
322 | }
323 | }
324 |
325 |
326 | UIView *line_otherBtn =[[UIView alloc] init];
327 | if (_separatorColor) {
328 | line_otherBtn.backgroundColor = _separatorColor;
329 | } else {
330 | line_otherBtn.backgroundColor = DefaultSeparatorColor;
331 | }
332 |
333 | if (message) {
334 | [_sheet addSubview:line_otherBtn];
335 | [line_otherBtn addConstraint:NSLayoutAttributeLeft equalTo:messageView offset:0];
336 | [line_otherBtn addConstraint:NSLayoutAttributeRight equalTo:messageView offset:0];
337 | [line_otherBtn addConstraint:NSLayoutAttributeHeight equalTo:nil offset:1];
338 | }
339 |
340 |
341 |
342 | if (idx == 0) {
343 | if (message) {
344 | [otherBtn addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeBottom offset:margin_btnTobtn];
345 | [line_otherBtn addConstraint:NSLayoutAttributeTop equalTo:messageView toAttribute:NSLayoutAttributeBottom offset:0];
346 | } else {
347 | if (title) {
348 | [otherBtn addConstraint:NSLayoutAttributeTop equalTo:titleView toAttribute:NSLayoutAttributeBottom offset:margin_btnTobtn];
349 | [line_otherBtn addConstraint:NSLayoutAttributeTop equalTo:titleView toAttribute:NSLayoutAttributeBottom offset:0];
350 | } else {
351 | [otherBtn addConstraint:NSLayoutAttributeBottom equalTo:upperPart toAttribute:NSLayoutAttributeBottom offset:0];
352 | }
353 | }
354 |
355 | } else {
356 | if (!title && !message) {
357 | [otherBtn addConstraint:NSLayoutAttributeBottom equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeTop offset:-margin_btnTobtn];
358 | // separator
359 | [line_otherBtn addConstraint:NSLayoutAttributeBottom equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeTop offset:0];
360 | } else {
361 | [otherBtn addConstraint:NSLayoutAttributeTop equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeBottom offset:margin_btnTobtn];
362 | // separator
363 | [line_otherBtn addConstraint:NSLayoutAttributeTop equalTo:otherBtns[idx - 1] toAttribute:NSLayoutAttributeBottom offset:0];
364 | }
365 |
366 |
367 | }
368 |
369 | [otherBtn layoutIfNeeded];
370 |
371 | if (!title && !message) {
372 | if (idx == 0) {
373 |
374 | UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:otherBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(DefaultCornerRadius, DefaultCornerRadius)];
375 |
376 | CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
377 |
378 | maskLayer.frame = otherBtn.bounds;
379 |
380 | maskLayer.path = maskPath.CGPath;
381 |
382 | otherBtn.layer.mask = maskLayer;
383 |
384 | }
385 | if (idx == otherButtonTitles.count - 1) {
386 | UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:otherBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(DefaultCornerRadius, DefaultCornerRadius)];
387 |
388 | CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
389 |
390 | maskLayer.frame = otherBtn.bounds;
391 |
392 | maskLayer.path = maskPath.CGPath;
393 |
394 | otherBtn.layer.mask = maskLayer;
395 | }
396 | } else {
397 | if (idx == otherButtonTitles.count - 1) {
398 |
399 | UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:otherBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(DefaultCornerRadius, DefaultCornerRadius)];
400 |
401 | CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
402 |
403 | maskLayer.frame = otherBtn.bounds;
404 |
405 | maskLayer.path = maskPath.CGPath;
406 |
407 | otherBtn.layer.mask = maskLayer;
408 |
409 | }
410 |
411 | }
412 |
413 | if (cancelButtonTitle) {
414 | otherBtn.tag = idx + 1;
415 | } else {
416 | otherBtn.tag = idx;
417 | }
418 |
419 | CGFloat hei = buttonHeight + margin_btnTobtn;
420 | otherButtonsHeight += hei;
421 |
422 | }];
423 | }
424 |
425 | [upperPart layoutIfNeeded];
426 |
427 | [_sheet layoutIfNeeded];
428 |
429 | [self setupColorsForTitleView:titleView titleLabel:titleLabel messageView:messageView messageLabel:messageLabel];
430 |
431 | [UIView animateWithDuration:0.25 animations:^{
432 |
433 | CGRect sheetFrame = _sheet.frame;
434 |
435 | if (cancelButtonTitle) {
436 | if (message) {
437 | sheetFrame.size.height = titleView.frame.size.height + messageView.frame.size.height + margin_edge + underPart.frame.size.height + otherButtonsHeight;
438 | } else {
439 |
440 | sheetFrame.size.height = titleView.frame.size.height + messageView.frame.size.height + underPart.frame.size.height + otherButtonsHeight - margin_edge;
441 | }
442 |
443 | } else {
444 | sheetFrame.size.height = titleView.frame.size.height + messageView.frame.size.height + margin_partTobottom + otherButtonsHeight;
445 | if (message) {
446 | sheetFrame.size.height = titleView.frame.size.height + messageView.frame.size.height + margin_partTobottom + otherButtonsHeight;
447 | } else {
448 | sheetFrame.size.height = titleView.frame.size.height + messageView.frame.size.height + 0 + otherButtonsHeight;
449 | if (title) {
450 | sheetFrame.size.height = titleView.frame.size.height + messageView.frame.size.height + otherButtonsHeight - margin_edge - margin_partTobottom;
451 | }
452 | }
453 | }
454 |
455 | sheetFrame.origin.y = screenH - sheetFrame.size.height;
456 |
457 | self.sheet.frame = sheetFrame;
458 |
459 | self.shadowView.backgroundColor = [UIColor colorWithRed:10/255.0 green:10/255.0 blue:10/255.0 alpha:0.4];
460 |
461 | } completion:^(BOOL finished) {
462 |
463 | }];
464 | }
465 |
466 | - (void)hidenSheetView {
467 |
468 | CGRect frame = self.sheet.frame;
469 |
470 | frame.origin.y = shadowH;
471 | // frame.size.height = 0;
472 |
473 | [UIView animateWithDuration:animationTime delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:0 animations:^{
474 | self.sheet.frame = frame;
475 | _shadowView.alpha = 0.0f;
476 |
477 | } completion:^(BOOL finished) {
478 |
479 | [self removeFromSuperview];
480 | }];
481 |
482 | }
483 |
484 | - (void)buttonClick:(UIButton *)sender {
485 | [self hidenSheetView];
486 | if ([self.delegate respondsToSelector:@selector(actionSheet:didSelectedButtonWithButtonIndex:buttonTitle:)]) {
487 | [self.delegate actionSheet:self didSelectedButtonWithButtonIndex:sender.tag buttonTitle:sender.titleLabel.text];
488 | }
489 | if (_clickHandler) {
490 | _clickHandler(self,sender.tag,sender.titleLabel.text);
491 | }
492 | }
493 |
494 | - (void)setupColorsForTitleView:(UIView *)titleView titleLabel:(UILabel *)titleLabel messageView:(UIView *)messageView messageLabel:(UILabel *)messageLabel {
495 | if (_titleBackgroundColor) {
496 | titleView.backgroundColor = _titleBackgroundColor;
497 | } else {
498 | titleView.backgroundColor = DefaultBackgroundColor;
499 | }
500 | if (_titleColor) {
501 | titleLabel.textColor = _titleColor;
502 | } else {
503 | titleLabel.textColor = DefaultTextColor;
504 | }
505 | if (_messageBackgroundColor) {
506 | messageView.backgroundColor = _messageBackgroundColor;
507 | } else {
508 | messageView.backgroundColor = DefaultBackgroundColor;
509 | }
510 | if (_messageColor) {
511 | messageLabel.textColor = _messageColor;
512 | } else {
513 | messageLabel.textColor = DefaultTextColor;
514 | }
515 |
516 |
517 | }
518 |
519 |
520 | // 获取当前控制器的view
521 | - (UIView *)getCurrentView {
522 | return [self getCurrentVC].view;
523 | }
524 |
525 | //获取当前屏幕显示的viewcontroller
526 | - (UIViewController *)getCurrentVC
527 | {
528 | UIViewController *result = nil;
529 |
530 | UIWindow * window = [[UIApplication sharedApplication] keyWindow];
531 | if (window.windowLevel != UIWindowLevelNormal)
532 | {
533 | NSArray *windows = [[UIApplication sharedApplication] windows];
534 | for(UIWindow * tmpWin in windows)
535 | {
536 | if (tmpWin.windowLevel == UIWindowLevelNormal)
537 | {
538 | window = tmpWin;
539 | break;
540 | }
541 | }
542 | }
543 |
544 | UIView *frontView = [[window subviews] objectAtIndex:0];
545 | id nextResponder = [frontView nextResponder];
546 |
547 | if ([nextResponder isKindOfClass:[UIViewController class]])
548 | result = nextResponder;
549 | else
550 | result = window.rootViewController;
551 |
552 | return result;
553 | }
554 |
555 | + (UIViewController *)getCurrentVc
556 | {
557 | UIViewController *result = nil;
558 |
559 | UIWindow * window = [[UIApplication sharedApplication] keyWindow];
560 | if (window.windowLevel != UIWindowLevelNormal)
561 | {
562 | NSArray *windows = [[UIApplication sharedApplication] windows];
563 | for(UIWindow * tmpWin in windows)
564 | {
565 | if (tmpWin.windowLevel == UIWindowLevelNormal)
566 | {
567 | window = tmpWin;
568 | break;
569 | }
570 | }
571 | }
572 |
573 | UIView *frontView = [[window subviews] objectAtIndex:0];
574 | id nextResponder = [frontView nextResponder];
575 |
576 | if ([nextResponder isKindOfClass:[UIViewController class]])
577 | result = nextResponder;
578 | else
579 | result = window.rootViewController;
580 |
581 | return result;
582 | }
583 |
584 | @end
585 |
--------------------------------------------------------------------------------
/SLAlertViewDemo/SLAlertViewDemo/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
34 |
42 |
50 |
58 |
65 |
73 |
81 |
89 |
97 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
119 |
127 |
135 |
143 |
151 |
159 |
166 |
174 |
182 |
190 |
198 |
206 |
214 |
222 |
230 |
238 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
--------------------------------------------------------------------------------