@property(nonatomic,readonly,strong) CALayer *layer;
//设置阴影
//opacity:不透明度
_imageview.layer.shadowOpacity = 1;
_imageview.layer.shadowOffset = CGSizeMake(10, 10);
//注意:图层的颜色都是核心绘图框架,通常CGColor
_imageview.layer.shadowColor = [UIColor redColor].CGColor;
_imageview.layer.shadowRadius = 10;
//cornerRadiu设置控件的主层边框
_imageview.layer.cornerRadius = 50;
_imageview.layer.borderColor = [UIColor purpleColor].CGColor;
_imageview.layer.borderWidth = 2;
_imageview.layer.cornerRadius = 200;
//如果是yes,表示需要裁剪。裁剪的是主层。超出主层边框的内容会被全部裁剪掉。缺点:边缘可能会有锯齿
_imageview.layer.masksToBounds = YES;
_imageview.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5);
_imageview.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
//快速进行图层缩放,KVC
[_imageview.layer setValue:@0.5 forKeyPath:@"transform.scale"];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(50, 100, 200, 200);
layer.contents = (id)[UIImage imageNamed:@"阿狸头像.png"].CGImage;
[self.view.layer addSublayer:layer];
速度控制函数(CAMediaTimingFunction)
- kCAMediaTimingFunctionLinear(线性):匀速,给你一个相- 对静态的感觉
- kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开
- kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
- kCAMediaTimingFunctionEaseInEaseOut(渐进渐出):动画缓慢的进入,中间加速,然后减速的到达目的地。这个是默认的动画行为。
#pragma mark 暂停CALayer的动画
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
// 让CALayer的时间停止走动
layer.speed = 0.0;
// 让CALayer的时间停留在pausedTime这个时刻
layer.timeOffset = pausedTime;
}
#pragma mark 恢复CALayer的动画
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = layer.timeOffset;
// 1. 让CALayer的时间继续行走
layer.speed = 1.0;
// 2. 取消上次记录的停留时刻
layer.timeOffset = 0.0;
// 3. 取消上次设置的时间
layer.beginTime = 0.0;
// 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
// 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
layer.beginTime = timeSincePause;
}
是CAAnimation的子类,也是个抽象类,要想创建动画对象,应该使用它的两个子类:
属性说明:
//创建动画
CABasicAnimation *anim = [CABasicAnimation animation];
// //描述下修改哪个属性产生动画。只能是layer的属性
// anim.keyPath = @"position";
// //设置值
// anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 250)];
//缩放
anim.keyPath = @"transform.scale";
//设置值
anim.toValue = @0.5;
//设置动画执行次数.MAXFLOAT表示无限次执行
anim.repeatCount = MAXFLOAT;
//设置动画完成的时候不要移除动画
anim.removedOnCompletion = NO;
//设置动画执行完成要保持最新的效果
anim.fillMode = kCAFillModeForwards;
[_imageview.layer addAnimation:anim forKey:nil];
属性说明:
CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation
#define angle2Radion(angle) (angle / 180.0 * M_PI)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle2Radion(-5)),@(angle2Radion(5)),@(angle2Radion(-5))];
anim.duration = 1.0;
anim.repeatCount = MAXFLOAT;
[_imageview.layer addAnimation:anim forKey:nil];
_imageview.layer.anchorPoint = CGPointZero;
anim.keyPath = @"position";
anim.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(30, 30, 100, 100)].CGPath;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//touch
UITouch *touch = [touches anyObject];
NSLog(@"touch---%@",touches);
//获取手指的触点
CGPoint curP = [touch locationInView:self];
//创建路径
UIBezierPath *path = [UIBezierPath bezierPath];
_path = path;
//设置起点
[path moveToPoint:curP];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
//获取手指的触点
CGPoint curP = [touch locationInView:self];
[_path addLineToPoint:curP];
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect{
[_path stroke];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.path = _path.CGPath;
anim.duration = 1;
anim.repeatCount = MAXFLOAT;
[[[self.subviews firstObject] layer] addAnimation:anim forKey:nil];
}
static int i = 2;
-(void)trans{
//转场代码
if (i == 4) {
i = 1;
}
//加载图片名称
NSString *imageN = [NSString stringWithFormat:@"%d",i];
_imageview.image = [UIImage imageNamed:imageN];
i++;
//转场动画
CATransition *anim = [CATransition animation];
anim.type = @"suckEffect"; //suckEffect是往父控件左上角抽的
[_imageview.layer addAnimation:anim forKey:nil];
}
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
参数说明:
duration:动画的持续时间
view:需要进行转场动画的视图
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;
参数说明:
duration:动画的持续时间
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block
CAAnimationGroup *group = [CAAnimationGroup animation];
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @0.5;
CABasicAnimation *rotation = [CABasicAnimation animation];
rotation.keyPath = @"transform.rotation";
rotation.toValue = @(M_PI_4);
CABasicAnimation *position = [CABasicAnimation animation];
position.keyPath = @"position";
position.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(200))];
group.animations = @[scale,rotation,position];
[_imageview.layer addAnimation:group forKey:nil];
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"position";
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(150, 400)];
//注意取消反弹必须放在图层添加动画之前
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.delegate = self;
[_imageview.layer addAnimation:anim forKey:nil];
在动画完成的代理中打印_imageview.layer.position,打印结果为{165, 114.5},说明_imageview.layer.position并没有发生改变
[UIView animateWithDuration:0.5 animations:^{
_imageview.layer.position = CGPointMake(150, 400);
} completion:^(BOOL finished) {
NSLog(@"%@",NSStringFromCGPoint(_imageview.layer.position));
}];
打印结果为{150, 400}