今天很匆忙的寫了這篇文章,希望對(duì)大家與幫助。先展示一下動(dòng)畫效果泳梆!
都是一些簡(jiǎn)單的動(dòng)畫效果
// 縮放
/**
* 縮放
*/
- (void)scale {
CALayer *scaleLayer = [[CALayer alloc] init];
scaleLayer.backgroundColor = [UIColor purpleColor].CGColor;
scaleLayer.frame = CGRectMake(50, 70, 25, 25);
scaleLayer.shadowOffset = CGSizeMake(0, 10);
scaleLayer.shadowRadius = 3.0f;
scaleLayer.shadowOpacity = 0.5f;
scaleLayer.cornerRadius = scaleLayer.bounds.size.width * 0.5;
scaleLayer.masksToBounds = YES;
[self.view.layer addSublayer:scaleLayer];
CABasicAnimation *scaleAnimation = [[CABasicAnimation alloc] init];
scaleAnimation.keyPath = @"transform.scale"; // 設(shè)置動(dòng)畫的方式
scaleAnimation.fromValue = [NSNumber numberWithInteger:1]; // 起點(diǎn)
scaleAnimation.toValue = [NSNumber numberWithInteger:2]; // 終點(diǎn)
scaleAnimation.duration = 1.0f; // 設(shè)置動(dòng)畫的時(shí)間
scaleAnimation.repeatCount = MAXFLOAT; // 設(shè)置動(dòng)畫的次數(shù)
scaleAnimation.autoreverses = YES; // 返回原始狀態(tài)是否動(dòng)畫方式
[scaleLayer addAnimation:scaleAnimation forKey:@"scale"]; // 添加動(dòng)畫
}
// 平移
/**
* 平移
*/
- (void)position {
CALayer *positionLayer = [[CALayer alloc] init];
positionLayer.backgroundColor = [UIColor redColor].CGColor;
positionLayer.cornerRadius = 5.0f;
positionLayer.masksToBounds = YES;
positionLayer.frame = CGRectMake(50, 140, 25, 25);
[self.view.layer addSublayer:positionLayer];
CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; // 設(shè)置動(dòng)畫方式沿著X軸
positionAnimation.toValue = [NSNumber numberWithInteger:250];
positionAnimation.duration = 3.0f;
positionAnimation.repeatCount = MAXFLOAT;
positionAnimation.autoreverses = YES;
[positionLayer addAnimation:positionAnimation forKey:@"position"];
}
// 旋轉(zhuǎn)
/**
* 旋轉(zhuǎn)
*/
- (void)rotate {
CALayer *rotateLayer = [[CALayer alloc] init];
rotateLayer.backgroundColor = [UIColor blueColor].CGColor;
rotateLayer.cornerRadius = 5.0f;
rotateLayer.masksToBounds = YES;
rotateLayer.frame = CGRectMake(50, 200, 50, 50);
[self.view.layer addSublayer:rotateLayer];
CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
rotateAnimation.duration = 2.0f;
rotateAnimation.autoreverses = NO;
rotateAnimation.repeatCount = MAXFLOAT;
[rotateLayer addAnimation:rotateAnimation forKey:@"rotate"];
}
// 動(dòng)畫結(jié)合
/**
* 動(dòng)畫組
*/
- (void)groupAnimation {
CALayer *groupLayer = [[CALayer alloc] init];
groupLayer.backgroundColor = [UIColor grayColor].CGColor;
groupLayer.frame = CGRectMake(50, 280, 25, 25);
groupLayer.cornerRadius = 10.0f;
groupLayer.masksToBounds = YES;
[self.view.layer addSublayer:groupLayer];
// 縮放
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
scaleAnimation.toValue = [NSNumber numberWithFloat:2.0];
scaleAnimation.duration = 1.0f;
scaleAnimation.repeatCount = MAXFLOAT;
scaleAnimation.autoreverses = YES;
// 平移
CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];
positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 400)];
positionAnimation.duration = 3.0f;
positionAnimation.autoreverses = YES;
// 旋轉(zhuǎn)
CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
rotateAnimation.duration = 0.8f;
rotateAnimation.autoreverses = NO;
rotateAnimation.repeatCount = MAXFLOAT;
// 動(dòng)畫組
CAAnimationGroup *animationGroup = [[CAAnimationGroup alloc] init];
animationGroup.duration = 2.0f;
animationGroup.autoreverses = YES;
animationGroup.repeatCount = MAXFLOAT;
animationGroup.animations = @[scaleAnimation, positionAnimation, rotateAnimation];
[groupLayer addAnimation:animationGroup forKey:@"group"];
}
//iOS還有一些其他的KeyPath來實(shí)現(xiàn)其他的動(dòng)畫
//transform.rotation.x 繞X軸旋轉(zhuǎn)
//transform.rotation.y 繞Y軸旋轉(zhuǎn)
//transform.scale.x 縮放X
//transform.scale.y 縮放Y
//transform.translation.x 平移X
//transform.translation.y 平移Y