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 ...
随机推荐
- 利用IDEA创建Web Service服务端和客户端的详细过程
创建服务端 一.file–>new–>project 二.点击next后输入服务端名,点击finish,生成目录如下 三.在 HelloWorld.Java 文件中右击,选 WebServ ...
- Modular Rails: The complete Guide to Modular Rails Applications 笔记
fix SamuraiCRM/engines/core/test/dummy/config/routes 修改如下 Rails.application.routes.draw do mount Sam ...
- logstash 启动报找不主类或无法加载 java
logstash 启动报无法找到主类解决方案 Zparkle 关注 2018.03.08 22:04* 字数 2051 阅读 1评论 0喜欢 0 当logstash启动时,首先要注意正确配置java ...
- 一头扎进Spring之---------Spring七大核心模块
Spring七大核心模块 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanF ...
- 腾讯云技术专家卢萌凯手把手教你Demo一个人脸识别程序!
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自腾讯云技术沙龙,本次沙龙主题为Serverless架构开发与SCF部署实践 卢萌凯:毕业于东南大学,曾就职于华为,熟悉云行业解决方案 ...
- JavaScript中有对字符串编码的三个函数:escape,encodeURI,encodeURIComponent
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...
- 网页title添加图标
<link rel="shortcut icon" href="1.ico"> href="图片名字.ico"; 图片后缀名为: ...
- Mybatis初始
1.Mybatis 的作用 完成基本的sql语句 和 存储过程 高级的对象关系映射(ORM) 框架 封装了几乎所有的 JDBC 代码 参数的手工设置 结果集的遍历 2.Mybatis 框架的主体构成 ...
- MVC中学到的小知识(MVC中的跳转,传参)
1.mvc中视图中的href="XXX",这个XXX是控制器地址,不是另一个视图.(这里的href语句只能转向控制器,不能直接转向视图),如果要实现转向视图,可以先转到控制器,然后 ...
- 撩课-Java每天10道面试题第7天
撩课Java+系统架构 视频 点击开始学习 61.什么是并发修改异常? 什么是并发修改异常: 当我们在遍历实现了collection接口 与iterator接口的集合时(List.Set.Map), ...