POP动画[2]
POP动画[2]
1:定制控制器间的转场动画.
源码有点多-_-!!
//
// RootViewController.h
// Animation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.h
//
// RootViewController.m
// Animation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h" #import "ModelViewController.h"
#import "PresentingAnimator.h"
#import "DismissingAnimator.h" #import "POP.h" @interface RootViewController ()<UIViewControllerTransitioningDelegate> @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; UIButton *presentButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[self.view addSubview:presentButton];
presentButton.center = self.view.center;
presentButton.layer.cornerRadius = .f;
presentButton.backgroundColor = [UIColor redColor]; [presentButton setTitle:@"推出控制器"
forState:UIControlStateNormal]; // 添加button事件
[presentButton addTarget:self
action:@selector(present:)
forControlEvents:UIControlEventTouchUpInside];
} - (void)present:(id)sender
{
// 推出控制器
ModelViewController *modalViewController = [ModelViewController new]; // 设置转场动画代理
modalViewController.transitioningDelegate = self; // 定制转场动画
modalViewController.modalPresentationStyle = UIModalPresentationCustom; [self.navigationController presentViewController:modalViewController
animated:YES
completion:NULL];
} #pragma mark - UIViewControllerTransitioningDelegate - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
// 推出控制器的动画
return [PresentingAnimator new];
} - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
// dismissing控制器的动画
return [DismissingAnimator new];
} @end
RootViewController.m
//
// ModelViewController.h
// Animation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface ModelViewController : UIViewController @end
ModelViewController.h
//
// ModelViewController.m
// Animation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ModelViewController.h" @interface ModelViewController () @end @implementation ModelViewController - (void)viewDidLoad
{
[super viewDidLoad]; self.view.layer.cornerRadius = .f;
self.view.backgroundColor = [UIColor blackColor];
[self addDismissButton];
} - (void)addDismissButton
{
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeSystem];
dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
dismissButton.tintColor = [UIColor whiteColor];
dismissButton.titleLabel.font = [UIFont fontWithName:@"Avenir" size:];
[dismissButton setTitle:@"Dismiss" forState:UIControlStateNormal];
[dismissButton addTarget:self
action:@selector(dismiss:)
forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:dismissButton]; // 自动布局
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:dismissButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:.f
constant:.f]]; // 自动布局
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[dismissButton]-|"
options:
metrics:nil
views:NSDictionaryOfVariableBindings(dismissButton)]];
} - (void)dismiss:(id)sender
{
[self dismissViewControllerAnimated:YES
completion:nil];
} @end
ModelViewController.m
//
// PopAnimator.h
// Popping
//
// Created by André Schneider on 14.05.14.
// Copyright (c) 2014 André Schneider. All rights reserved.
// #import <Foundation/Foundation.h> @interface PresentingAnimator : NSObject <UIViewControllerAnimatedTransitioning> @end
PresentingAnimator.h
//
// PopAnimator.m
// Popping
//
// Created by André Schneider on 14.05.14.
// Copyright (c) 2014 André Schneider. All rights reserved.
// #import "PresentingAnimator.h"
#import "POP.h" @implementation PresentingAnimator // 转场动画时间
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.5f;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// 获取原始控制器的View
UIView *fromView = \
[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view; fromView.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
fromView.userInteractionEnabled = NO; // 获取推出控制器的View
UIView *toView = \
[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
toView.frame = \
CGRectMake(, ,
CGRectGetWidth(transitionContext.containerView.bounds) - .f,
CGRectGetHeight(transitionContext.containerView.bounds) - .f); // 设置toView的初始的位置
toView.center = \
CGPointMake(transitionContext.containerView.center.x,
-transitionContext.containerView.center.y); // 用来让背景变暗的动画效果
UIView *dimmingView = [[UIView alloc] initWithFrame:fromView.bounds];
dimmingView.backgroundColor = [UIColor blackColor];
dimmingView.layer.opacity = 0.0; // 添加的转场动画容器当中
[transitionContext.containerView addSubview:dimmingView];
[transitionContext.containerView addSubview:toView]; // 位置动画(Spring系列)
POPSpringAnimation *positionAnimation = \
[POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
positionAnimation.toValue = @(transitionContext.containerView.center.y);
positionAnimation.springBounciness = ;
[positionAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) { // 动画结束后记得设置这个参数
[transitionContext completeTransition:YES];
}]; // 缩放动画(Spring系列)
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.springBounciness = ;
scaleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(1.2, 1.4)]; // 透明度动画(基本动画系列)
POPBasicAnimation *opacityAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
opacityAnimation.toValue = @(0.7); // 添加动画
[toView.layer pop_addAnimation:positionAnimation
forKey:@"positionAnimation"]; [toView.layer pop_addAnimation:scaleAnimation
forKey:@"scaleAnimation"]; [dimmingView.layer pop_addAnimation:opacityAnimation
forKey:@"opacityAnimation"];
} @end
PresentingAnimator.m
//
// DismissingAnimator.h
// Popping
//
// Created by André Schneider on 16.05.14.
// Copyright (c) 2014 André Schneider. All rights reserved.
// #import <Foundation/Foundation.h> @interface DismissingAnimator : NSObject <UIViewControllerAnimatedTransitioning> @end
DismissingAnimator.h
//
// DismissingAnimator.m
// Popping
//
// Created by André Schneider on 16.05.14.
// Copyright (c) 2014 André Schneider. All rights reserved.
// #import "DismissingAnimator.h"
#import "POP.h" @implementation DismissingAnimator // 转场动画时间
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.5f;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
/*
- 在Dismiss动画中,transitionContext.containerView.subviews里面包含了
- 之前添加进去的View哦,这点很重要
*/ // 开启toVC的用户交互
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
toVC.view.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
toVC.view.userInteractionEnabled = YES; UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; // 找到刚刚的那个用于改变透明度的View
__block UIView *dimmingView;
[transitionContext.containerView.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
if (view.layer.opacity < .f) {
dimmingView = view;
*stop = YES;
}
}]; // 改变透明度动画(基本动画类型)
POPBasicAnimation *opacityAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
opacityAnimation.toValue = @(0.0); // 改变位置动画(基本类型)
POPBasicAnimation *offscreenAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY]; offscreenAnimation.toValue = @(-fromVC.view.layer.position.y);
[offscreenAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
// 通知动画结束
[transitionContext completeTransition:YES];
}]; // 执行动画
[fromVC.view.layer pop_addAnimation:offscreenAnimation forKey:@"offscreenAnimation"];
[dimmingView.layer pop_addAnimation:opacityAnimation forKey:@"opacityAnimation"];
} @end
DismissingAnimator.m
2:TableView动画
//
// RootTableViewController.h
// POPTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface RootTableViewController : UITableViewController @end
RootTableViewController.h
//
// RootTableViewController.m
// POPTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootTableViewController.h"
#import "PopTableViewCell.h" @interface RootTableViewController () @end @implementation RootTableViewController - (void)viewDidLoad
{
[super viewDidLoad];
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *resuseID = @"YouXianMing"; PopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuseID]; if (cell == nil)
{
cell = [[PopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:resuseID];
} cell.showTitle.text = @"YouXianMing"; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} @end
RootTableViewController.m
//
// PopTableViewCell.h
// POPTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface PopTableViewCell : UITableViewCell @property (nonatomic, strong) UILabel *showTitle; @end
PopTableViewCell.h
//
// PopTableViewCell.m
// POPTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "PopTableViewCell.h"
#import "POP.h" @implementation PopTableViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_showTitle = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[self addSubview:_showTitle]; _showTitle.textAlignment = NSTextAlignmentCenter;
_showTitle.font = [UIFont fontWithName:@"HelveticaNeue-Thin"
size:.f];
}
return self;
} - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated]; if (self.highlighted)
{
// 选中时动画
POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
scaleAnimation.duration = 0.1;
scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(0.85, 0.85)];
[_showTitle pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}
else
{
// 没有选中或者再次显示时的动画
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(, )];
scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(, )];
scaleAnimation.springBounciness = .f;
[_showTitle pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}
} @end
PopTableViewCell.m
这个地方才是动画效果的关键哦:).
3:高逼格按钮动画
//
// RootViewController.m
// ButtonAnimation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "CAShapeLayer+Circle.h"
#import "POP.h" @interface RootViewController ()<POPAnimationDelegate> @property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) CAShapeLayer *circleShape1;
@property (nonatomic, strong) CAShapeLayer *circleShape2; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 圆环1
_circleShape1 = [CAShapeLayer LayerWithCircleCenter:self.view.center
radius:.f
startAngle:DEGREES()
endAngle:DEGREES()
clockwise:YES
lineDashPattern:nil];
_circleShape1.lineWidth = .f;
_circleShape1.strokeColor = [UIColor cyanColor].CGColor;
_circleShape1.strokeEnd = .f;
[self.view.layer addSublayer:_circleShape1]; // 圆环2
_circleShape2 = [CAShapeLayer LayerWithCircleCenter:self.view.center
radius:.f
startAngle:DEGREES()
endAngle:DEGREES()
clockwise:NO
lineDashPattern:nil];
_circleShape2.lineWidth = .f;
_circleShape2.strokeColor = [UIColor magentaColor].CGColor;
_circleShape2.strokeEnd = .f;
[self.view.layer addSublayer:_circleShape2]; // 完整显示按住按钮后的动画效果
_button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
_button.layer.cornerRadius = .f;
_button.backgroundColor = [UIColor cyanColor];
_button.center = self.view.center;
[self.view addSubview:_button]; // 按住按钮后没有松手的动画
[_button addTarget:self
action:@selector(scaleToSmall)
forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter]; // 按住按钮松手后的动画
[_button addTarget:self
action:@selector(scaleAnimations)
forControlEvents:UIControlEventTouchUpInside]; // 按住按钮后拖拽出去的动画
[_button addTarget:self
action:@selector(scaleToDefault)
forControlEvents:UIControlEventTouchDragExit];
} - (void)scaleToSmall
{
// 变小尺寸
POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.7f, 0.7f)];
scaleAnimation.delegate = self; // 核心
[_button.layer pop_addAnimation:scaleAnimation forKey:nil]; // 颜色
POPSpringAnimation *backgroundColor = \
[POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];
backgroundColor.toValue = (id)[UIColor magentaColor].CGColor;
[_button.layer pop_addAnimation:backgroundColor forKey:@"magentaColor"];
} - (void)scaleAnimations
{
// 恢复尺寸
POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(.f, .f)];
scaleAnimation.delegate = self; // 核心
[_button.layer pop_addAnimation:scaleAnimation forKey:nil]; // 颜色
POPSpringAnimation *backgroundColor = \
[POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];
backgroundColor.toValue = (id)[UIColor cyanColor].CGColor;
[_button.layer pop_removeAnimationForKey:@"magentaColor"]; // 先移除再添加
[_button.layer pop_addAnimation:backgroundColor forKey:nil];
} - (void)scaleToDefault
{
// 恢复尺寸
POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(.f, .f)];
scaleAnimation.delegate = self; // 核心
[_button.layer pop_addAnimation:scaleAnimation forKey:nil]; // 颜色
POPSpringAnimation *backgroundColor = \
[POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];
backgroundColor.toValue = (id)[UIColor cyanColor].CGColor;
[_button.layer pop_removeAnimationForKey:@"magentaColor"]; // 先移除再添加
[_button.layer pop_addAnimation:backgroundColor forKey:nil];
} // 代理
- (void)pop_animationDidApply:(POPAnimation *)anim
{
NSValue *toValue = (NSValue *)[anim valueForKeyPath:@"currentValue"];
CGSize size = [toValue CGSizeValue]; _circleShape1.strokeEnd = \
(size.height - calculateConstant(, , , 0.7))/calculateSlope(, , , 0.7); _circleShape2.strokeEnd = \
(size.height - calculateConstant(, , , 0.7))/calculateSlope(, , , 0.7);
} CGFloat calculateSlope(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
{
return (y2 - y1) / (x2 - x1);
} CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
{
return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
} @end
几个非常核心的地方:
按钮的3个交互用状态一个都不能少哦:
需要实现动画的代理获取按钮的实时尺寸的信息,最为关键的一步哦:
计算一元一次方程:
POP动画[2]的更多相关文章
- iOS动画——弹窗动画(pop动画)
用pop动画简单实现弹窗的缩放和渐变,感觉这个动画常用,就写一下博客 pop动画是Facebook推出的动画引擎,请自行到GitHub上搜索下载拖拽导入xcode项目中. 更多pop动画使用和原理可网 ...
- 用POP动画引擎实现衰减动画(POPDecayAnimation)
效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @end @i ...
- POP动画引擎中Layer与CALayer的一点区别
POP动画引擎是facebook提供的一个开源框架, 可以实现很多的动画效果, 这里就不一一介绍啦, 有兴趣的童鞋请移步: https://github.com/facebook/pop 下面简单的讲 ...
- 用POP动画编写带富文本的自定义动画效果
用POP动画编写带富文本的自定义动画效果 [源码] https://github.com/YouXianMing/UI-Component-Collection [效果] [特点] * 支持富文本 * ...
- POP动画[1]
POP动画[1] pop动画是facebook扩展CoreAnimation的,使用及其方便:) 1:Spring系列的弹簧效果(两个动画kPOPLayerBounds与kPOPLayerCorner ...
- 用POP动画模拟真实秒钟摆动效果
用POP动画模拟真实秒钟摆动效果 静态图: 动画图: 此处用到了POP中的Spring系列动画,现提供源码如下: SecondClockView.h 与 SecondClockView.m // // ...
- POP动画[3]
POP动画[3] 这一节主要讲解POP动画的自定义动画属性. POP动画中有一个参数,叫timingFunction,与CoreAnimation中的一个参数CAMediaTimingFunction ...
- 自定义UINavigationController push和pop动画
http://segmentfault.com/q/1010000000143983 默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界 ...
- 转 脸书pop动画的五个步骤
http://blog.csdn.net/u013741809/article/details/38511741 5 Steps For Using Facebook Pop // 1. Pick ...
随机推荐
- [问题解决]Fresco设置圆角效果不生效问题探究
[问题解决]Fresco设置圆角效果不生效问题探究 /** * Created by diql on 2017/2/21 11:07:04. */ 问题 在View中设置: fresco:rounde ...
- java突破------一撸到底(做Java开发,遇到瓶颈是保持现状还是寻求突破?)
java突破------一撸到底(做Java开发,遇到瓶颈是保持现状还是寻求突破?) 很多人做Java开发2.3年之后,都会觉得自己遇到了瓶颈.什么都会又什么都不会,如何改变困境,为什么很多人写了7. ...
- 如果天空不死博客java阅读列表整理
如果天空不死的主页https://home.cnblogs.com/u/skywang12345 下面是最近总结的Java集合(JDK1.6.0_45)相关文章的目录. 01. Java 集合系列01 ...
- vue中添加Echarts图表的使用,Echarts的学习笔记
项目中需要使用一些折线图.柱状图.饼状图等等,之前使用过heightCharts(关于heightCharts请看我的另一篇 http://www.cnblogs.com/jasonwang2y60/ ...
- Android studio应用导入源码错误This attribute must be localized
This attribute must be localized 产生原因: 多语言错误,源码中关于语言的显示不能直接赋值,而是需要通过xml来实现: 例如 <TextView android: ...
- android应用执行需要root权限的shell命令
导入jar包:http://blog.csdn.net/zhw1551706847/article/details/77709142 RootTools:http://blog.csdn.net/st ...
- Linux下如何查看版本信息(转)
Linux下如何查看版本信息, 包括位数.版本信息以及CPU内核信息.CPU具体型号等等,整个CPU信息一目了然. 1.# uname -a (Linux查看版本当前操作系统内核信息) L ...
- Angular 4+ 修仙之路
Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...
- pygame(一)
昨天,是有在树莓派上面操作pygame的,但是树莓派又上不了网了,很奇怪,我的306wifi显示的是树莓派连接成功,但是就是无法用网络,所以就下载不了图片,坐等HDMI线吧. 现在,看小甲鱼的视频已经 ...
- facebook 登录开发记录
1.注册一个 facebook 的账号 2.进入 facebook 开发者的网站.(也可以在 facebook 登录后,点击自己的名字进入用户信息页面,在该页面的底部有个“更多”的链接,点击进去会看到 ...