码迷,mamicode.com
首页 > 移动开发 > 详细

《iOS核心动画高级技巧》part1-CALayer

时间:2015-07-28 22:59:09      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

UIView和CALayer的选择

可以发现,前面的2个效果不仅可以通过添加层来实现,还可以通过添加UIView来实现。如显示图片的层可以用一个UIImageView来实现。 既然CALayer和UIView都能实现相同的显示效果,那究竟该选择谁好呢?

其实,对比CALayer,UIView多了一个事件处理的功能。也就是说,CALayer不能处理用户的触摸事件,而UIView可以。

所以,在选择的过程中,需要考虑到实际的情况,如果显示出来的东西需要跟用户进行交互的话,用UIView;如果不需要跟用户进行交互,用UIView或者CALayer都可以

当然,CALayer的性能会高一些,因为它少了事件处理的功能,更加轻量级 。

特别注意:如果一个控件是另外一个控件的子控件,那么这个控件的layer也是另一个控件的子layer。

技术分享

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addLayerTwo];
//    [self addLayerOne];
}

//part1
-(void)addLayerOne{
    //添加图层
    CALayer *blueLayer = [CALayer layer];
    blueLayer.frame = CGRectMake(20.0,20.0,50.0, 50.0);
    blueLayer.backgroundColor= [UIColor blueColor].CGColor;
    [self.view.layer addSublayer:blueLayer];
    
    
    
    
    //为图层添加内容(图片)
    UIImage *image = [UIImage imageNamed:@"appResearch"];
    self.myView.layer.contents = (__bridge id)image.CGImage;
    //为图层设置图片的模式
    self.myView.layer.contentsGravity = kCAGravityCenter;
    //设置圆角半径为10
    self.myView.layer.cornerRadius=10;
    //如果设置了图片,那么需要设置这个属性为YES才能显示圆角效果
    self.myView.layer.masksToBounds=YES;
    //设置边框
    self.myView.layer.borderWidth=3;
    self.myView.layer.borderColor=[UIColor brownColor].CGColor;
}
//part2
-(void)addLayerTwo{
    CALayer *layer = [[CALayer alloc]init];
    layer.backgroundColor = [UIColor brownColor].CGColor;
    layer.bounds = CGRectMake(0, 0, 50, 50);
    layer.position = CGPointMake(100, 100);//锚点到父层(0,0)的坐标的距离,锚点默认为(0.5,0.5)居中
    [self.myView.layer addSublayer:layer];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 layer的响应事件

-(void)part1EventResponse{
    //响应测试
    self.blueLayer = [CALayer layer];
    self.blueLayer.frame = CGRectMake(50, 50, 100, 100);
    self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    self.blueLayer.delegate = self;
    [self.view.layer addSublayer:self.blueLayer];
}

-(void)touchesBegan:(NSSet *)touches
          withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject]locationInView:self.view];
    NSLog(@"point1=%@",NSStringFromCGPoint(point));
    if ([self.view.layer containsPoint:point]) {
        //判断点击区域方法1
//        point = [self.blueLayer convertPoint:point fromLayer:self.view.layer];
//        NSLog(@"point2=%@",NSStringFromCGPoint(point));
//        if ([self.blueLayer containsPoint:point]) {
//            [[[UIAlertView alloc]initWithTitle:@"layer" message:nil delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:nil, nil] show];
//        }
        //判断点击区域方法2
        CALayer *layer = [self.view.layer hitTest:point];
        if (layer==self.blueLayer) {
            [[[UIAlertView alloc]initWithTitle:@"layer" message:nil delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:nil, nil] show];
        }
    }
}

添加layer图层蒙版

    //图层蒙板
    CALayer *maskLayer = [CALayer layer];
    UIImage *image = [UIImage imageNamed:@"tuxedomask"];
    maskLayer.contents = (__bridge id)(image.CGImage);
    maskLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    self.imageView.layer.mask = maskLayer;

设置layer的矩阵变换

//********************************2d*****************************//
    //简单操作
    //    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_4);
    
    //组合操作
//    CGAffineTransform originTransform = CGAffineTransformIdentity;
//    originTransform = CGAffineTransformScale(originTransform, 0.5, 0.5);
//    originTransform = CGAffineTransformTranslate(originTransform, 200, 0);
//    originTransform = CGAffineTransformRotate(originTransform, M_PI_4);
//    self.imageView.layer.affineTransform = originTransform;
    //********************************3d*****************************//
    //没有设置m34透视投影的情况
//    CATransform3D transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
//    self.imageView.layer.transform = transform;
    //设置m34透视投影.需要设置视觉相机和屏幕之间的距离,通常是500~1000
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1.0/500;//
    transform = CATransform3DRotate(transform, M_PI_4, 0, 1, 0);
    self.imageView.layer.transform = transform;

 设置组合图层的矩阵变换

//sublayerTransform设置所有本图层及所有子图透视投影
    CATransform3D perspective = CATransform3DIdentity;
    perspective.m34 = -1.0/500;
    self.backgroundView.layer.sublayerTransform = perspective;
    
    CATransform3D transform1 = CATransform3DMakeRotation(M_PI_4, 0, 1, 0);
    self.view1.layer.contents  = (__bridge id)([UIImage imageNamed:@"123.jpg"].CGImage);
    self.view1.layer.transform = transform1;
    
    CATransform3D transform2 = CATransform3DMakeRotation(-M_PI_4, 0, 1, 0);
    self.view2.layer.contents = (__bridge id)([UIImage imageNamed:@"123.jpg"].CGImage);
    self.view2.layer.transform = transform2;

 

《iOS核心动画高级技巧》part1-CALayer

标签:

原文地址:http://www.cnblogs.com/huen/p/4286948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!