#pragma mark --- UIVIew动画
1.开始动画
[UIView beginAnimation:@“animation”context
:@“hahah"];
2.——之间写你要执行的动画 (setAnimation….)
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:1];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeat];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];
self.oldFrame = self.myView.frame;
self.oldColor = self.myView.backgroundColor;
self.oldAplha = self.myView.alpha;
self.myView.frame = CGRectMake(0, 0, 100, 100);
self.myView.backgroundColor = [UIColor redColor];
self.myView.alpha = 0.2;
[UIView commitAnimations];
#pragma mark --- UIVIew 的block动画
[UIView animateWithDuration:1 animations:^{
pragma mark --- 平移
self.myView.transform = CGAffineTransformTranslate(self.myView.transform, 100, 100);
} completion:^(BOOL finished) {
[UIView animateWithDuration:2 animations:^{
self.myView.transform = CGAffineTransformTranslate(self.myView.transform, -100, -100);
}completion:^(BOOL finished) {
#pragma mark --- 缩放
[UIView animateWithDuration:1 animations:^{
self.myView.transform = CGAffineTransformScale(self.myView.transform, 2, 2.5);
}];
}];
}];
#pragma mark --- 旋转
[UIView animationWithDuration:0.001 animation:^{
self.myView.transform = CGAffineTransformRotate(self.myView.transform, M_PI_4 / 2);
}completion:^(BOOL finished) {
[self MyViewRotate];
}];
--------------------------------------------------------------------------------------
#pragma mark --- 两个View之间的跳转:动画的翻页,左转,右转,上翻,下翻等效果
- (void)click:(UIBarButtonItem *)item
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
[UIView transitionFromView:self.view toView:secondVC.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
[self.navigationController pushViewController:secondVC animated:NO];
}
- (void)click:(UIBarButtonItem *)item
{
RootViewController *rootVC = self.navigationController.viewControllers[0];
[UIView transitionFromView:self.view toView:rootVC.view duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
--------------------------------------------------------------------------------------
#pragma mark --- CALayer
/*
视图的构成 分两部分
最底下是UIView ,UIView的上面有一个Layer层
layer层是负责渲染视图
UIView负责把渲染完成的视图,展示出来
UIVIew好比画布,layer好比画布上的画
*/
self.imageV.layer.cornerRadius = self.imageV.frame.size.width/2;
self.imageV.layer.shadowColor = [UIColor blackColor].CGColor;
self.imageV.layer.shadowOffset = CGSizeMake(0,0);
self.imageV.layer.shadowRadius = 50;
self.imageV.layer.shadowOpacity = 1;
self.imageV.layer.borderColor = [UIColor magentaColor].CGColor;
self.imageV.layer.borderWidth = 2;
/*
1.选取合适的类去创建动画 (修改1个值就用基本动画,修改一组值就用轨迹动画)
2.创建动画 并且设置要修改的值
3.添加到要展示动画的视图的layer层上
*/
#pragma mark ---- 旋转
- (void)buttonClick1:(UIButton *)button
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.toValue = [NSNumber numberWithFloat:M_PI];
animation.duration = 2;
animation.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:animation forKey:@"transform.rotation.x"];
}
#pragma mark ---- 改视图的大小
- (void)buttonClick2:(UIButton *)button
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(0, 0)];
animation.toValue = [NSValue valueWithCGSize:CGSizeMake(200, 200)];
animation.duration = 1;
animation.repeatCount = 1;
[self.imageV.layer addAnimation:animation forKey:@"bounds.size"];
}
#pragma mark ---- 改变一组背景颜色
- (void)buttonClick3:(UIButton *)button
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
CGColorRef red = [UIColor redColor].CGColor;
CGColorRef black = [UIColor blackColor].CGColor;
CGColorRef orange = [UIColor orangeColor].CGColor;
animation.values = @[(id)red,(id)black,(id)orange];
animation.duration = 2;
animation.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:animation forKey:@"backgroundColor"];
}
#pragma mark ---- 按轨迹移动 调整位置
- (void)buttonClick4:(UIButton *)button
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSLog(@"%@",NSStringFromCGPoint(self.imageV.layer.position));
CGPoint p1 = CGPointMake(100, 0);
CGPoint p2 = CGPointMake(100, 300);
CGPoint p3 = CGPointMake(300, 300);
CGPoint p4 = CGPointMake(300, 100);
CGPoint p5 = CGPointMake(200, 100);
CGPoint p6 = CGPointMake(200, 200);
NSValue *v1 = [NSValue valueWithCGPoint:p1];
NSValue *v2 = [NSValue valueWithCGPoint:p2];
NSValue *v3 = [NSValue valueWithCGPoint:p3];
NSValue *v4 = [NSValue valueWithCGPoint:p4];
NSValue *v5 = [NSValue valueWithCGPoint:p5];
NSValue *v6 = [NSValue valueWithCGPoint:p6];
animation.values = @[v1,v2,v3,v4,v5,v6];
animation.duration = 5;
animation.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:animation forKey:@"position"];
}
#pragma mark ---- 来回晃动
- (void)buttonClick5:(UIButton *)button
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
CGFloat centerX = self.imageV.layer.position.x;
CGFloat leftX = centerX - 100;
CGFloat rightX = centerX + 100;
NSNumber *center = [NSNumber numberWithFloat:centerX];
NSNumber *left = [NSNumber numberWithFloat:leftX];
NSNumber *righr = [NSNumber numberWithFloat:rightX];
animation.values = @[center,left,righr,center,left,righr,center,left,righr,center,left,righr,center,left,righr,center,left,righr,];
animation.duration = 0.3;
animation.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:animation forKey:@"position.x"];
}
#pragma mark ---- 抖动
- (void)buttonClick6:(UIButton *)button
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
animation.values = @[@(-M_PI_4 /90.0 * 10),@(M_PI_4 /90.0 * 10),@(-M_PI_4 /90.0 * 10)];
animation.duration = 0.5;
animation.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:animation forKey:@"transform.rotation"];
}
#pragma mark ---- 3D转动
- (void)buttonClick7:(UIButton *)button
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
NSValue *value = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageV.layer.transform, M_PI, 100, 100, 100)];
animation.toValue = value;
animation.duration = 3;
animation.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:animation forKey:@"transform"];
}
#pragma mark ---- 组动画
- (void)buttonClick8:(UIButton *)button
{
CAAnimationGroup *group = [CAAnimationGroup animation];
CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
baseAnimation.toValue = [NSNumber numberWithFloat:M_PI];
baseAnimation.duration = 3;
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
CGColorRef red = [UIColor redColor].CGColor;
CGColorRef black = [UIColor blackColor].CGColor;
CGColorRef orange = [UIColor orangeColor].CGColor;
keyAnimation.values = @[(id)red,(id)black,(id)orange];
keyAnimation.duration = 2;
CAKeyframeAnimation *keyAnimation2 = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
keyAnimation2.values = @[@(-M_PI_4 /90.0 * 10),@(M_PI_4 /90.0 * 10),@(-M_PI_4 /90.0 * 10)];
keyAnimation2.duration = 0.5;
group.animations = @[baseAnimation,keyAnimation,keyAnimation2];
group.duration = 5;
group.repeatCount = CGFLOAT_MAX;
[self.imageV.layer addAnimation:group forKey:@"group"];
}