关于 UIView 的动画实现:
类方法调用UIView实现动画,每个动画块里设一个动作的顺序: begin方法->set方法->控件属性->commit方法 动画的实现有一定的顺序要求,否则影响动画的动作。以一个按钮来控制view的动作可参考以下代码:
-(void)didclick
{
[UIView beginAnimations:@"start" context:@"xxxx"]; //1
// begin动画
[UIView setAnimationDuration:2]; //2
// 动画耗时
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//
[UIView setAnimationDelay:3];
// 动画延时
[UIView setAnimationRepeatCount:INFINITY];
// 动画重复次数: 无限次数
[UIView setAnimationRepeatAutoreverses:YES];
// 动画自动返回
_square.bounds = CGRectMake(0, 0, 50, 50); //3
_square.center = CGPointMake(200, 200);
//双动作,view大小转变,view的位置转变
_square.backgroundColor = [UIColor redColor];
//view的背景颜色改变
[UIView commitAnimations]; //4
// commit动画
}
- (IBAction)aa:(id)sender {
[UIView beginAnimations:@"abc" context:NULL];
//begin动画
[UIView setAnimationDuration:3];
//动画耗时
[UIView setAnimationBeginsFromCurrentState:YES];
//动画从当前位置开始,不用在按下按键以后到目标位置再移动,可以立马变动位置
_quer.bounds = CGRectMake(0, 0, 50, 50);
_quer.center = CGPointMake(200, 0);
//双动作,view大小转变,view的位置转变
[UIView commitAnimations];
//cimmit动画
}
还有其他协议属性。
使用block方法实现view动作:view动作和view动画
view动作:
_purpleView.transform = CGAffineTransformScale(_purpleView.transform, 1, 1.1);
//scale比例的意思,长度不变 1:1,宽度增加 1:1.1
_purpleView.transform = CGAffineTransformRotate(_purpleView.transform, M_PI_4);
//Rotate顺时针旋转,M_PI_4,旋转45度
_purpleView.transform = CGAffineTransformTranslate(_purpleView.transform,100,0);
//translate移动,(100,50),100>0,向左移动,50>0,向下移动
block的view动画;
[UIView animateWithDuration:(NSTimeInterval) animations:^(void)animations ];
//这种是最简单的一类block动画设置方法,只有“Duration”和“animations”两个动作
[UIView animateWithDuration:(NSTimeInterval) animations:(void(^)(void))animations completion:(void(^)(BOOL finished))completion];
//这种比上一种在有“Duration”和“animations”动作之外,加了一个“completion”动作。
[UIView animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^)(BOOL finished))completion];
//以上三种,属一类。包括动作“总耗时 Duration”、“延时 delay”、“选项 option”、“动作 animations”、“完成 completion” 5个动作
//animations:^{
_purpleView.center = CGPointMake(300, 300);
//将view的中心移动到(100,300)的位置
_purpleView.backgroundColor = [UIColor greenColor];
//将view的背景设置成绿色
}
//completion:^(BOOL finished) {
}
//options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
[UIView transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^)(BOOL finished))completion];
//上面三个是平移动画,看了这个“transition”应该就知道了,意思是转“转变”,即 “画面转动动画”
有几个参数:“对象 view”、“总耗时 duration”、“选项 option”、“动作 animation”、“完成 completion”
//animations:^{
if (_redView.superview) {
[_redView removeFromSuperview];
}
else {
[_purpleView addSubview:_redView];
}
}
//completion:^(BOOL finished) {
}
[UIView transitionFromView:(UIView *)fromView ToView:(UIView *)ToView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void(^)(BOOL finished))completion];
//
view动画、block动画
最新推荐文章于 2022-01-13 16:41:00 发布