UIView 动画基本学习
2015-06-06 13:25
639 查看
UIView 可以设置很多动画, 淡入淡出,改变颜色等,只要把这些独立动画合并到一起,就会起到意想不到的效果
创建一个 UIview 添加一个UIButton 当点击button时执行动作。我添加了三个button做不同的动画效果。
前两个button只是普通操作,第三个button使用的是block动画方式, 这两种的区别是普通的动画要写更多的代码,block只是用一行代码实现功能,
但是block里面也可以写入很多设置代码,如设置时间,透明度,执行次数等。
以下就是代码
- (void)viewDidLoad {
[super
viewDidLoad];
UIView *view = [[UIView
alloc] initWithFrame:CGRectMake(40,
40, 100,
100)];
[view setBackgroundColor:[UIColor
greenColor]];
[view
setTag:1001];
[self.view
addSubview:view];
//第一个button
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button.frame =
CGRectMake(10,
400, 80,
40);
[button setTitle:@"Do Ani"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(doAnimations:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
//第二个button
UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button1.frame =
CGRectMake(100,
400, 80,
40);
[button1 setTitle:@"Do Back"
forState:UIControlStateNormal];
[button1 addTarget:self
action:@selector(doTransition:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button1];
//第三个button
UIButton *button2 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button2.frame =
CGRectMake(200,
400, 80,
40);
[button2 setTitle:@"Blk ani"
forState:UIControlStateNormal];
[button2 addTarget:self
action:@selector(doBlockAnimation:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button2];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)doAnimations:(UIButton *) button
{
//通过tag值取得当前的view
UIView *view = [self.view viewWithTag:1001];
//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间
[UIView beginAnimations:nil context:NULL];
//动画过程
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationRepeatCount:2]; //动画执行次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果
枚举类型,可以通过API修改其他值
[UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复
view.frame = CGRectMake(60, 200, 170, 110);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor redColor]];
//改变颜色
[UIView commitAnimations];
}
- (void)doAnimations:(UIButton *) button
{
//通过tag值取得当前的view
UIView *view = [self.view viewWithTag:1001];
//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间
[UIView beginAnimations:nil context:NULL];
//动画过程
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationRepeatCount:2]; //动画执行次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果
枚举类型,可以通过API修改其他值
[UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复
view.frame = CGRectMake(60, 200, 170, 110);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor redColor]];
//改变颜色
[UIView commitAnimations];
}
- (void)doTransition:(UIButton *) button
{
UIView *view = [self.view viewWithTag:1001];
// [UIView set]
[UIView beginAnimations:nil context:NULL];
//加入翻页效果,也是枚举类型 可以查API, cache的就是说要不要缓存,为了提高运行速度,一般选 YES
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];
[UIView setAnimationDuration:1.5];
[UIView setAnimationRepeatCount:2];
//自动恢复到位置
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.frame = CGRectMake(40, 70, 200, 150);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor yellowColor]];
[UIView commitAnimations];
}
//通过block进行动画操作, block函数也有几个,只使用其中一个
- (void)doBlockAnimation:(UIButton *)button
{
UIView *view = [self.view
viewWithTag:1001];
// [UIView set]
[UIView
beginAnimations:nil
context:NULL];
//第一个参数执行次数, 第二个参数就是动作, 第三个结束后执行什么动作,可以是删除,改变颜色等等
[UIView
animateWithDuration:2
animations:^{
[UIView
setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:view cache:YES];
[UIView
setAnimationDuration:5];
[UIView
setAnimationRepeatCount:4];
//自动恢复到位置
[UIView
setAnimationRepeatAutoreverses:YES];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.frame =
CGRectMake(40,
70, 200,
150);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor
yellowColor]];
}
completion:^(BOOL finished) {
// [view removeFromSuperview];
}];
[UIView
commitAnimations];
}
- (void)doAnimations:(UIButton *) button
{
//通过tag值取得当前的view
UIView *view = [self.view viewWithTag:1001];
//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间
[UIView beginAnimations:nil context:NULL];
//动画过程
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationRepeatCount:2]; //动画执行次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果
枚举类型,可以通过API修改其他值
[UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复
view.frame = CGRectMake(60, 200, 170, 110);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor redColor]];
//改变颜色
[UIView commitAnimations];
}
- (void)doTransition:(UIButton *) button
{
UIView *view = [self.view viewWithTag:1001];
// [UIView set]
[UIView beginAnimations:nil context:NULL];
//
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];
[UIView setAnimationDuration:1.5];
[UIView setAnimationRepeatCount:2];
//自动恢复到位置
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.frame = CGRectMake(40, 70, 200, 150);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor yellowColor]];
[UIView commitAnimations];
}
创建一个 UIview 添加一个UIButton 当点击button时执行动作。我添加了三个button做不同的动画效果。
前两个button只是普通操作,第三个button使用的是block动画方式, 这两种的区别是普通的动画要写更多的代码,block只是用一行代码实现功能,
但是block里面也可以写入很多设置代码,如设置时间,透明度,执行次数等。
以下就是代码
- (void)viewDidLoad {
[super
viewDidLoad];
UIView *view = [[UIView
alloc] initWithFrame:CGRectMake(40,
40, 100,
100)];
[view setBackgroundColor:[UIColor
greenColor]];
[view
setTag:1001];
[self.view
addSubview:view];
//第一个button
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button.frame =
CGRectMake(10,
400, 80,
40);
[button setTitle:@"Do Ani"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(doAnimations:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
//第二个button
UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button1.frame =
CGRectMake(100,
400, 80,
40);
[button1 setTitle:@"Do Back"
forState:UIControlStateNormal];
[button1 addTarget:self
action:@selector(doTransition:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button1];
//第三个button
UIButton *button2 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button2.frame =
CGRectMake(200,
400, 80,
40);
[button2 setTitle:@"Blk ani"
forState:UIControlStateNormal];
[button2 addTarget:self
action:@selector(doBlockAnimation:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button2];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)doAnimations:(UIButton *) button
{
//通过tag值取得当前的view
UIView *view = [self.view viewWithTag:1001];
//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间
[UIView beginAnimations:nil context:NULL];
//动画过程
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationRepeatCount:2]; //动画执行次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果
枚举类型,可以通过API修改其他值
[UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复
view.frame = CGRectMake(60, 200, 170, 110);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor redColor]];
//改变颜色
[UIView commitAnimations];
}
- (void)doAnimations:(UIButton *) button
{
//通过tag值取得当前的view
UIView *view = [self.view viewWithTag:1001];
//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间
[UIView beginAnimations:nil context:NULL];
//动画过程
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationRepeatCount:2]; //动画执行次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果
枚举类型,可以通过API修改其他值
[UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复
view.frame = CGRectMake(60, 200, 170, 110);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor redColor]];
//改变颜色
[UIView commitAnimations];
}
- (void)doTransition:(UIButton *) button
{
UIView *view = [self.view viewWithTag:1001];
// [UIView set]
[UIView beginAnimations:nil context:NULL];
//加入翻页效果,也是枚举类型 可以查API, cache的就是说要不要缓存,为了提高运行速度,一般选 YES
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];
[UIView setAnimationDuration:1.5];
[UIView setAnimationRepeatCount:2];
//自动恢复到位置
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.frame = CGRectMake(40, 70, 200, 150);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor yellowColor]];
[UIView commitAnimations];
}
//通过block进行动画操作, block函数也有几个,只使用其中一个
- (void)doBlockAnimation:(UIButton *)button
{
UIView *view = [self.view
viewWithTag:1001];
// [UIView set]
[UIView
beginAnimations:nil
context:NULL];
//第一个参数执行次数, 第二个参数就是动作, 第三个结束后执行什么动作,可以是删除,改变颜色等等
[UIView
animateWithDuration:2
animations:^{
[UIView
setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:view cache:YES];
[UIView
setAnimationDuration:5];
[UIView
setAnimationRepeatCount:4];
//自动恢复到位置
[UIView
setAnimationRepeatAutoreverses:YES];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.frame =
CGRectMake(40,
70, 200,
150);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor
yellowColor]];
}
completion:^(BOOL finished) {
// [view removeFromSuperview];
}];
[UIView
commitAnimations];
}
- (void)doAnimations:(UIButton *) button
{
//通过tag值取得当前的view
UIView *view = [self.view viewWithTag:1001];
//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间
[UIView beginAnimations:nil context:NULL];
//动画过程
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationRepeatCount:2]; //动画执行次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果
枚举类型,可以通过API修改其他值
[UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复
view.frame = CGRectMake(60, 200, 170, 110);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor redColor]];
//改变颜色
[UIView commitAnimations];
}
- (void)doTransition:(UIButton *) button
{
UIView *view = [self.view viewWithTag:1001];
// [UIView set]
[UIView beginAnimations:nil context:NULL];
//
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];
[UIView setAnimationDuration:1.5];
[UIView setAnimationRepeatCount:2];
//自动恢复到位置
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.frame = CGRectMake(40, 70, 200, 150);
//view.alpha = 0.0;
[view setBackgroundColor:[UIColor yellowColor]];
[UIView commitAnimations];
}
相关文章推荐
- Gifski:一个跨平台的高质量 GIF 编码器
- 模仿动画的放大缩小容器
- php判断GIF图片是否为动画的方法
- js排序动画模拟-插入排序
- jQuery动画特效实例教程
- JQuery动画和停止动画实例代码
- JQuery动画与特效实例分析
- Jquery 自定义动画概述及示例
- 基于jquery的动画效果代码
- 锋利的jQuery 要点归纳(三) jQuery中的事件和动画(上:事件篇)
- 基于Jquery的温度计动画效果
- javascript与css3动画结合使用小结
- js运动动画的八个知识点
- Android开场动画类完整实现代码
- Android开发之动画实现方法
- android判断动画已结束示例代码
- Android开发之图形图像与动画(五)LayoutAnimationController详解
- VC++实现输出GIF到窗体并显示GIF动画的方法
- VC实现图片拖拽及动画的实例
- JS运动框架之分享侧边栏动画实例