看到其他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