Esta es la clase personalizada:
@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
Estos son adiciones a Xcode 4.2 plantilla por defecto:
-(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];
}
----------------- editado:
Ok ... esto no es lo que pediste :) Lo siento: |
----------------- editado (2):
Y ¿Por qué necesitaría algo por el estilo? (void)display
se puede utilizar, pero la documentación dice que está ahí para establecer self.contents
...
Otra cosa que no funciona: subclassing y anulación 'setFrame',' setBounds' y 'setPosition'. No son llamados durante la animación. –
No te entiendo del todo. ¿Qué estás tratando de animar? ¿Solo los límites de CALayer o algo más? Bounds animating es una tarea bastante simple, animando cuadros, más compleja. – beryllium
Imagine que su capa contiene algo así como un botón con un gráfico de borde complejo pero independiente del tamaño. Si lo animas para que diga el doble de ancho, se animará usando la escala de mapa de bits, estirándose y pixelando a lo largo de la animación, incluso si tienes 'needsDisplayOnBoundsChange' YES. Solo el cuadro final se representará correctamente con 'drawInContext:'. –