利用"animateWithDuration"实现带弹簧动画的顶部通知栏

看到其他App上有带弹簧动画的顶部通知栏,于是研究了实现方法,封装了一下实现了效果,代码如下:


PBFSpringEffectAlertView.h

//
//  PBFSpringEffectAlertView.h
//  BoyingCaptial
//
//  Created by BY-MAC01 on 16/1/6.
//  Copyright © 2016年 BY-MAC01. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol PBFSpringEffectAlertViewDatasource
//装备View
- (void)assmebleViewTopBar:(UIView *)viewBackground;
@end

@interface PBFSpringEffectAlertView : UIView

- (instancetype)initWithSize:(CGSize)size;

@property (nonatomic,weak)id<PBFSpringEffectAlertViewDatasource>     datasource;

//弹出
- (void)show;
//弹回
- (void)hide;
@end



PBFSpringEffectAlertView.m

//
//  PBFSpringEffectAlertView.m
//  BoyingCaptial
//
//  Created by BY-MAC01 on 16/1/6.
//  Copyright © 2016年 BY-MAC01. All rights reserved.
//

#import "PBFSpringEffectAlertView.h"

@interface PBFSpringEffectAlertView()
//背景
@property (nonatomic,strong)UIView                  *VIEWBackground;
@end

@implementation PBFSpringEffectAlertView
- (instancetype)initWithSize:(CGSize)size{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7f]];
    
    //白色背景
    CGRect rect = [UIScreen mainScreen].bounds;
    self.VIEWBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0,size.width, size.height)];
    [self.VIEWBackground setBackgroundColor:[UIColor whiteColor]];
    [self.VIEWBackground.layer setCornerRadius:3.0f];
    self.VIEWBackground.center = CGPointMake(rect.size.width/2, rect.size.height/2);
    
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    [self addSubview:self.VIEWBackground];
    if (self.datasource) {
        [self.datasource assmebleViewTopBar:self.VIEWBackground];
    }
}

//弹出
- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    CGRect rect = [UIScreen mainScreen].bounds;
    CGRect rectBackground = self.VIEWBackground.frame;
    rectBackground.origin.y = -rectBackground.size.height;
    [self.VIEWBackground setFrame:rectBackground];
    [self.VIEWBackground setAlpha:0];
    [UIView animateWithDuration:0.7f delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.VIEWBackground.center = CGPointMake(rect.size.width/2, rect.size.height/2);
        [self.VIEWBackground setAlpha:1];
    } completion:^(BOOL finished) {
        [self hide];
    }];
}

//弹回
- (void)hide{
    [UIView animateWithDuration:0.3f animations:^{
        CGRect rectBackground = self.VIEWBackground.frame;
        rectBackground.origin.y = -rectBackground.size.height;
        [self.VIEWBackground setFrame:rectBackground];
        [self.VIEWBackground setAlpha:0];
    } completion:^(BOOL finished) {
        if([self superview]){
            [self removeFromSuperview];
        }
    }];

}

@end


效果如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值