WYLNotificationView 1.1.1

WYLNotificationView 1.1.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年5月

wangyanlong维护。



  • By
  • 王颜龙

为公司封装的一个简单的吐司view(公司内部称之为吐司...)

效果图

图1 普通吐司

普通吐司

图2 节日吐司

节日吐司

图3 特殊吐司

特殊吐司

开始使用

使用CocoaPods

1. 在你的podfile中添加pod WYLNotificationView

pod 'WYLNotificationView'

2. 在终端中运行pod install,然后打开新生成的xcworkspace

3. 在你需要添加的地方添加头文件WYLNotificationView.h

通过GitHub集成

1. 下载WYLNotificationView文件夹,放入你的project

2. 在你需要添加的地方添加头文件WYLNotificationView.h

如何使用

1. 在你的项目中的AppDelegate.h文件和任何你想发送吐司的文件里,添加#import"WYLNotification.h"

2. 如果你想发送普通的吐司需求,你可以写下如下代码。例如

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showTS:) name:kTSNotification object:nil];

return YES;
}

- (void)showTS:(NSNotification *)not{

NSDictionary *dict = [not userInfo];

WYLNotificationView *view = nil;
view = (WYLNotificationView *)[self.window viewWithTag:15000];

if (view){

    view.tusiStr = [dict objectForKey:kTUCAOStr];
    view.duration = [[dict objectForKey:kTUCAODuration] floatValue];
    [view keepSelf];

}else{

    view = [[WYLNotificationView alloc]init];

    view.window = self.window;
    view.tusiStr = [dict objectForKey:kTUCAOStr];
    view.height = self.window.frame.size.height / 10.0f;
    view.font = [UIFont systemFontOfSize:20];
    view.duration = [[dict objectForKey:kTUCAODuration] floatValue];

    [view createTuSi];

}

}

我们将吐司的view添加到window上,然后使其可以在任意界面展示。如果你想发送,你只需要配置一个dict,然后post它即可,例如

- (IBAction)post:(id)sender {

NSDictionary *dict = @{kTUCAOStr:[NSString stringWithFormat:@"这次的测试数字是 : %d",255 - (arc4random()%255)],kTUCAODuration:@(1)};

[[NSNotificationCenter defaultCenter] postNotificationName:kTSNotification object:nil userInfo:dict];

}

其中,`kTUCAOStr`是要发送字符串的key,`kTUCAODuration`是要展示的时间。最后效果如 图1

3. 如果你想发送类似节日的吐司需求,你可以写下如下代码。例如

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHoliday:) name:kHolidayNotification object:nil];

return YES;
}

- (void)showHoliday:(NSNotification *)not{

for (int i = 0; i < 15; i++) {

    @autoreleasepool {

        UIImage *image = (UIImage *)[not object];

        int x = (arc4random()%(int)(self.window.frame.size.width)) - image.size.width;

        WYLHolidayNotificationView *view =[[WYLHolidayNotificationView alloc]initWithFrame:CGRectMake(x, -100, 50, 50)];
        view.size = self.window.frame.size;
        view.image = image;

        [self.window addSubview:view];
        [self.window bringSubviewToFront:view];

        [view startDropOut];

    }

}

}

你只需要把一个你想要展示的图片文件传入dict然后发送出去即可,效果如图2。例如

- (IBAction)post2:(UIButton *)sender {

UIImage *image = [UIImage imageNamed:@"girl"];

[[NSNotificationCenter defaultCenter] postNotificationName:kHolidayNotification object:image userInfo:nil];

}

4. 如果你想要发送带一个按钮的特殊需求,你可以写下如下代码。例如

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSpecialNotification:) name:kTSSpecialNotification object:nil];

return YES;
}

- (void)showSpecialNotification:(NSNotification *)not{

NSDictionary *dict = [not userInfo];

WYLSpecialNotificationView *view = nil;
view = (WYLSpecialNotificationView *)[self.window viewWithTag:16000];

if (view) {
    view.tusiStr = [dict objectForKey:kTUCAOStr];
    [view keepSelf];
}else{
    view = [[WYLSpecialNotificationView alloc]init];

    view.window = self.window;
    view.tusiStr = [dict objectForKey:kTUCAOStr];
    view.height = self.window.frame.size.height / 10.0f;
    view.font = [UIFont systemFontOfSize:20];
    view.duration = [[dict objectForKey:kTUCAODuration] floatValue];
    view.btnStr = [dict objectForKey:kTUBtnStr];

    __weak WYLSpecialNotificationView *weakView = view;
    view.callBtn = ^{
        [weakView removeSelf];
    };

    [view createTuSi];
    [view createBtn];
}

}

这次你除了需要传递一个要展示的字符串和展示时间以外,你还需要传递一个按钮的名字。效果如图3。例如

- (IBAction)post3:(UIButton *)sender {

NSDictionary *dict = @{kTUCAOStr:[NSString stringWithFormat:@"这次的测试数字是 : %d",255 - (arc4random()%255)],kTUCAODuration:@(1),kTUBtnStr:@"继续下载"};

[[NSNotificationCenter defaultCenter] postNotificationName:kTSSpecialNotification object:nil userInfo:dict];

}

注意:特殊吐司不会自动消失,而是添加了手势上滑消失或点击按钮消失的选项。

版本

1.0.7

1. 修改了消失的逻辑

2. 特殊吐司开放出了btn属性

3. 调整了一些颜色

1.0.8

1. 修改了特殊吐司消失的逻辑

1.1.0

1. 为特殊吐司加入了箭头图片

许可证

MIT