@interface RootViewController () {
UIView *animationView;
UIView *animationView1;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//动画分为:UIView动画和CAAnimation动画
animationView = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 100, 300)];
animationView.backgroundColor = [UIColor brownColor];
[self.view addSubview:animationView];
[animationView release];
animationView1 = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 300)];
animationView1.backgroundColor = [UIColor cyanColor];
[self.view addSubview:animationView1];
[animationView1 release];
// [self propertAnimation];
//作用在UIView上的动画叫UIView动画
//UIView动画分为 UIView属性动画 和 UIView过渡动画
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 64, 100, 100);
button.backgroundColor = [UIColor colorWithWhite:0.018 alpha:0.070];
[button setTitle:@"属性动画" forState:UIControlStateNormal];
button.tintColor = [UIColor blackColor];
[button addTarget:self action:@selector(propertAnimation) forControlEvents:(UIControlEventTouchUpInside)];
button.layer.borderWidth = 1;
button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.cornerRadius = 10;
[self.view addSubview:button];
//过渡
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(250, 64, 100, 100);
button1.backgroundColor = [UIColor colorWithWhite:0.018 alpha:0.070];
[button1 setTitle:@"过渡动画" forState:UIControlStateNormal];
button1.tintColor = [UIColor blackColor];
[button1 addTarget:self action:@selector(transitionAnimation) forControlEvents:(UIControlEventTouchUpInside)];
button1.layer.borderWidth = 1;
button1.layer.borderColor = [UIColor blackColor].CGColor;
button1.layer.cornerRadius = 10;
[self.view addSubview:button1];
//计时器,每隔多少秒执行一次某方法
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(propertAnimation) userInfo:nil repeats:YES];
}
//属性动画(在动画期间对其属性值做了改变)
- (void)propertAnimation {
/*
//写法1
//以beginAnimations开头,以commitAnimations结尾,中间修改的属性
//开始动画
[UIView beginAnimations:@"yangchao" context:nil];
//动画时间,默认0.2秒
[UIView setAnimationDuration:0];
//动画重复次数
// [UIView setAnimationRepeatCount:100];
//动画是否往返
// [UIView setAnimationRepeatAutoreverses:YES];
//动画延时执行(单位:秒)
[UIView setAnimationDelay:0];
//动画执行速率
[UIView setAnimationCurve:(UIViewAnimationCurveLinear)];
//设置动画是否可用,默认值是YES
[UIView setAnimationsEnabled:YES];
//修改属性值
//改颜色
animationView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.000];
//改位置
// animationView.center = CGPointMake(arc4random() % (325 - 50 + 1) + 50, arc4random() % (617 - 50 + 1) + 50);
//改大小
// animationView.bounds = CGRectMake(0, 0, arc4random() % 375, arc4random() % 667);
animationView.transform = CGAffineTransformRotate(animationView.transform, M_PI_4);
//提交动画
[UIView commitAnimations];
*/
//写法2(a)
// [UIView animateWithDuration:0.1 animations:^{
// //修改属性
// animationView.transform = CGAffineTransformRotate(animationView.transform, M_PI_4);
// }];
//写法2(b)
// [UIView animateWithDuration:0.1 animations:^{
// //修改属性
// animationView.transform = CGAffineTransformRotate(animationView.transform, M_PI_4);
// } completion:^(BOOL finished) {
// NSLog(@"当动画执行完");
// }];
//写法2(c)
// [UIView animateWithDuration:0.1 delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^{
// //修改属性
// animationView.transform = CGAffineTransformRotate(animationView.transform, M_PI_4);
// animationView.transform = CGAffineTransformScale(animationView.transform, 0.9, 1.1);
// } completion:^(BOOL finished) {
// NSLog(@"动画执行完");
// }];
}
//过渡动画,主要用于视图的切换
- (void)transitionAnimation {
//写法1:
/*
//开头
[UIView beginAnimations:@"过渡" context:nil];
[UIView setAnimationDuration:1];
//过渡改变属性
[UIView setAnimationTransition:(UIViewAnimationTransitionCurlUp) forView:animationView cache:YES];
//改颜色
animationView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.000];
//结尾
[UIView commitAnimations];
*/
//写法2:
/*
//a.
[UIView transitionWithView:animationView duration:1 options:(UIViewAnimationOptionTransitionFlipFromLeft) animations:^{
//修改属性
} completion:^(BOOL finished) {
//动画执行完后的操作
}];
*/
//b.
//FromView,动画执行结束后,从父视图上移除
//toView,动画结束后,添加到父视图
[UIView transitionFromView:animationView toView:animationView1 duration:1 options:(UIViewAnimationOptionTransitionCurlUp) completion:^(BOOL finished) {
//动画执行完后的操作
}];
//CAAmination动画:作用在CALayer上的动画
//UIView和CALayer的关系
//1.UIView视图自带一个属性layer
//2.一个视图由UIView和CALayer组成,UIView负责视图的交互,CALayer负责视图的绘制
//创建CAlayer,层, 继承于NSObject
/*
CALayer *layer = [[CALayer alloc] init];
//layer的大小,默认和UIView的frame保持一致
layer.frame = CGRectMake(0, 0, 200, 100);
//layer的颜色,设置UIView的背景色实际上是设置了UIView自带的layer的背景色
layer.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.100].CGColor;
//边框宽度
layer.borderWidth = 10;
//边框颜色
layer.borderColor = [UIColor colorWithWhite:0.000 alpha:0.180].CGColor;
//圆角
// layer.cornerRadius = 50;
//阴影颜色
layer.shadowColor = [UIColor blackColor].CGColor;
//阴影偏移
layer.shadowOffset = CGSizeMake(10, 10);
//阴影的不透明度
layer.shadowOpacity = 1.0;
//阴影的模糊程度,默认是3.0
layer.shadowRadius = 2.0;
//变形参数(旋转,平移,缩放)
// layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 1, 1);;//旋转
// layer.transform = CATransform3DMakeTranslation(0, 100, 100);//平移
// layer.transform = CATransform3DMakeScale(1, 1, 1);//缩放,默认为1
//锚点,默认值:(0.5, 0.5)<在层的中心>
//所有的旋转都是以锚点为中心
NSLog(@"%@", NSStringFromCGPoint(layer.anchorPoint));
//锚点在父视图坐标中的位置
NSLog(@"%@", NSStringFromCGPoint(layer.position));
//改变锚点的位置,不影响position,影响frame
layer.anchorPoint = CGPointMake(0, 0);
NSLog(@"%@", NSStringFromCGPoint(layer.position));
//添加到父视图
[self.view.layer addSublayer:layer];
[layer release];
*/
animationView = [[UIView alloc] initWithFrame:CGRectMake(140, 400, 100, 100)];
animationView.backgroundColor = [UIColor brownColor];
[self.view addSubview:animationView];
[animationView release];
// [self groupAnimation];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(groupAnimation) userInfo:nil repeats:YES];
//CALayer动画,是模拟动画
//CAAnimation,继承于NSObject,是一个抽象动画类,不能直接使用,需要通过子类使用
//CAAnimation子类
//a.CAPropertyAnimation,属性动画,继承于CAAnimation,是一个抽象动画子类,
//1.CABasicAnimation,基本动画,继承于CAPropertyAnimation
//2.CAKeyframeAnimation,关键帧动画,继承于CAPropertyAnimation
//b.CATransition,过渡动画,继承于CAAnimation
//c.CAAnimationGroup,组动画,继承于CAAnimation
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(170, 600, 50, 50);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"变" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:30];
[button addTarget:self action:@selector(transitionAnimation) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 335, 40)];
textField.placeholder = @"请输入用户名";
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
[textField release];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(20, 160, 335, 40);
[button1 setTitle:@"登录" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(login) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
}
//关键帧动画
- (void)login {
if (textField.text.length > 0) {
NSLog(@"输入正确");
}else {
// UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名为空" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
// [alertView show];
// [alertView release];
/*
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyframe.values =
@[
[NSValue valueWithCGPoint:CGPointMake(375 / 2 + 20, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 - 20, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 + 15, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 - 15, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 + 10, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 + 10, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 + 5, 120)],
[NSValue valueWithCGPoint:CGPointMake(375 / 2 - 5, 120)],
];
[textField.layer addAnimation:keyframe forKey:@"keyframe"];
*/
CAKeyframeAnimation *keyframe1 = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
keyframe1.values =
@[@(375 / 2 + 20),
@(375 / 2 - 20),
@(375 / 2 + 15),
@(375 / 2 - 15),
@(375 / 2 + 10),
@(375 / 2 + 10),
@(375 / 2 + 5),
@(375 / 2 - 5)
];
[textField.layer addAnimation:keyframe1 forKey:@"keyframe1"];
}
}
//基本动画
- (void)basicAnimation {
//创建方式
//KeyPath内写要对layer的哪些属性值进行修改
//注:并不是所有的layer的属性都能做动画,看属性的注释中是否有Animatable关键字
/*
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
//动画时长
basicAnimation.duration = 2;
basicAnimation.toValue = (id)[UIColor redColor].CGColor;
[animationView.layer addAnimation:basicAnimation forKey:@"base"];
*/
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
basicAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)];
[animationView.layer addAnimation:basicAnimation forKey:@"base"];
}
//关键帧动画
- (void)keyFrameAnimation {
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
keyFrameAnimation.duration = 10;
keyFrameAnimation.values =
@[
(id)[UIColor redColor].CGColor,
(id)[UIColor blueColor].CGColor,
(id)[UIColor cyanColor].CGColor,
(id)[UIColor magentaColor].CGColor,
(id)[UIColor orangeColor].CGColor,
(id)[UIColor greenColor].CGColor
];
//keyTimes数组的值,取值范围(0-1),必须是升序
keyFrameAnimation.keyTimes = @[@0.1, @0.5, @0.6, @0.8, @0.9];
[animationView.layer addAnimation:keyFrameAnimation forKey:@"key"];
}
//过渡动画
- (void)transitionAnimation {
CATransition *transition = [CATransition animation];
//动画类型
//kCATransitionFade ;淡隐淡出
//kCATransitionMoveIn ;推开
//kCATransitionPush ;移入
//kCATransitionReveal;移出
//suckEffect(三角)
//rippleEffect(水波抖动)
//pageCurl(上翻页)
//pageUnCurl(下翻页)
//oglFlip(上下翻转)
//cameraIris/cameraIrisHollowOpen/cameraIrisHollowClose (镜头快门,这一组动画是有效果,只是很难看,不建议使用
transition.type = @"rippleEffect";
//动画类型
transition.subtype = kCATransitionFromTop;
//动画时长
transition.duration = 2;
[self.view.layer addAnimation:transition forKey:nil];
}
//组动画
- (void)groupAnimation {
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
//动画时长
basicAnimation.duration = 2;
basicAnimation.toValue = (id)[UIColor redColor].CGColor;
CABasicAnimation *basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"bounds"];
basicAnimation1.duration = 2;
basicAnimation1.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)];
CABasicAnimation *basicAnimation2 = [CABasicAnimation animationWithKeyPath:@"center"];
basicAnimation2.duration = 2;
basicAnimation2.fromValue = [NSValue valueWithCGPoint:CGPointMake(arc4random() % (302 - 73 + 1) + 73, arc4random() % (594 - 73 + 1) + 73)];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 2;
group.animations = @[basicAnimation,basicAnimation1, basicAnimation2];
[animationView.layer addAnimation:group forKey:nil];
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end