Core Animation (UI  高级技术)

来源:互联网 发布:高级编程语言培训 编辑:程序博客网 时间:2024/05/17 22:29

1.Core Animation  是一个图形渲染的底层实现的框架;

可以提供专门级的动画,是高层图形技术的基础。

        如何使用:通过ACALayer类。可以直接对一个视图的Core Animation

层进行设置,达到需要的效果。

任何一个视图对象, 都有一个属性叫.layer, 是CALayer类型。

CALayer的简单应用:

1.创建一个ViewController

@interfaceTRViewController ()

@property (weak,nonatomic) IBOutletUIImageView *imageView;创建一个UIImageView

@end


- (void)viewDidLoad

{

    [superviewDidLoad];

   //拿到视图的层对象

CALayer *layer =self.view.layer;

    layer.backgroundColor = [UIColororangeColor].CGColor;//设置layer的背景颜色

    layer.cornerRadius =20.0; //可以通过这个属性轻易的将视图改为圆角

    

    self.imageView.layer.cornerRadius = 8.0;

    self.imageView.layer.masksToBounds = YES;//是否添加蒙板,显露出圆角

    

    //加子层

   CALayer *subLayer = [CALayerlayer];

    subLayer.backgroundColor = [[UIColorpurpleColor] CGColor];

    subLayer.shadowColor = [UIColorgreenColor].CGColor;//阴影颜色

    subLayer.shadowOffset =CGSizeMake(2,2);//阴影的偏移量

    subLayer.shadowRadius =5.0;//阴影的圆角属性

    subLayer.shadowOpacity =0.8;//阴影透明度

    subLayer.frame =CGRectMake(30,200, 100, 120);

    subLayer.cornerRadius =10.0;

    [layeraddSublayer:subLayer];

    

   //加有内容(可以是图片)的层

   CALayer *imageLayer = [CALayernew];

    imageLayer.frame =CGRectMake(180,200, 100, 120);

    imageLayer.contents = (id)[UIImageimageNamed:@"d.jpg"].CGImage;//注意类型

    imageLayer.cornerRadius =10.0;

    imageLayer.masksToBounds =YES;//遮罩(蒙板) 存在蒙板的情况下添加阴影效果会被蒙板遮罩 ,如果需要阴影效果,可能需要再图片位置创建另一个视图,将其透明度设置为0.1;注意:如果将其设置为0 其阴影的透明度也将为0;

    [layeraddSublayer:imageLayer];    //不要忘了将子层添加到父层中

}

2.Core Animation 动画

2.1>关键帧动画(CAKeyframeAnimation)

可以根据(UIBezierPath)创建的路径进行运动的动画

创建方法:

[CAKeyframeAnimationanimationWithKeyPath:@"position"];

参数指的是CALayer中的position属性,不能随便写,动画过程其实是对此属性的不断修改。

2.2>基本动画(CABasicAnimation)

可以用改变属性值的方式做一些如旋转,缩放,alpha等的变化

[CABasicAnimationanimationWithKeyPath:@"transform"];

参数指是CALayer中的transform属性

[CABasicAnimationanimationWithKeyPath:@"opacity"];

CALayer中的opacity属性是透明度设置

注意: 改变其frame 用position ,改变其alpha(透明度)用opacity ,改变其角度、缩变 这样的形变用 transform。

如:

//创建path路径

- (IBAction)keyframeAnimation//创建按钮来开始动画

{


UIBezierPath *path = [UIBezierPathbezierPath];

    [path moveToPoint:self.imageView.center];

    CGPoint target =CGPointMake(self.view.bounds.size.width - self.imageView.frame.size.width * 0.5 - 20 ,self.view.bounds.size.height -self.imageView.frame.size.height * 0.5 - 20);

    [path addCurveToPoint:target controlPoint1:CGPointMake(self.imageView.center.x, target.y) controlPoin t2:CGPointMake(target.x,self.imageView.center.y)];

    //创建关键帧

    CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

   moveAnimation.path = path.CGPath;//指定动画行走路径

    moveAnimation.removedOnCompletion = YES;//动画结束后删除动画


     CABasicAnimation *scaleAnimation = [CABasicAnimationanimationWithKeyPath:@"transform"];

    scaleAnimation.fromValue = [NSValuevalueWithCATransform3D:CATransform3DIdentity];

    scaleAnimation.toValue = [NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.1,0.1, 1.0)];

    scaleAnimation.removedOnCompletion = YES;

   

     //基本动画(CABasicAnimation)

    //透明度动画

    CABasicAnimation *alphaAnimation = [CABasicAnimationanimationWithKeyPath:@"opacity"];

    alphaAnimation.fromValue = @1.0;//初始值

    alphaAnimation.toValue = @0.1;// 结束值

    alphaAnimation.removedOnCompletion = YES;

    //执行动画


    CAAnimationGroup *animationGroup = [CAAnimationGroupanimation];//创建动画组

animationGroup.animations =@[moveAnimation, scaleAnimation, alphaAnimation];//将每个动画效果添加到动画组中,以便统一同一属性。

    animationGroup.duration = 2.0;// 动画时长

    animationGroup.delegate = self;//将自身付给委托协议,调用委托方法,实现动画结束后所需处理的事,比如删除图片;

    [self.imageView.layeraddAnimation:animationGroup forKey:nil];

}

//委托方法

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

    [self.imageView removeFromSuperview];

}



0 0
原创粉丝点击