iOS Core Animation - 图层几何学

Z坐标轴

CALayer存在三维坐标系,layer还存在zPosition属性,通过改变zPosition的值可以改变图层的顺序

Hit Testing

CALayer不能直接处理触摸或者手势事件,但是可以通过其他方法处理事件,-containPoint:和-hitTest;

区分点击图层那一部分

例1,-containPoint


- (void)viewDidLoad {
    [super viewDidLoad];
    CALayer *blueLayer = [[CALayer alloc] init];
    blueLayer.frame = CGRectMake(0, 0, 100, 100);
    blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    [self.layerView.layer addSublayer:blueLayer];
    self.blueLayer = blueLayer;
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    point = [self.layerView.layer convertPoint:point fromLayer:self.view.layer];
    if ([self.layerView.layer containsPoint:point]) {
        point = [self.blueLayer convertPoint:point fromLayer:self.layerView.layer];
        if ([self.blueLayer containsPoint:point]) {
            NSLog(@"blueLayer");
        }else{
            NSLog(@"layerView");
        }
    }
}

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    CALayer *layer = [self.layerView.layer hitTest:point];
    if (layer == self.blueLayer) {
      NSLog(@"blueLayer");
    }else if (layer == self.layerView.layer){
        NSLog(@"layerView");
    }
}

iOS Core Animation - 图层几何学_第1张图片

 

你可能感兴趣的:(CoreAnimation,ios)