diff --git a/README.md b/README.md index e7a2bcd..6beccb9 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ RKOTextView *textViewWithCode = [RKOTextView textViewWithFrame:frame ### RKOTopAlert

- + @@ -89,23 +89,15 @@ RKOTextView *textViewWithCode = [RKOTextView textViewWithFrame:frame #### 集成: ```shell - pod 'RKOTopAlert', '~> 1.0.2' + pod 'RKOTopAlert', '~> 1.0.3' ``` #### 使用: ```objc -[RKOTopAlert popAlertViewWithText:@"提醒文字" textColor:[UIColor redColor] ackgroundColor:[UIColor blackColor]; -``` - -或者: - -```objc -// 创建单例并设置样式。 -RKOTopAlert *topAlert = [[self sharedManager] alertViewWithText:@"提醒文字" textColor:[UIColor whiteColor] ackgroundColor:[UIColor redColor]]; +RKOTopAlert *topAlert = [RKOTopAlert alertViewWithText:@"提示文字" textColor:[UIColor whiteColor] ackgroundColor:[UIColor redColor]]; -// 出现 -[topAlert alertAppearWithDuration:0.3f]; +[topAlert alertAppearWithDuration:2.0]; ``` --------------------------------------------------------------------- diff --git a/RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.m b/RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.m index bda1556..376f599 100644 --- a/RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.m +++ b/RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.m @@ -23,9 +23,6 @@ @interface RKOTextView () // TextView的最大高度。 @property (nonatomic, assign) CGFloat maxTextH; -// 达到最大标志的标识符。 -@property (nonatomic, assign) BOOL achMaxTextH; - @end @implementation RKOTextView diff --git a/RKOTopAlertManager/README.md b/RKOTopAlertManager/README.md index ac219eb..26cb3c1 100644 --- a/RKOTopAlertManager/README.md +++ b/RKOTopAlertManager/README.md @@ -1,7 +1,7 @@ # RKOTopAlert

- + @@ -11,6 +11,8 @@ 自定义一个顶端的`Alert`提示窗。弹出时从顶端向下移动。(**在iPhone X下可用**) +随仓库配套了一个简单的 **演示Demo**,方便大家使用参考。 + 可以设置**提示文字**、**文字颜色**、**背景颜色**。 **高度**为`Status` + `NavigationBar`的高度。(不论您的`ViewController`是否添加到`NavigationController`中) @@ -30,75 +32,61 @@ ## 集成 ```shell - pod 'RKOTopAlert', '~> 1.0.2' + pod 'RKOTopAlert', '~> 1.0.3' ``` ## 使用 -在需要弹出该提示窗的地方调用下面的方法: +使用下面的方法创建一个`Alert`视图,对`Alert`视图调用 `alertAppearWithDuration` 并设置时候,即可弹出 `Alert`视图。 ```objc -[RKOTopAlert popAlertViewWithText:@"提醒文字" textColor:[UIColor whiteColor] ackgroundColor:[UIColor redColor]; +RKOTopAlert *topAlert = [RKOTopAlert alertViewWithText:@"提示文字" textColor:[UIColor whiteColor] ackgroundColor:[UIColor redColor]]; + +[topAlert alertAppearWithDuration:2.0]; ``` -如果您的`App`需要统一多个提示窗的样式,那么推荐您使用下面的方法: +如果您的`App`需要统一多个提示窗的样式,那么推荐您在 `AppDelegate` 中进行设置: ```objc -// 创建单例并设置样式。 -RKOTopAlert *topAlert = [[self sharedManager] alertViewWithText:@"提醒文字" textColor:[UIColor whiteColor] ackgroundColor:[UIColor redColor]]; - -// 出现 -[topAlert alertAppearWithDuration:0.3f]; +// 在 AppDelegate.h 中设置属性 +@property (nonatomic, strong) RKOTopAlert *topAlert; + +// 懒加载属性,创建对象并设置样式。 +- (RKOTopAlert *)topAlert { + return [RKOTopAlert alertViewWithText:@"在AppDelegate中总体设置" textColor:[UIColor blackColor] ackgroundColor:[UIColor orangeColor]]; +} + +// 在按钮方法或其它方法中,获取属性调用弹出方法。 +- (IBAction)popAlert:(id)sender { + [((AppDelegate *)[UIApplication sharedApplication].delegate).topAlert alertAppearWithDuration:2.0]; +} ``` ## 接口 -该提示窗提供一个方法用于设置样式并弹出提示窗。 +本控件提供一个工厂方法,用以创建对象并设置样式: ```objc /** * 设置提示窗的样式,并弹出提示窗。(其对象参数均不可为nil。) - - * @param text 提示窗显示文字。 - * @param textColor 文字颜色。 - * @param backgroundColor 提示窗背景颜色。 - * @param duration 横幅持续显示的时间。 - */ -+ (void)popAlertViewWithText:(NSString *)text - textColor:(UIColor *)textColor - ackgroundColor:(UIColor *)backgroundColor - duration:(CGFloat)duration; -``` - -此外,考虑到您可以需要统一的设置多个弹窗,我们提供如下的方法进行设置: - -```objc -/** - * 单例方法,创建对象。 - - * @return 提示窗Alert。 - */ -+ (RKOTopAlert *)sharedManager; - -/** - * 设置样式。(其对象参数均不可为nil。) - + * @param text 提示窗显示文字。 * @param textColor 文字颜色。 * @param backgroundColor 提示窗背景颜色。 + * @return 调用对象本身 */ -- (void)alertViewWithText:(NSString *)text - textColor:(UIColor *)textColor - ackgroundColor:(UIColor *)backgroundColor; ++ (instancetype)alertViewWithText:(NSString *)text + textColor:(UIColor *)textColor + ackgroundColor:(UIColor *)backgroundColor; ``` -此外,我们分别提供了弹窗弹窗的**弹出**方法与**消失**方法,方便您在他处动态地控制弹窗的弹出与消失: +此外,我们分别提供了弹窗弹窗的**弹出**方法与**消失**方法,方便您动态地控制弹窗的弹出与消失: ```objc /** * 弹出提示窗的方法。 - * @param duration 横幅持续显示的时间。 + * @param duration 横幅持续显示的时间。如果传0,则需要手动调用alertDisappear方法使视图消失。 */ - (void)alertAppearWithDuration:(CGFloat)duration; diff --git a/RKOTopAlertManager/RKOTopAlert.podspec b/RKOTopAlertManager/RKOTopAlert.podspec index 34cdad9..722d6bc 100644 --- a/RKOTopAlertManager/RKOTopAlert.podspec +++ b/RKOTopAlertManager/RKOTopAlert.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = "RKOTopAlert" - s.version = "1.0.2" + s.version = "1.0.3" s.summary = "Appears at the top of the notification view" s.description = <<-DESC Appears at the top of the notification view @@ -23,7 +23,7 @@ Pod::Spec.new do |s| s.ios.deployment_target = "7.0" - s.source = { :git => "https://github.com/rakuyoMo/RKOControls.git", :tag => "1.0.5" } # #{s.version} + s.source = { :git => "https://github.com/rakuyoMo/RKOControls.git", :tag => "1.0.7" } # #{s.version} s.requires_arc = true diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.h b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.h index 3e69b57..5c7f9a7 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.h +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.h @@ -8,10 +8,14 @@ #import +@class RKOTopAlert; + @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; +// 顶部的提示视图 +@property (nonatomic, strong) RKOTopAlert *topAlert; @end diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.m b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.m index 9468683..1d3ad17 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.m +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/AppDelegate.m @@ -7,6 +7,7 @@ // #import "AppDelegate.h" +#import "RKOTopAlert.h" @interface AppDelegate () @@ -14,6 +15,10 @@ @interface AppDelegate () @implementation AppDelegate +#pragma mark - Top Alert +- (RKOTopAlert *)topAlert { + return [RKOTopAlert alertViewWithText:@"在AppDelegate中总体设置" textColor:[UIColor blackColor] ackgroundColor:[UIColor orangeColor]]; +} - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Assets.xcassets/AppIcon.appiconset/Contents.json b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Assets.xcassets/AppIcon.appiconset/Contents.json index 118c98f..19882d5 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", @@ -29,6 +39,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "3x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" } ], "info" : { diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Base.lproj/Main.storyboard b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Base.lproj/Main.storyboard index 4529698..5f3a135 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Base.lproj/Main.storyboard +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/Base.lproj/Main.storyboard @@ -1,14 +1,18 @@ - - + + + + + - + + - + @@ -16,11 +20,30 @@ + + + + + diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.h b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.h index 518b281..6c6b3cc 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.h +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.h @@ -19,43 +19,22 @@ NS_ASSUME_NONNULL_BEGIN @interface RKOTopAlert : UIView -#pragma mark - 分别设置每个弹窗样式。 /** * 设置提示窗的样式,并弹出提示窗。(其对象参数均不可为nil。) * @param text 提示窗显示文字。 * @param textColor 文字颜色。 * @param backgroundColor 提示窗背景颜色。 - * @param duration 横幅持续显示的时间。 + * @return 调用对象本身 */ -+ (void)popAlertViewWithText:(NSString *)text - textColor:(UIColor *)textColor - ackgroundColor:(UIColor *)backgroundColor - duration:(CGFloat)duration; - -#pragma mark - 统一设置App所有弹窗样式 -/** - * 单例方法,创建对象。 - - * @return 提示窗Alert。 - */ -+ (RKOTopAlert *)sharedManager; - -/** - 设置样式。(其对象参数均不可为nil。) - - @param text 提示窗显示文字。 - @param textColor 文字颜色。 - @param backgroundColor 提示窗背景颜色。 - */ -- (void)alertViewWithText:(NSString *)text - textColor:(UIColor *)textColor - ackgroundColor:(UIColor *)backgroundColor; ++ (instancetype)alertViewWithText:(NSString *)text + textColor:(UIColor *)textColor + ackgroundColor:(UIColor *)backgroundColor; /** * 弹出提示窗的方法。 - * @param duration 横幅持续显示的时间。 + * @param duration 横幅持续显示的时间。如果传0,则需要手动调用alertDisappear方法使视图消失。 */ - (void)alertAppearWithDuration:(CGFloat)duration; @@ -67,4 +46,3 @@ NS_ASSUME_NONNULL_BEGIN @end NS_ASSUME_NONNULL_END - diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.m b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.m index a9c236a..6890231 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.m +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/RKOTopAlert/RKOTopAlert.m @@ -8,6 +8,8 @@ #import "RKOTopAlert.h" +#define weakify_self __weak typeof(self) weakSelf = self + // 记录高度的结构体。 struct { CGFloat statusbarH; @@ -17,11 +19,8 @@ @interface RKOTopAlert() -// 提示文字。 -@property (nonatomic, copy) NSString *text; - -// 文字的颜色。 -@property (nonatomic, strong) UIColor *textColor; +// 提示内容的Label +@property (nonatomic, strong) UILabel *contentLable; // 标记视图将要消失 @property (nonatomic, assign) BOOL alertWillDisappear; @@ -54,7 +53,7 @@ + (RKOTopAlert *)sharedManager { topHight.alertViewH = topHight.navigationH + topHight.statusbarH; // 初始化提示窗,并设置Frame - instance = [[self alloc] initWithFrame:CGRectMake(0, -topHight.alertViewH, vc.navigationBar.frame.size.width, topHight.alertViewH)]; + instance = [[self alloc] initWithFrame:CGRectMake(0, -topHight.alertViewH, [UIScreen mainScreen].bounds.size.width, topHight.alertViewH)]; // 添加点击及滑动手势 [instance addGestureRecognizer]; @@ -64,79 +63,73 @@ + (RKOTopAlert *)sharedManager { } // 设置样式并提示窗 -+ (void)popAlertViewWithText:(NSString *)text textColor:(UIColor *)textColor ackgroundColor:(UIColor *)backgroundColor duration:(CGFloat)duration { - - // 创建单例 - RKOTopAlert *topAlert = [self sharedManager]; ++ (instancetype)alertViewWithText:(NSString *)text textColor:(UIColor *)textColor ackgroundColor:(UIColor *)backgroundColor { - // 设置样式 - [topAlert alertViewWithText:text textColor:textColor ackgroundColor:backgroundColor]; - - // 出现 - [topAlert alertAppearWithDuration:duration]; + // 创建对象并设置样式。 + return [[self sharedManager] alertViewWithText:text textColor:textColor ackgroundColor:backgroundColor]; } // 设置样式。 -- (void)alertViewWithText:(NSString *)text textColor:(UIColor *)textColor ackgroundColor:(UIColor *)backgroundColor { +- (instancetype)alertViewWithText:(NSString *)text textColor:(UIColor *)textColor ackgroundColor:(UIColor *)backgroundColor { + + // 如果已经被添加,则不再出现。 + if (self.superview) { + return self; + } // 设置背景颜色。 self.backgroundColor = backgroundColor; - // 设置文字颜色。 - self.textColor = textColor; + // 初始化按钮 + self.contentLable = [[UILabel alloc] initWithFrame:CGRectMake(0, topHight.statusbarH, self.frame.size.width, topHight.navigationH)]; // 设置提示内容。 - self.text = text; -} - -#pragma mark - Set && Get -- (void)setText:(NSString *)text { - _text = text; - - // 设置显示文字。 - UILabel *alertLabel = [[UILabel alloc] init]; - alertLabel.text = text; - alertLabel.textColor = _textColor; - alertLabel.font = [UIFont boldSystemFontOfSize:FONTSIZE]; - alertLabel.backgroundColor = [UIColor clearColor]; + self.contentLable.text = text; + // 设置文字颜色。 + self.contentLable.textColor = textColor; + // 设置字体字号 + self.contentLable.font = [UIFont boldSystemFontOfSize:FONTSIZE]; + // 设置背景颜色 + self.contentLable.backgroundColor = [UIColor clearColor]; // 水平居中 - alertLabel.textAlignment = NSTextAlignmentCenter; - alertLabel.frame = CGRectMake(0, topHight.statusbarH, self.frame.size.width, topHight.navigationH); + self.contentLable.textAlignment = NSTextAlignmentCenter; - // 添加视图 - [self addSubview:alertLabel]; + return self; } #pragma mark - Animate // 出现的动画 - (void)alertAppearWithDuration:(CGFloat)duration { - NSLog(@"alertAppearWithDuration"); - // 如果已经被添加,则不再出现。 if (self.superview) { return; } + // 添加视图 + [self addSubview:self.contentLable]; + UIWindow *keyWindow = [UIApplication sharedApplication].delegate.window; [keyWindow addSubview:self]; // 显示到最上层。 [keyWindow bringSubviewToFront:self]; __block CGRect alertFrame = self.frame; + weakify_self; [UIView animateWithDuration:ALERT_APPEAR_ANIMATE_DURATION animations:^{ // 向下移动,显示提示窗。 alertFrame.origin.y = 0; - self.frame = alertFrame; + weakSelf.frame = alertFrame; } completion:^(BOOL finished) { // 显示动画完成 - if (finished) { + if (finished && duration != 0) { // duration秒后横幅自动消失 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 消失。 - [self alertDisappear]; + [weakSelf alertDisappear]; + self.alertWillDisappear = NO; }); } }]; @@ -151,25 +144,24 @@ - (void)alertDisappear { } __block CGRect alertFrame = self.frame; + weakify_self; //移除横幅动画,设置完全透明并从父视图中移除 [UIView animateWithDuration:ALERT_DISAPPEAR_ANIMATE_DURATION animations:^{ - self.alertWillDisappear = YES; + weakSelf.alertWillDisappear = YES; // 向上移动消失。 alertFrame.origin.y = -topHight.alertViewH; - self.frame = alertFrame; + weakSelf.frame = alertFrame; } completion:^(BOOL finished) { if (finished) { - UILabel *alertLabel = self.subviews[0]; - alertLabel.text = nil; - alertLabel.textColor = nil; - self.backgroundColor = nil; // 从父视图中移除。 - [self removeFromSuperview]; + [weakSelf.contentLable removeFromSuperview]; + [weakSelf removeFromSuperview]; + } }]; } @@ -193,4 +185,3 @@ - (void)addGestureRecognizer { } @end - diff --git a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/ViewController.m b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/ViewController.m index a7a34f1..3d0fb95 100644 --- a/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/ViewController.m +++ b/RKOTopAlertManager/RKOTopAlert/RKOTopAlert/ViewController.m @@ -7,6 +7,8 @@ // #import "ViewController.h" +#import "AppDelegate.h" +#import "RKOTopAlert.h" @interface ViewController () @@ -16,13 +18,18 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + } +- (IBAction)popAlert:(id)sender { + [((AppDelegate *)[UIApplication sharedApplication].delegate).topAlert alertAppearWithDuration:2.0]; +} -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. +- (IBAction)popAlertTwo:(id)sender { + + RKOTopAlert *topAlert = [RKOTopAlert alertViewWithText:@"单独设置提示文字" textColor:[UIColor whiteColor] ackgroundColor:[UIColor redColor]]; + + [topAlert alertAppearWithDuration:2.0]; }