iOS TranslationZoom(平移 缩放 弹性)~demo
//联系人:石虎 QQ: 1224614774昵称:嗡嘛呢叭咪哄
/**
注意点: 1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
三、实现代码:
=========================
===================================================
==========================控制器1: ViewController.m
//
// ViewController.m
// TranslationZoom(平移缩放弹性)~demo
//
// Created by 石虎 on 2017/8/19.
// Copyright © 2017年 shihu. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UIImageView *testView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//创建移动视图
_testView=[[UIImageViewalloc] init];
_testView.frame=CGRectMake(0,200, 100,100);
_testView.backgroundColor=[UIColororangeColor];
[self.viewaddSubview:_testView];
//创建功能按钮
NSArray *array=@[@"缩放",@"弹性",@"平移"];
for (int i =0; i<array.count; i++) {
UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(10+60*i,500, 50,40);
btn.tag=i+1;
btn.backgroundColor=[UIColorredColor];
[btn setTitle:array[i]forState:UIControlStateNormal];
[btn setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(startAnimation:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
}
#pragma mark - 缩放弹性平移功能回调
-(void)startAnimation:(UIButton *)btn{
_testView.transform=CGAffineTransformIdentity;
switch (btn.tag) {
case1://缩放
{
[UIViewanimateWithDuration:0delay:0options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[_testView.layersetValue:@(0.1)forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.23delay:0options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[_testView.layersetValue:@(1.2)forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.09delay:0.02options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[_testView.layersetValue:@(.9)forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.05delay:0.02options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[_testView.layersetValue:@(1.0)forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
}];
}];
}];
}];
[UIViewanimateWithDuration:.3delay:0options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[btn.layersetValue:@(1.3)forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:.3delay:0options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[btn.layersetValue:@(1.0)forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
}];
}];
}
break;
case2://弹性
{
//Damping 弹性,0-1,越小弹性越大
//Velocity 弹性复位速度
[_testView.layersetValue:@(0)forKeyPath:@"transform.translation.x"];
[UIViewanimateWithDuration:.6delay:0usingSpringWithDamping:.3initialSpringVelocity:.5options:UIViewAnimationOptionCurveEaseInOutanimations:^{
[_testView.layersetValue:@(150)forKeyPath:@"transform.translation.x"];
} completion:^(BOOL finished) {
}];
}
break;
case3://平移
{
[_testView.layersetValue:@(50)forKeyPath:@"position.x"];
[UIViewanimateWithDuration:1.3animations:^{
[_testView.layersetValue:@(150)forKeyPath:@"position.x"];
} completion:^(BOOL finished) {
}];
}
break;
default:
break;
}
}
@end
================
=======