美文网首页程序员iOS开发
Nav 向下的弹出提醒View

Nav 向下的弹出提醒View

作者: 朱Younger | 来源:发表于2017-03-28 11:29 被阅读63次

APP内有各种提示,从navgationBar 弹出的view 就是一种,类似于微博的更新。大体的思路就是一个View 加 UILabel 还有一个动画,实现方法都具有多样性,在这里按照自己的思路写一下。
1、由于本人不愿意使用代码布局,所以采用XIB,看自己习惯。纯代码、Masonry 、SDAtuoLauyout 布局都很简单


QQ20170328-111709.png

2、.h 文件,可以设由外部控制的属性,比如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、实现动画方法

- (void) show
{
    //向下移动的同时改变透明度 时间持续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、使用的时候,放到你自己想要的图层。以 xib 的方式加载,

//设置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、需要展示的地方调用 show

[_noDataV show];

相关文章