APP內(nèi)有各種提示锤悄,從navgationBar 彈出的view 就是一種,類似于微博的更新投剥。大體的思路就是一個(gè)View 加 UILabel 還有一個(gè)動(dòng)畫输吏,實(shí)現(xiàn)方法都具有多樣性,在這里按照自己的思路寫一下色洞。
1戏锹、由于本人不愿意使用代碼布局,所以采用XIB火诸,看自己習(xí)慣锦针。純代碼、Masonry 置蜀、SDAtuoLauyout 布局都很簡(jiǎn)單
2奈搜、.h 文件,可以設(shè)由外部控制的屬性盾碗,比如lab 的文字媚污、背景顏色
@interface EHNavTopNoData : UIView
//顯示文字
@property (nonatomic,copy) NSString *alertTitle;
//展示
- (void) show;
@end
3、重寫Set 方法賦值
- (void) setAlertTitle:(NSString *)alertTitle
{
_alertTitle = alertTitle;
self.titleLab.text = _alertTitle;
}
4廷雅、實(shí)現(xiàn)動(dòng)畫方法
- (void) show
{
//向下移動(dòng)的同時(shí)改變透明度 時(shí)間持續(xù)2秒
CAAnimationGroup *gropAnimation = [CAAnimationGroup animation];
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = [NSNumber numberWithFloat:0];
opacityAnimation.toValue = [NSNumber numberWithFloat:1];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint: self.layer.position];
CGPoint toPoint = self.layer.position;
toPoint.y += 40;
animation.toValue = [NSValue valueWithCGPoint:toPoint];
gropAnimation.animations = @[opacityAnimation,animation];
gropAnimation.duration = 0.5;
gropAnimation.removedOnCompletion = NO;
gropAnimation.fillMode = kCAFillModeForwards;
[self.layer addAnimation:gropAnimation forKey:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.layer removeAllAnimations];
});
}
4耗美、使用的時(shí)候,放到你自己想要的圖層航缀。以 xib 的方式加載,
//設(shè)置View為成員變量
_noDataV = [[[NSBundle mainBundle] loadNibNamed:@"EHNavTopNoData" owner:nil options:nil] lastObject];
_noDataV.frame = CGRectMake(0, 24, SCREEN_WIDTH, 40);
[self.view addSubview:_noDataV];
[self.view insertSubview:_noDataV aboveSubview:self.collectionView];
5商架、需要展示的地方調(diào)用 show
[_noDataV show];