這是你的自定義類:
@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:'正確渲染。 –