diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..761abcc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +# reference: http://www.objc.io/issue-6/travis-ci.html + +language: objective-c + +script: +- xcodebuild -project SCLAlertView.xcodeproj -target SCLAlertView -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO \ No newline at end of file diff --git a/README.md b/README.md index a3785bd..166a57a 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,11 @@ SCLAlertView-Objective-C Animated Alert View written in Swift but ported to Objective-C, which can be used as a `UIAlertView` or `UIAlertController` replacement. +[![Build Status](https://travis-ci.org/dogo/SCLAlertView.svg?branch=master)](https://travis-ci.org/dogo/SCLAlertView) + ![BackgroundImage](https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot.png)_ ![BackgroundImage](https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png) +![BackgroundImage](ScreenShots/ScreenShot3.png) ###Easy to use ```Objective-C @@ -19,6 +22,7 @@ SCLAlertView *alert = [[SCLAlertView alloc] init]; [alert showWarning:self title:@"Hello Warning" subTitle:@"This is a more descriptive warning text." closeButtonTitle:@"Done" duration:0.0f]; // Warning [alert showInfo:self title:@"Hello Info" subTitle:@"This is a more descriptive info text." closeButtonTitle:@"Done" duration:0.0f]; // Info [alert showEdit:self title:@"Hello Edit" subTitle:@"This is a more descriptive info text with a edit textbox" closeButtonTitle:@"Done" duration:0.0f]; // Edit +[alert showCustom:self image:[UIImage imageNamed:@"git"] color:color title:@"Custom" subTitle:@"Add a custom icon and color for your own type of alert!" closeButtonTitle:@"OK" duration:0.0f]; // Custom // Advanced SCLAlertView *alert = [[SCLAlertView alloc] init]; @@ -87,7 +91,8 @@ typedef NS_ENUM(NSInteger, SCLAlertViewStyle) Notice, Warning, Info, - Edit + Edit, + Custom }; ``` diff --git a/SCLAlertView-Objective-C.podspec b/SCLAlertView-Objective-C.podspec index e5c0cfb..6abfb11 100644 --- a/SCLAlertView-Objective-C.podspec +++ b/SCLAlertView-Objective-C.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "SCLAlertView-Objective-C" - spec.version = "0.1.2" + spec.version = "0.2.0" spec.summary = "Beautiful animated Alert View. Written in Swift but ported to Objective-C" spec.homepage = "https://github.com/dogo/SCLAlertView" spec.screenshots = "https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot.png", "https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png" @@ -10,7 +10,7 @@ Pod::Spec.new do |spec| spec.social_media_url = "http://twitter.com/di_autilio" spec.platform = :ios spec.ios.deployment_target = '7.0' - spec.source = { :git => "https://github.com/dogo/SCLAlertView.git", :tag => "0.1.2" } + spec.source = { :git => "https://github.com/dogo/SCLAlertView.git", :tag => "0.2.0" } spec.source_files = "SCLAlertView/*" spec.requires_arc = true end diff --git a/SCLAlertView.xcodeproj/project.pbxproj b/SCLAlertView.xcodeproj/project.pbxproj index ab4b7ed..6fc6e64 100644 --- a/SCLAlertView.xcodeproj/project.pbxproj +++ b/SCLAlertView.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 274E5C1C19F1ECFE009D8B4A /* git.png in Resources */ = {isa = PBXBuildFile; fileRef = 274E5C1B19F1ECFE009D8B4A /* git.png */; }; DD4BA9BE19DED822008D73EB /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD4BA9BD19DED822008D73EB /* AVFoundation.framework */; }; DD4BA9C119DED8EF008D73EB /* right_answer.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = DD4BA9C019DED8EF008D73EB /* right_answer.mp3 */; }; DD7282B919D6087C00077F54 /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DD7282B719D6087C00077F54 /* Storyboard.storyboard */; }; @@ -32,6 +33,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 274E5C1B19F1ECFE009D8B4A /* git.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = git.png; sourceTree = ""; }; DD0D295B19D902DA00881F53 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = SCLAlertViewExample/Info.plist; sourceTree = SOURCE_ROOT; }; DD4BA9BD19DED822008D73EB /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; DD4BA9C019DED8EF008D73EB /* right_answer.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = right_answer.mp3; sourceTree = ""; }; @@ -126,6 +128,7 @@ DDB15FD319D5B8DB00173158 /* main.m */, DD4BA9C219DEDD8E008D73EB /* SCLAlertView-Objective-C-prefix.pch */, DD0D295B19D902DA00881F53 /* Info.plist */, + 274E5C1B19F1ECFE009D8B4A /* git.png */, DD4BA9BF19DED8EF008D73EB /* Sounds */, ); name = "Supporting Files"; @@ -245,6 +248,7 @@ files = ( DD4BA9C119DED8EF008D73EB /* right_answer.mp3 in Resources */, DDB15FD019D5B8BF00173158 /* Images.xcassets in Resources */, + 274E5C1C19F1ECFE009D8B4A /* git.png in Resources */, DD7282B919D6087C00077F54 /* Storyboard.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SCLAlertView/SCLAlertView.h b/SCLAlertView/SCLAlertView.h index 1925e16..7e13a9c 100755 --- a/SCLAlertView/SCLAlertView.h +++ b/SCLAlertView/SCLAlertView.h @@ -15,7 +15,7 @@ typedef NSAttributedString* (^SCLAttributedFormatBlock)(NSString *value); /** Alert Styles * - * TODO + * Set SCLAlertView Style */ typedef NS_ENUM(NSInteger, SCLAlertViewStyle) { @@ -24,7 +24,18 @@ typedef NS_ENUM(NSInteger, SCLAlertViewStyle) Notice, Warning, Info, - Edit + Edit, + Custom +}; + +/** Alert hide animation styles + * + * Set SCLAlertView hide animation type. + */ +typedef NS_ENUM(NSInteger, SCLAlertViewAnimation) +{ + NoAnimation, + FadeOut }; /** Title Label @@ -42,7 +53,7 @@ typedef NS_ENUM(NSInteger, SCLAlertViewStyle) /** Dismiss on tap outside * * A boolean value that determines whether to dismiss when tapping outside the SCLAlertView. - * (Default = NO) + * (Default: NO) */ @property (nonatomic, assign) BOOL shouldDismissOnTapOutside; @@ -58,9 +69,16 @@ typedef NS_ENUM(NSInteger, SCLAlertViewStyle) */ @property (nonatomic, copy) SCLAttributedFormatBlock attributedFormatBlock; +/** Hide animation type + * + * Holds the hide animation type. + * (Default: FadeOut) + */ +@property (nonatomic) SCLAlertViewAnimation hideAnimationType; + /** Hide SCLAlertView * - * TODO + * Hide SCLAlertView using animation and removing from super view. */ - (void)hideView; @@ -124,5 +142,17 @@ typedef NS_ENUM(NSInteger, SCLAlertViewStyle) */ - (void)showTitle:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle style:(SCLAlertViewStyle)style closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration; +/** Shows a custom SCLAlertView without using a predefined type, allowing for a custom image and color to be specified. + * + * @param vc The view controller the alert view will be displayed in. + * @param image A UIImage object to be used as the icon for the alert view. + * @param color A UIColor object to be used to tint the background of the icon circle and the buttons. + * @param title The title text of the alert view. + * @param subTitle The subtitle text of the alert view. + * @param closeButtonTitle The text for the close button. + * @param duration The amount of time the alert will remain on screen until it is automatically dismissed. If automatic dismissal is not desired, set to 0. + */ +- (void)showCustom:(UIViewController *)vc image:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration; + @end diff --git a/SCLAlertView/SCLAlertView.m b/SCLAlertView/SCLAlertView.m index 651d23b..01b5c40 100755 --- a/SCLAlertView/SCLAlertView.m +++ b/SCLAlertView/SCLAlertView.m @@ -17,7 +17,10 @@ green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \ blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] -@interface SCLAlertView () +#define KEYBOARD_HEIGHT 80 +#define PREDICTION_BAR_HEIGHT 40 + +@interface SCLAlertView () @property (nonatomic, strong) NSMutableArray *inputs; @property (nonatomic, strong) NSMutableArray *buttons; @@ -76,6 +79,7 @@ - (id)init kWindowHeight = 178.0f; kTextHeight = 90.0f; _shouldDismissOnTapOutside = NO; + _hideAnimationType = FadeOut; // Init _labelTitle = [[UILabel alloc] init]; @@ -126,10 +130,19 @@ - (id)init _labelTitle.textColor = UIColorFromRGB(0x4D4D4D); _viewText.textColor = UIColorFromRGB(0x4D4D4D); _contentView.layer.borderColor = UIColorFromRGB(0xCCCCCC).CGColor; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; } return self; } +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; +} + #pragma mark - View Cycle -(void)viewWillLayoutSubviews @@ -240,6 +253,8 @@ - (UITextField *)addTextField:(NSString *)title // Add text field UITextField *txt = [[UITextField alloc] init]; + txt.delegate = self; + txt.returnKeyType = UIReturnKeyDone; txt.borderStyle = UITextBorderStyleRoundedRect; txt.font = [UIFont fontWithName:kDefaultFont size:14.0f]; txt.autocapitalizationType = UITextAutocapitalizationTypeWords; @@ -255,9 +270,54 @@ - (UITextField *)addTextField:(NSString *)title [_contentView addSubview:txt]; [_inputs addObject:txt]; + // If there are other fields in the inputs array, get the previous field and set the + // return key type on that to next. + if (_inputs.count > 1) + { + NSUInteger indexOfCurrentField = [_inputs indexOfObject:txt]; + UITextField *priorField = _inputs[indexOfCurrentField - 1]; + priorField.returnKeyType = UIReturnKeyNext; + } return txt; } +# pragma mark - UITextFieldDelegate + +- (BOOL)textFieldShouldReturn:(UITextField *)textField +{ + // If this is the last object in the inputs array, resign first responder + // as the form is at the end. + if (textField == [_inputs lastObject]) + { + [textField resignFirstResponder]; + } + else // Otherwise find the next field and make it first responder. + { + NSUInteger indexOfCurrentField = [_inputs indexOfObject:textField]; + UITextField *nextField = _inputs[indexOfCurrentField + 1]; + [nextField becomeFirstResponder]; + } + return NO; +} + +- (void)keyboardDidShow:(NSNotification *)notification +{ + [UIView animateWithDuration:0.2f animations:^{ + CGRect f = self.view.frame; + f.origin.y -= KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT; + self.view.frame = f; + }]; +} + +-(void)keyboardDidHide:(NSNotification *)notification +{ + [UIView animateWithDuration:0.2f animations:^{ + CGRect f = self.view.frame; + f.origin.y += KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT; + self.view.frame = f; + }]; +} + #pragma mark - Buttons - (SCLButton *)addButton:(NSString *)title @@ -277,6 +337,14 @@ - (SCLButton *)addButton:(NSString *)title return btn; } +- (SCLButton *)addDoneButtonWithTitle:(NSString *)title +{ + SCLButton *btn = [self addButton:title]; + [btn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside]; + + return btn; +} + - (SCLButton *)addButton:(NSString *)title actionBlock:(ActionBlock)action { SCLButton *btn = [self addButton:title]; @@ -315,12 +383,15 @@ - (void)buttonTapped:(SCLButton *)btn { NSLog(@"Unknown action type for button"); } - [self hideView]; + if([self isVisible]) + { + [self hideView]; + } } #pragma mark - Show Alert --(SCLAlertViewResponder *)showTitle:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle duration:(NSTimeInterval)duration completeText:(NSString *)completeText style:(SCLAlertViewStyle)style +-(SCLAlertViewResponder *)showTitle:(UIViewController *)vc image:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle duration:(NSTimeInterval)duration completeText:(NSString *)completeText style:(SCLAlertViewStyle)style { self.view.alpha = 0; self.rootViewController = vc; @@ -367,6 +438,12 @@ -(SCLAlertViewResponder *)showTitle:(UIViewController *)vc title:(NSString *)tit viewColor = UIColorFromRGB(0xA429FF); iconImage = SCLAlertViewStyleKit.imageOfEdit; break; + + case Custom: + viewColor = color; + iconImage = image; + kCircleIconHeight = kCircleIconHeight * 2; + break; } // Title @@ -417,7 +494,7 @@ -(SCLAlertViewResponder *)showTitle:(UIViewController *)vc title:(NSString *)tit // Add button, if necessary if(completeText != nil) { - [self addButton:completeText target:self selector:@selector(hideView)]; + [self addDoneButtonWithTitle:completeText]; } // Alert view colour and images @@ -473,47 +550,74 @@ -(SCLAlertViewResponder *)showTitle:(UIViewController *)vc title:(NSString *)tit - (void)showSuccess:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Success]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Success]; } - (void)showError:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Error]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Error]; } - (void)showNotice:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Notice]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Notice]; } - (void)showWarning:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Warning]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Warning]; } - (void)showInfo:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Info]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Info]; } - (void)showEdit:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Edit]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Edit]; } - (void)showTitle:(UIViewController *)vc title:(NSString *)title subTitle:(NSString *)subTitle style:(SCLAlertViewStyle)style closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration { - [self showTitle:vc title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:style]; + [self showTitle:vc image:nil color:nil title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:style]; +} + +- (void)showCustom:(UIViewController *)vc image:(UIImage *)image color:(UIColor *)color title:(NSString *)title subTitle:(NSString *)subTitle closeButtonTitle:(NSString *)closeButtonTitle duration:(NSTimeInterval)duration +{ + [self showTitle:vc image:image color:color title:title subTitle:subTitle duration:duration completeText:closeButtonTitle style:Custom]; +} + +#pragma mark - Visibility + +- (BOOL)isVisible +{ + return (self.shadowView.alpha && self.view.alpha); } #pragma mark - Hide Alert -// Close SCLAlertView - (void)hideView +{ + switch (_hideAnimationType) + { + case NoAnimation: + [self NoAnimation]; + break; + + case FadeOut: + [self fadeOut]; + break; + } +} + +#pragma mark - Animations + +- (void)fadeOut { [UIView animateWithDuration:0.2f animations:^{ - self.shadowView.alpha = 0; - self.view.alpha = 0; + self.shadowView.alpha = 0.0f; + self.view.alpha = 0.0f; } completion:^(BOOL completed) { [self.shadowView removeFromSuperview]; [self.view removeFromSuperview]; @@ -521,4 +625,11 @@ - (void)hideView }]; } +- (void)NoAnimation +{ + [self.shadowView removeFromSuperview]; + [self.view removeFromSuperview]; + [self removeFromParentViewController]; +} + @end \ No newline at end of file diff --git a/SCLAlertView/git.png b/SCLAlertView/git.png new file mode 100644 index 0000000..8d11b6d Binary files /dev/null and b/SCLAlertView/git.png differ diff --git a/SCLAlertViewExample/Base.lproj/Storyboard.storyboard b/SCLAlertViewExample/Base.lproj/Storyboard.storyboard index 75830df..eb83715 100644 --- a/SCLAlertViewExample/Base.lproj/Storyboard.storyboard +++ b/SCLAlertViewExample/Base.lproj/Storyboard.storyboard @@ -89,6 +89,15 @@ + diff --git a/SCLAlertViewExample/ViewController.h b/SCLAlertViewExample/ViewController.h index 26e3cb9..eb7f124 100644 --- a/SCLAlertViewExample/ViewController.h +++ b/SCLAlertViewExample/ViewController.h @@ -16,6 +16,7 @@ - (IBAction)showWarning:(id)sender; - (IBAction)showInfo:(id)sender; - (IBAction)showEdit:(id)sender; +- (IBAction)showCustom:(id)sender; @end diff --git a/SCLAlertViewExample/ViewController.m b/SCLAlertViewExample/ViewController.m index 53d1e1c..8139ff4 100644 --- a/SCLAlertViewExample/ViewController.m +++ b/SCLAlertViewExample/ViewController.m @@ -136,6 +136,13 @@ - (IBAction)showWithDuration:(id)sender [alert showNotice:self title:kNoticeTitle subTitle:@"You've just displayed this awesome Pop Up View with 5 seconds duration" closeButtonTitle:nil duration:5.0f]; } +- (IBAction)showCustom:(id)sender { + SCLAlertView *alert = [[SCLAlertView alloc] init]; + + UIColor *color = [UIColor colorWithRed:65.0/255.0 green:64.0/255.0 blue:144.0/255.0 alpha:1.0]; + [alert showCustom:self image:[UIImage imageNamed:@"git"] color:color title:@"Custom" subTitle:@"Add a custom icon and color for your own type of alert!" closeButtonTitle:@"OK" duration:0.0f]; +} + - (void)firstButton { NSLog(@"First button tapped"); diff --git a/ScreenShots/ScreenShot3.png b/ScreenShots/ScreenShot3.png new file mode 100644 index 0000000..77265c5 Binary files /dev/null and b/ScreenShots/ScreenShot3.png differ