zoukankan      html  css  js  c++  java
  • CALayers的代码示例

    //
    //  KKViewController.m
    //  KKCoreAnimation
    //
    //  Created by SunKe on 13-11-6.
    //  Copyright (c) 2013年 Coneboy_K. All rights reserved.
    //
    
    #import <QuartzCore/QuartzCore.h>
    
    #import "KKViewController.h"
    
    @interface KKViewController ()
    
    @end
    
    @implementation KKViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        
    }
    /*
     rotation.x  沿着x轴翻转
     rotation.y  沿着y轴翻转
     rotation.z  沿着z轴翻转
     rotation    沿着z轴翻转
     
     scale.x    沿着x轴缩放
     scale.y    沿着y轴缩放
     scale.z    沿着z轴缩放
     scale      沿着x,y,z轴缩放
     
     translation.x  在三维坐标系沿着x轴移动
     translation.y  在三维坐标系沿着y轴移动
     translation.z  在三维坐标系沿着z轴移动
     translation    在三维坐标系沿着x,y轴移动
     
     CABasicAnimation *scaoleAnimation  = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];通过对引号内的名字进行更改 从而改变layer的动画效果
     */
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [self moveLayerPosition];
        [self moveLayerScaole];
        [self moveLayerRotate];
        
        [self moveLayer];
    }
    
    - (void)moveLayerPosition
    {
        
        CALayer *kkLayer = [[CALayer alloc]init];
        kkLayer.backgroundColor = [[UIColor blueColor]CGColor];
        kkLayer.frame = CGRectMake(50, 65, 40, 40);
        kkLayer.cornerRadius = 5;
        [self.view.layer addSublayer:kkLayer];
        
        // 移动kkLayer的position
    	CABasicAnimation *animation  = [CABasicAnimation animationWithKeyPath:@"position"];
    	animation.fromValue = [NSValue valueWithCGPoint:kkLayer.position];
    	CGPoint toPoint = kkLayer.position;
    	toPoint.x += 180;
    	animation.toValue = [NSValue valueWithCGPoint:toPoint];
        animation.autoreverses = YES;
        animation.duration = 3;
        animation.repeatCount = NSNotFound;
    	
        [kkLayer addAnimation:animation forKey:@"animationPosition"];
    
    }
    
    - (void)moveLayerScaole
    {
        
        CALayer *kkLayer = [[CALayer alloc]init];
        kkLayer.backgroundColor = [[UIColor greenColor]CGColor];
        kkLayer.frame = CGRectMake(50, 131, 40, 40);
        kkLayer.cornerRadius = 5;
        [self.view.layer addSublayer:kkLayer];
        
        // 对kkLayer进行放大缩小
    	CABasicAnimation *scaoleAnimation  = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    	scaoleAnimation.duration = 3;
        scaoleAnimation.autoreverses = YES;
    	scaoleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    	scaoleAnimation.toValue = [NSNumber numberWithFloat:2.5];
        scaoleAnimation.fillMode = kCAFillModeForwards;
        scaoleAnimation.repeatCount = NSNotFound;
        
        [kkLayer addAnimation:scaoleAnimation forKey:@"animationScaole"];
    
    	
    }
    
    - (void)moveLayerRotate
    {
        
        CALayer *kkLayer = [[CALayer alloc]init];
        kkLayer.backgroundColor = [[UIColor grayColor]CGColor];
        kkLayer.frame = CGRectMake(50, 217, 40, 40);
        kkLayer.cornerRadius = 5;
        [self.view.layer addSublayer:kkLayer];
        
        // 以x轴进行旋转
    	CABasicAnimation *rotateAnimation  = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    	rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    	rotateAnimation.toValue = [NSNumber numberWithFloat:6.0 * M_PI];
        rotateAnimation.duration = 3;
        rotateAnimation.repeatCount = NSNotFound;
        
        [kkLayer addAnimation:rotateAnimation forKey:@"animationRotate"];
    
    }
    
    
    - (void)moveLayer
    {
        
        CALayer *kkLayer = [[CALayer alloc]init];
        kkLayer.backgroundColor = [[UIColor redColor]CGColor];
        kkLayer.frame = CGRectMake(50, 295, 40, 40);
        kkLayer.cornerRadius = 5;
        [self.view.layer addSublayer:kkLayer];
        
        // 移动kkLayer的position
    	CABasicAnimation *animation  = [CABasicAnimation animationWithKeyPath:@"position"];
    	animation.fromValue = [NSValue valueWithCGPoint:kkLayer.position];
    	CGPoint toPoint = kkLayer.position;
    	toPoint.x += 180;
    	animation.toValue = [NSValue valueWithCGPoint:toPoint];
    	
        // 以x轴进行旋转
    	CABasicAnimation *rotateAnimation  = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    	rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    	rotateAnimation.toValue = [NSNumber numberWithFloat:6.0 * M_PI];
        
        // 对kkLayer进行放大缩小
    	CABasicAnimation *scaoleAnimation  = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    	scaoleAnimation.duration = 3;
        scaoleAnimation.autoreverses = YES;
    	scaoleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    	scaoleAnimation.toValue = [NSNumber numberWithFloat:2.5];
        scaoleAnimation.fillMode = kCAFillModeForwards;
    	
    	// 把上面的动画组合起来
    	CAAnimationGroup *group = [CAAnimationGroup animation];
    	group.autoreverses = YES;  // 完成后反向完成
    	group.duration = 3.0;
    	group.animations = [NSArray arrayWithObjects:animation,rotateAnimation, scaoleAnimation, nil];
    	group.repeatCount = NSNotFound;
        
        group.fillMode = kCAFillModeForwards;
    	
    	[kkLayer addAnimation:group forKey:@"kkLayerMove"];
      
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
  • 相关阅读:
    JAVA编程规则【转自java编程思想】
    诊断 Java 代码: 轻松掌握 Java 泛型
    Linux开启telnet远程登录服务全攻略
    TCP详解
    UNIX环境高级编程文件描述符浅析
    DHCP与BOOTP有什么区别
    Linux 多播(组播)例程
    你所不知道的传输层
    虚电路方式,数据报方式
    java foreach 使用
  • 原文地址:https://www.cnblogs.com/AbelChen1991/p/3626961.html
Copyright © 2011-2022 走看看