- 今天第一次發(fā)文章溺蕉,才發(fā)現(xiàn)寫個文章水也很深啊某宪。
- 廢話不多說。來說一說YsyNoticeView
- 由于馬上項目需要更新掏秩,產(chǎn)品大大做了相關(guān)原型或舞,于是我這個小小的程序猿開始了各種封裝
- 產(chǎn)品中有一個提示跟QQ點贊的提示類似的效果。所以就寫了這樣一個demo蒙幻。
-
因為gif制作太麻煩了映凳。所以用的都是截圖。哈哈請見諒邮破。下面是demo的部分截圖诈豌。
- 并且給view添加了手勢
- 使用的話直接調(diào)用類方法
[YsyNoticeView showInfo:@"網(wǎng)絡(luò)出錯" withStyle:YsyNoticeStyleTop];
- 其中YsyNoticeStyle是選擇展示方式抒和,有上中下三種效果矫渔。
typedef NS_ENUM(NSUInteger, YsyNoticeStyle)
{
YsyNoticeStyleTop = 0,
YsyNoticeStyleTableView,
YsyNoticeStyleBottom
} ;
- YsyNoticeStyleTableView,類似汽車之家刷新之后的效果摧莽。
-
YsyNoticeStyleBottom庙洼,在屏幕下方淡出的效果。
如果你的tableView位置不是常規(guī)的位置的話 使用下面的代碼調(diào)用
+(void)showInfo:(NSString *)info withTableView:(UITableView *)tableView;
- 例如下圖
![刷新結(jié)束]](http://upload-images.jianshu.io/upload_images/1830398-93618a5e73ad212c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 實現(xiàn)方法其實很簡單 簡單解釋下YsyNoticeStyleTop代碼吧
- 第一步 創(chuàng)建topview 和UIImageView 還有l(wèi)abel
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, -height, SCREEN_WIDTH, height)];
topView.userInteractionEnabled = YES;
topView.backgroundColor = [UIColor whiteColor];
topView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
topView.layer.shadowOpacity = 1;
topView.layer.shadowRadius = 5;
topView.layer.shadowOffset = CGSizeMake(3.0, 3.0);
UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 64 - 20 - 11 , 20, 20)];
icon.image = [UIImage imageNamed:@"提示"];
[topView addSubview:icon];
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + 20 + 10, 64 - 20 - 11, SCREEN_WIDTH - 40 - 20, 20)];
textLabel.text = info;
textLabel.textColor = [UIColor blackColor];
textLabel.font = [UIFont boldSystemFontOfSize:14.0];
[topView addSubview:textLabel];
- 第二步 給top添加手勢以及使用RAC監(jiān)聽手勢。(用于反饋用戶上滑)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
[[pan rac_gestureSignal] subscribeNext:^(UIPanGestureRecognizer *pan) {
NSLog(@"%@", pan);
[UIView animateWithDuration:0.7 animations:^{
pan.view.frame = CGRectMake(0, -height, SCREEN_WIDTH, height);
} completion:^(BOOL finished) {
[pan.view removeFromSuperview];
}];
[pan.view removeGestureRecognizer:pan];
}];
[topView addGestureRecognizer:pan];
- 第三步 將topView直接添加到window上并且添加彈簧動畫油够,然后再1.5秒之后自動上已隱藏
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:topView];
// 彈簧動畫蚁袭,參數(shù)分別為:時長,延時叠聋,彈性(越小彈性越大)撕阎,初始速度
[UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:0.3 options:0 animations:^{
topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
} completion:^(BOOL finished) {
if (finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.7 animations:^{
topView.frame = CGRectMake(0, -height, SCREEN_WIDTH, height);
} completion:^(BOOL finished) {
[topView removeFromSuperview];
}];
});
}
}];
}
- 這樣就完成了。雖然其中還有很多細(xì)節(jié)可以完善(例如使用單例碌补,防止重復(fù)創(chuàng)建 等等 )。但是暫時可以滿足業(yè)務(wù)需求于是也沒多多加雕琢棉饶。(__) 嘻嘻……
- Git Hub下載地址YsyNoticeViewDemo