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]的更多相关文章

  1. iOS动画——弹窗动画(pop动画)

    用pop动画简单实现弹窗的缩放和渐变,感觉这个动画常用,就写一下博客 pop动画是Facebook推出的动画引擎,请自行到GitHub上搜索下载拖拽导入xcode项目中. 更多pop动画使用和原理可网 ...

  2. 用POP动画引擎实现衰减动画(POPDecayAnimation)

    效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @end @i ...

  3. POP动画引擎中Layer与CALayer的一点区别

    POP动画引擎是facebook提供的一个开源框架, 可以实现很多的动画效果, 这里就不一一介绍啦, 有兴趣的童鞋请移步: https://github.com/facebook/pop 下面简单的讲 ...

  4. 用POP动画编写带富文本的自定义动画效果

    用POP动画编写带富文本的自定义动画效果 [源码] https://github.com/YouXianMing/UI-Component-Collection [效果] [特点] * 支持富文本 * ...

  5. POP动画[1]

    POP动画[1] pop动画是facebook扩展CoreAnimation的,使用及其方便:) 1:Spring系列的弹簧效果(两个动画kPOPLayerBounds与kPOPLayerCorner ...

  6. 用POP动画模拟真实秒钟摆动效果

    用POP动画模拟真实秒钟摆动效果 静态图: 动画图: 此处用到了POP中的Spring系列动画,现提供源码如下: SecondClockView.h 与 SecondClockView.m // // ...

  7. POP动画[3]

    POP动画[3] 这一节主要讲解POP动画的自定义动画属性. POP动画中有一个参数,叫timingFunction,与CoreAnimation中的一个参数CAMediaTimingFunction ...

  8. 自定义UINavigationController push和pop动画

    http://segmentfault.com/q/1010000000143983 默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界 ...

  9. 转 脸书pop动画的五个步骤

    http://blog.csdn.net/u013741809/article/details/38511741 5 Steps For Using Facebook Pop   // 1. Pick ...

随机推荐

  1. [问题解决]gradle编译失败系统找不到指定的文件

    [问题解决]gradle编译失败系统找不到指定的文件 问题描述 Error:C:\Users\diql.gradle\caches\2.14.1\scripts-remapped\settings_9 ...

  2. 群晖MyDS账号注册--实现使用QuickConnect外网访问

    最近公司拿了个NAS给我,让我把它配置好,之前没有接触过这个东西,上网一查,发现就是和去年很火的玩客云和斐讯天天链N1的功能一样,可以实现文件储存和文件共享. 设备型号:群晖DS214SE 系统版本: ...

  3. JAVA 方法 和Scanner

    方法:包含于类或对象中,是解决一类问题步骤的有序组合. 算法:解决一类问题的思想. Scanner next()与nextLine()区别 next(): 1.一定要读取到有效字符后才可以结束输入. ...

  4. SDSM框架

    标题解释 SDSM指的是SpringMVC+Dubbo+Spring+Mybatis的框架 ------------------------------------------------------ ...

  5. jquery里正则的使用方法及常用的正则验证

    本文是一篇关于jquery使用正则来验证输入,及一些常用验证规则的基础文章,适合新手. 假设我们的网页里有这样的一个表单: <input id="aijquery" type ...

  6. unity assetStore 常用插件

    常用插件 20180723============= 教程类 =============<Mecanim Example Scenes > 官方示例场景<Surivial Shoot ...

  7. 一、hbase单机安装

    下文将快速构建并启动单节点hbase,不使用hdfs作为存储,不使用独立的zookeeper hbase官网:http://hbase.apache.org/ 一.JDK环境 hbase需要JDK环境 ...

  8. 集合框架以及Map(一)

    集合又称容器,编程思想中对其的定义为持有对象 我们在使用集合或者数组时得到最多的异常就是数组下表越界异常 Java.lang.ArrayIndexOutOfBoundsException这篇文章我们不 ...

  9. 使用Tensorflow和MNIST识别自己手写的数字

    #!/usr/bin/env python3 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data ...

  10. 【SSH网上商城项目实战17】购物车基本功能的实现

    转自:https://blog.csdn.net/eson_15/article/details/51418350 上一节我们将商品的详细页面做完了,并使用了Hibernate的二级缓存加载详细页面来 ...