2013-02-24 92 views
0

去除進入CALayer的世界後,造成動畫:CALayer的幀值甚至superlayer

我創建需要保持在視圖中,無論設備方向的層。有人可以告訴我爲什麼我的圖層在旋轉之後從舊位置開始動畫,即使我將它從超級圖層中移除了?我知道frame和borderWidth屬性是動畫的,但它們是否可以從superLayer中移除後生成動畫?

如果從superLayer中刪除不會重置圖層屬性,因爲圖層對象還沒有被釋放(好吧,我可以理解這一點),我該如何模仿新顯示的圖層的行爲,以便邊框不會顯示像它在旋轉之後從舊位置移動。

我創建了這個示例項目 - 剪切和粘貼,如果你願意。你只需要鏈接石英核心庫。

#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 
@property (nonatomic,strong) CALayer *layerThatKeepAnimating; 
@end 

@implementation ViewController 

-(CALayer*) layerThatKeepAnimating 
{ 
    if(!_layerThatKeepAnimating) 
    { 
    _layerThatKeepAnimating=[CALayer layer]; 
    _layerThatKeepAnimating.borderWidth=2; 
    } 
return _layerThatKeepAnimating; 
} 


-(void) viewDidAppear:(BOOL)animate 
{  
self.layerThatKeepAnimating.frame=CGRectMake(self.view.bounds.size.width/2-50,self.view.bounds.size.height/2-50, 100, 100); 
    [self.view.layer addSublayer:self.layerThatKeepAnimating]; 
} 


-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [self.layerThatKeepAnimating removeFromSuperlayer]; 
} 


-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    self.layerThatKeepAnimating.frame=CGRectMake(self.view.bounds.size.width/2-50,self.view.bounds.size.height/2-50, 100, 100); 
    [self.view.layer addSublayer:self.layerThatKeepAnimating]; 
} 

@end 
+0

[移動CALayers時禁用動畫]的可能重複(http://stackoverflow.com/questions/2930166/disable-animation-when-moving-calayers) – jrturton 2013-02-24 16:39:31

+0

該鏈接接受的答案(雖然我」 d使用setDisablesActions)將會做你需要的。 – jrturton 2013-02-24 16:40:14

+0

@ jrturton,這不是重複的。在另一個鏈接和你的兩個答案都屬於CATransaction。我沒有在這裏使用它。 – Spectravideo328 2013-02-24 16:44:35

回答

0

奇怪,因爲這聽起來,答案是在

willRotateToInterfaceOrientation移動代碼 到 viewWillLayoutSubviews

-(void) viewWillLayoutSubviews 
{ 
    self.layerThatKeepAnimating.frame=CGRectMake(self.view.bounds.size.width/2-50,self.view.bounds.size.height/2-50, 100, 100); 
    [self.view.layer addSublayer:self.layerThatKeepAnimating]; 
} 

它看起來像任何層「重繪」這裏發生無動畫,甚至如果圖層屬性是可動畫的。

0

那麼問題不在於你的想法;當你從超級視圖中移除該層時,它實際上並沒有被刪除,因爲你保留了對它的強烈引用。您的代碼不進入,吸附劑中的if語句創建一個新層,因爲它從來都不是零後的第一次:

if(!_layerThatKeepAnimating) 
    { 
    _layerThatKeepAnimating=[CALayer layer]; 
    _layerThatKeepAnimating.borderWidth=2; 
    } 

因此,要麼改變你的參考VC層中弱:

@property (nonatomic, weak) CALayer * layerThatKeepAnimating; 

或通過顯式刪除它:

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [self.layerThatKeepAnimating removeFromSuperlayer]; 
    self.layerThatKeepAnimating = nil; 
} 

我會建議你,因爲你添加層(或子視圖)到你的意見已經獲得一個有力的參考使用第一個選項。這就是爲什麼總是建議這樣做:

@property (weak, nonatomic) IBOutlet UIView *view; 

但不(強,非原子)。

0
[self.sublayerToRemove removeFromSuperlayer]; 
self.sublayerToRemove = nil;