2012-04-05 8 views
0

私は自分のコードでCALayersのアニメーションを使用しています。 以下は私のコードCALayerのイベント

CALayer *backingLayer = [CALayer layer]; 
     backingLayer.contentsGravity = kCAGravityResizeAspect; 
     // set opaque to improve rendering speed 
     backingLayer.opaque = YES; 


     backingLayer.backgroundColor = [UIColor whiteColor].CGColor; 

     backingLayer.frame = CGRectMake(0, 0, templateWidth, templateHeight); 

     [backingLayer addSublayer:view.layer]; 
     CGFloat scale = [[UIScreen mainScreen] scale]; 
     CGSize size = CGSizeMake(backingLayer.frame.size.width*scale, backingLayer.frame.size.height*scale); 
     UIGraphicsBeginImageContextWithOptions(size, NO, scale); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     [backingLayer renderInContext:context]; 

     templateImage = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

このbackingLayerは、多くのサブ層は、このように追加されました、そしてこのビューは私のサブビューです。 しかし、私はサブレイヤとしてそれらを追加して以来、私はそれぞれのUIViewのビューのイベントを得ることができます、私は彼らがサブレイヤであるにもかかわらず、ページナビゲーションとクリックイベントを持っているFlipboardアプリケーションのようなものを達成しようとしています。

答えて

2

CALayersのポイントは、軽量で、特にイベント処理のオーバーヘッドがないことです。これがUIViewの目的です。イベントトラッキングのためにコードをUIViewを使用して変換するか、独自のイベントパッシングコードを記述するかの選択肢があります。 2番目の場合は、基本的には、あなたの含むUIViewは、各サブレイヤの境界のために "rect point in rect"クエリを行い、最も高いz位置を持つCALayer(のカスタムメソッド)にイベントを渡します。

1

言い換えれば、CALayersはイベント処理を直接サポートしていません。ただし、CALayerを含むUIViewでイベントをキャプチャし、UIViewの暗黙的なレイヤに「hitTest」メッセージを送信して、どのレイヤーにタッチしたかを判断できます。たとえば:

Returns the farthest descendant of the receiver in the layer hierarchy (including itself) that contains a specified point. 

Return Value 
The layer that contains thePoint, or nil if the point lies outside the receiver’s bounds rectangle. 
:ここ

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 
    CALayer *target = [self.layer hitTest:location]; 
    // target is the layer that was tapped 
} 

は、AppleのドキュメントからhitTestについての詳細情報です