这是你的自定义类:
@implementation MyLayer
-(id)init
{
self = [super init];
if (self != nil)
self.actions = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNull null], @"bounds",
nil];
return self;
}
-(void)drawInContext:(CGContextRef)context
{
CGContextSetRGBFillColor(context,
drand48(),
drand48(),
drand48(),
1);
CGContextFillRect(context,
CGContextGetClipBoundingBox(context));
}
+(BOOL)needsDisplayForKey:(NSString*)key
{
if ([key isEqualToString:@"bounds"])
return YES;
return [super needsDisplayForKey:key];
}
@end
这些补充的Xcode 4.2默认的模板:
-(BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// create and add layer
MyLayer *layer = [MyLayer layer];
[self.window.layer addSublayer:layer];
[self performSelector:@selector(changeBounds:)
withObject:layer];
return YES;
}
-(void)changeBounds:(MyLayer*)layer
{
// change bounds
layer.bounds = CGRectMake(0, 0,
drand48() * CGRectGetWidth(self.window.bounds),
drand48() * CGRectGetHeight(self.window.bounds));
// call "when idle"
[self performSelector:@selector(changeBounds:)
withObject:layer
afterDelay:0];
}
-----------------编辑:
好吧......这不是你要求的:)对不起:
-----------------编辑(2):
为什么你会需要这样的东西吗? (void)display
可能会被使用,但文件说,它是在那里设置self.contents
...
另一件行不通的事:子类化和重载'setFrame','setBounds'和'setPosition'。在动画过程中不会调用它们。 –
我不太了解你。你想要做什么动画?只是CALayer的界限或别的什么?限制动画是非常简单的任务,框架动画 - 更复杂。 – beryllium
想象一下,您的图层包含类似具有复杂但大小独立的边框图形的按钮。如果将动画设置为宽度的两倍,则它将使用位图缩放进行动画制作,在整个动画中变得拉伸并像素化,即使您拥有“needsDisplayOnBoundsChange”YES。只有最后一帧才能用'drawInContext:'正确渲染。 –