进入 CALayer 世界:
我正在创建一个无论设备方向如何都需要保持在视图中间的层。有人可以告诉我为什么我的图层在从旧位置旋转后会动画,即使我将它从超级图层中移除?我知道 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