沒有妹紙你們是不會進來的!
- 今天第一次發文章,才發現寫個文章水也很深啊。
- 廢話不多說。來說一說YsyNoticeView
- 由于馬上項目需要更新,產品大大做了相關原型,于是我這個小小的程序猿開始了各種封裝
QQ點贊已滿的提示
- 產品中有一個提示跟QQ點贊的提示類似的效果。所以就寫了這樣一個demo。
-
因為gif制作太麻煩了。所以用的都是截圖。哈哈請見諒。下面是demo的部分截圖。
YsyNoticeView - 并且給view添加了手勢
- 使用的話直接調用類方法
[YsyNoticeView showInfo:@"網絡出錯" withStyle:YsyNoticeStyleTop];
- 其中YsyNoticeStyle是選擇展示方式,有上中下三種效果。
typedef NS_ENUM(NSUInteger, YsyNoticeStyle)
{
YsyNoticeStyleTop = 0,
YsyNoticeStyleTableView,
YsyNoticeStyleBottom
} ;
- YsyNoticeStyleTableView,類似汽車之家刷新之后的效果。
YsyNoticeStyleTableView效果
-
YsyNoticeStyleBottom,在屏幕下方淡出的效果。
YsyNoticeStyleBottom效果 如果你的tableView位置不是常規的位置的話 使用下面的代碼調用
+(void)showInfo:(NSString *)info withTableView:(UITableView *)tableView;
- 例如下圖
刷新
![刷新結束]](http://upload-images.jianshu.io/upload_images/1830398-93618a5e73ad212c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 實現方法其實很簡單 簡單解釋下YsyNoticeStyleTop代碼吧
- 第一步 創建topview 和UIImageView 還有label
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監聽手勢。(用于反饋用戶上滑)
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];
// 彈簧動畫,參數分別為:時長,延時,彈性(越小彈性越大),初始速度
[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];
}];
});
}
}];
}
- 這樣就完成了。雖然其中還有很多細節可以完善(例如使用單例,防止重復創建 等等 )。但是暫時可以滿足業務需求于是也沒多多加雕琢。(__) 嘻嘻……
- Git Hub下載地址YsyNoticeViewDemo