代码改变世界

ios中自定义图层

2013-07-14 10:48  甘超波  阅读(1912)  评论(0编辑  收藏  举报

图层和VIEW的区别

1;view不具备显示功能,是因view内部有一个图层,才能显示出来

2:图层不具备事件功能,VIEW继承UIRespone具有处理事件功能

3:自定义的图层有一个影式动画,VIEW中图层没有隐式动画(影式动画:改变图层的某个属性,会发生动画,uiview内部图层没有影视动画)

自定义图层

#import <QuartzCore/QuartzCore.h>

@interface MJViewController ()

@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(0, 0, 100, 100);
    // position默认的含义:指当前图层的中点
    layer.position = CGPointMake(100, 100);
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.borderWidth = 5;
    layer.borderColor = [UIColor blueColor].CGColor;
    
    // 显示的内容(图片)
    layer.contents = (id)[UIImage imageNamed:@"lufy.png"].CGImage;
    [self.view.layer addSublayer:layer];
}

 自定义calayer的影式动画

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(100, 100, 100, 100);
    layer.position = CGPointMake(50, 50);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
    _layer = layer;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //self.myview.layer.position = CGPointMake(200, 200);
    
    // 可以参与隐式动画的属性:可动画属性
    [CATransaction begin];
    // 关闭隐式动画
    [CATransaction setDisableActions:YES];
    
    _layer.position = CGPointMake(200, 200);
    
    [CATransaction commit];
    
    //_layer.bounds = CGRectMake(0, 0, 20, 20);
    
    //_layer.borderWidth = 20;
    //_layer.borderColor = [UIColor blueColor].CGColor;
    
   // _layer.backgroundColor = [UIColor blueColor].CGColor;
}

 怎样操作 自定义图层中影视动画属性

xcode---window --->organizer-->在搜索框内搜索“animatable”-->在搜索结果中找到”CaLayer animable propertiy“