OObjective-c CALayer 動畫

ViewController.m文件動畫

//
//  ViewController.m
//  動畫
//
//  Created by DC017 on 15/12/22.
//  Copyright © 2015年 DC017. All rights reserved.
//

#import "ViewController.h"
//宏定義
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
#define YANSE(r,g,b,a) [UIColor colorWithRed:r/225.0 green:g/225.0 blue:b/225.0 alpha:a]
//設置layer的寬和高
#define LayerWidth 50

@interface ViewController (){
    CALayer * layer1;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    layer1=[[CALayer alloc]init];
    //設置寬度
    layer1.bounds=CGRectMake(0, 0, LayerWidth, LayerWidth);
    //設置中心點的位置
    layer1.position=CGPointMake(W/2, H/2);
    //設置背景顏色
    layer1.backgroundColor=YANSE(144, 80, 122, 1).CGColor;
    //添加
    [self.view.layer addSublayer:layer1];
    //設置圓角
    layer1.cornerRadius=LayerWidth/2;
    
    //設置陰影
    //設置陰影顏色
    layer1.shadowColor=[UIColor grayColor].CGColor;
    layer1.shadowOffset=CGSizeMake(2, 2);
    layer1.shadowOpacity=0.8;//設置透明度
    
    //描點(x,y  範圍都是0-1)
//    layer1.anchorPoint=CGPointMake(1, 1);
    
    NSLog(@"%@",layer1);
    
   
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //獲取點擊位置
    UITouch * touch=[touches anyObject];
    //打印點擊位置
    NSLog(@"點擊位置是:%@", NSStringFromCGPoint([touch locationInView:self.view]));
    //打印layer的對象地址
    NSLog(@"%@",self.view.layer.sublayers);
    //獲取對象
    CALayer * layer2=self.view.layer.sublayers[2];
    //將layer移到點擊位置
    //點擊位置經過locationInView來獲取
    layer2.position=[touch locationInView:self.view];
    
    //放大
    CGFloat width=layer2.bounds.size.width;
    
    if (width==LayerWidth) {
        width=LayerWidth*4;
    }else{
        width=LayerWidth;
    }
    layer2.bounds=CGRectMake(0, 0, width, width);
    layer2.cornerRadius=width/2;//圓角是根據當前圖形寬度來設置
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
相關文章
相關標籤/搜索