用pop动画简单实现弹窗的缩放和渐变,感觉这个动画常用,就写一下博客
pop动画是Facebook推出的动画引擎,请自行到GitHub上搜索下载拖拽导入xcode项目中。
更多pop动画使用和原理可网上搜索学习
本处只简单介绍代码开发使用,紧以弹窗效果为思路。
1. 控制器导入头文件 #import "POP.h"
2.创建弹窗透明黑色背景
- - (void)createBlackView {
- self.blackView = [[UIView alloc] initWithFrame:self.contentView.bounds];
- self.blackView.backgroundColor = [UIColor blackColor];
- self.blackView.alpha = 0;
- [self addSubview:self.blackView];
- [UIView animateWithDuration:0.3f animations:^{
- self.blackView.alpha = 0.25f;
- }];
- }
3.创建白色上面的白色view,并加上所需的控件,给白色的messageView做pop动画效果,
代码如下
- // 执行动画 改变透明度
- POPBasicAnimation *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
- alpha.toValue = @(1.f);
- alpha.duration = 0.3f;
- [self.messageView pop_addAnimation:alpha forKey:nil];
- // 缩放回弹
- POPSpringAnimation *scale = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
- scale.fromValue = [NSValue valueWithCGSize:CGSizeMake(1.75f, 1.75f)];
- scale.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
- scale.dynamicsTension = 1000;
- scale.dynamicsMass = 1.3;
- scale.dynamicsFriction = 10.3;
- scale.springSpeed = 20;
- scale.springBounciness = 15.64;
- scale.delegate = self;
- [self.messageView.layer pop_addAnimation:scale forKey:nil];
4.弹窗消失的思路代码
- - (void)removeViews {
- [UIView animateWithDuration:0.2f animations:^{
- self.blackView.alpha = 0.f;
- self.messageView.alpha = 0.f;
- self.messageView.transform = CGAffineTransformMakeScale(0.75f, 0.75f);
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }