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<UITouch *> *)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<UITouch *> *)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");
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章