2012-09-04 13 views
0

我有一個CALayer,並且作爲這個CALayer的子圖層,我添加了一個imageLayer,其中包含分辨率爲276x183的圖像。使用UIPanGestureRecognizer在視圖周圍移動子圖層是不錯的選擇嗎?

我添加了一個UIPanGestureRecognizer到主視圖和計算的CALayer的座標如下:

- (void)panned:(UIPanGestureRecognizer *)sender{ 

     subLayer.frame=CGRectMake([sender locationInView:self.view].x-138, [sender locationInView:self.view].y-92, 276, 183); 

} 

在viedDidLoad我有:

subLayer.backgroundColor=[UIColor whiteColor].CGColor; 
subLayer.frame=CGRectMake(22, 33, 276, 183); 

imageLayer.contents=(id)[UIImage imageNamed:@"A.jpg"].CGImage; 
imageLayer.frame=subLayer.bounds; 
imageLayer.masksToBounds=YES; 
imageLayer.cornerRadius=15.0; 

subLayer.shadowColor=[UIColor blackColor].CGColor; 
subLayer.cornerRadius=15.0; 
subLayer.shadowOpacity=0.8; 
subLayer.shadowOffset=CGSizeMake(0, 3); 
[subLayer addSublayer:imageLayer]; 
[self.view.layer addSublayer:subLayer]; 

這是給需要的輸出,但有點慢在模擬器中。我還沒有在Device中測試過它。所以我的問題是 - 移動包含圖像的CALayer可以嗎?

回答

1

兩件事情:

首先,你不能得出基於模擬器的性能的任何結論。模擬器上的某些事情比設備上的快一個數量級,而其他事情則顯着較慢。動畫尤其是混合包。

如果你做性能關鍵工作,測試設備上,及早並經常。

其次,你當然可以用動畫手勢識別層,但是這是一個非常迂迴的方式來做到這一點。手勢識別器設計用於處理視圖,將識別器綁定到子視圖而不是子圖層會更容易和更清晰。

使用圖層時遇到的一個重大問題是命中測試。如果你放開圖像,然後嘗試拖動它,則必須在包含視圖上顯示手勢,獲取手勢座標並在圖層上進行測試。啊。

看看蘋果觸摸示例應用程序的基於手勢的版本。它向你展示瞭如何使用手勢在屏幕上清理移動UIView對象。

請注意,您可以創建具有自定義圖層內容的視圖,並將其拖動。

1

是的,移動包含圖像的CALayer是可以的。

如果您只想移動圖像而不是更新整個frame,則應該更新圖層的position屬性。像這樣的:

- (void)panned:(UIPanGestureRecognizer *)sender{ 
    subLayer.position=CGPointMake([sender locationInView:self.view].x, [sender locationInView:self.view].y); 

}

相關問題