在ViewController.m中声明
@interface ViewController ()
@property (nonatomic, strong) UIView *myView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UIView层的动画分为: 代码块(begin...commit) , block块(常用)
self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//第一种(比较常用,)
// 这里要写上(防止循环引用) 这个也可以__weak ViewController *myself = self;
__weak typeof(self) pSelf = self;
//第一个参数animateWithDuration:代表的是动画的执行时间
//block块里面代表的是执行的动画
[UIView animateWithDuration:2.0f animations:^{
UIViewAnimation
//设置frame有动画 点击屏幕后,宽高会发生变化
pSelf.myView.frame = CGRectMake(100, 100, 200, 200);
//改变bounds的时候也可以执行动画,但是是以试图的center为中心像两边扩充
// pSelf.myView.bounds = CGRectMake(0, 0, 200, 200);
//设置视图的透明度
pSelf.myView.alpha = 0.1;
//中心
//pSelf.myView.center = pSelf.view.center;
//设置边框
//pSelf.myView.layer.borderWidth = 10;
}];
******************************************第二种(在第一种的基础之上添加了一个参数)*********************************************************
//第一个参数:代表的时间
//第二个参数:block代表执行的动画
//第三个参数:block代表动画完成
[UIView animateWithDuration:1.0f animations:^{
pSelf.myView.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) {
//这里的代码不会当作动画执行
pSelf.myView.backgroundColor = [UIColor redColor];
}];
//************************************第三种(添加一个延迟) **********************************************
//第一个参数:代表的执行时间
//第二个参数delay:代表的是延迟多久执行
//第三个参数:代表的是动画的一些特效(此处填的是重复执行)
//点击鼠标后开始执行的东西
[UIView animateWithDuration:2.0f delay:1.0f options: UIViewAnimationOptionRepeat animations:^{
pSelf.myView.center = pSelf.view.center;
pSelf.myView.backgroundColor = [UIColor greenColor];
//鼠标屏幕后,接下来要执行的东西
} completion:^(BOOL finished) {
pSelf.myView.backgroundColor = [UIColor yellowColor];
}];
//**********************************第四个******************************************************************
//usingSpringWithDamping :0~1 ,值越小,越明显 ,代表的是阻尼系数,值越大,效果越小
//initialSpringVelocity:代表的是初始速度
//点击鼠标后开始执行的东西
[UIView animateWithDuration:2.0f delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:50 options:UIViewAnimationOptionRepeat animations:^{
pSelf.myView.frame = CGRectMake(100, 300, 200, 100);
//鼠标屏幕后,接下来要执行的东西
} completion:^(BOOL finished) {
pSelf.myView.backgroundColor = [UIColor yellowColor];
}];
//********代码块
//第一个参数:代表的是动画的名字
//第二个参数:代表的是上下文相关的,目前没用,以后不确定
[UIView beginAnimations:nil context:nil];
//从当前状态开始
[UIView setAnimationBeginsFromCurrentState:YES];
//设置动画在开始和结束的时候的一些特效
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置动画的持续时间
[UIView setAnimationDuration:3.0f];
//设置动画的延迟时间
//[UIView setAnimationDelay:10.0f];
//设置动画的重复次数
[UIView setAnimationRepeatCount:1000];
//设置动画的代理人
[UIView setAnimationDelegate:self];
//设置动画将要开始,会执行的SEL方法
//[UIView setAnimationWillStartSelector:@selector(<#selector#>)];
//设置动画将要结束,会执行的SEL方法
//UIView setAnimationDidStopSelector:@selector(<#selector#>);
self.myView.backgroundColor = [UIColor greenColor];
self.myView.frame = CGRectMake(100, 100, 200, 200);
//提交动画,才会执行动画
[UIView commitAnimations];
//*****************UIView切换的动画(不常用)
//第一个参数:要变没的视图
//第二个参数:要变出的视图
//第三个参数:变出的动画动作
[UIView transitionFromView:<#(UIView *)#> toView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> completion:<#^(BOOL finished)completion#>];
//比较常用的
//可以配合着切换tableview与collectionView
[UIView transitionWithView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]
}