Ho cercato di sviluppare un oggetto "CardView" basato su CALayer
che presenta due livelli rivolti l'uno verso l'altro per presentare i due lati di una scheda. Ho lavorato con cose come la proprietà doublesided
dello CALayer
e trovo i risultati confusi. La mia classe base è CALayer
e sto aggiungendo due sublayers
ad essa, uno con una trasformazione M_PI/2 ed entrambi con doublesided = NO
ma così com'è, ottengo la parte anteriore che mostra non importa come ruoto la scheda e il retro non mostra affatto. Se non creo il fronte, la parte posteriore mostra entrambi i lati e il testo è in un angolo, non a 48 punti e sfocato.Tentativo di creare una scheda da CALayers che può capovolgere
Ecco un collegamento a uno screenshot che mostra la scheda che ha appena terminato un giro completo in una vista UIViewController's
. Stai vedendo il retro del fronte. Questo dovrebbe essere invisibile, e la parte posteriore dovrebbe essere mostrando ... credo ...
https://files.me.com/gazelips/lj2aqo
// CardView.h
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#define kCardWidth 100.0f
#define kCardHeight 150.0f
@interface CardView : CALayer {
CALayer *cardFront, *cardBack;
}
@property (nonatomic, retain) CALayer *cardFront, *cardBack;
- (id)initWithPosition:(CGPoint)point;
- (void)addPerspective;
- (CALayer *)createFront;
- (CALayer *)createBack;
@end
// CardView.m
#import "CardView.h"
@implementation CardView
@synthesize cardFront, cardBack;
static CATransform3D kPerspectiveTransform;
- (id)initWithPosition:(CGPoint)point {
NSLog(@"--initWithPosition:CardView");
self = [super init];
if (self != nil) {
[self addPerspective];
self.bounds = CGRectMake(0, 0, kCardWidth, kCardHeight);
self.position = point;
self.edgeAntialiasingMask = 0;
self.backgroundColor = [[UIColor clearColor] CGColor];
self.borderColor = [[UIColor blackColor] CGColor];
self.borderWidth = 0.5;
self.doubleSided = YES;
cardBack = [self createBack];
[self addSublayer: cardBack];
cardFront = [self createFront];
[self addSublayer: cardFront];
}
return self;
}
- (void)addPerspective {
NSLog(@"--prepare:CardView");
kPerspectiveTransform = CATransform3DIdentity;
kPerspectiveTransform.m34 = -1.0/800.0;
self.transform = kPerspectiveTransform;
}
- (CALayer *)createFront {
NSLog(@"--createFront:CardView");
CALayer *front = [[CALayer alloc] init];
front.bounds = CGRectMake(0.0f, 0.0f, kCardWidth, kCardHeight);
front.position = CGPointMake(kCardWidth/2, kCardHeight/2);
front.edgeAntialiasingMask = 0;
front.backgroundColor = [[UIColor whiteColor] CGColor];
front.cornerRadius = 8 * (kCardHeight/150);
front.borderWidth = 1;
front.borderColor = [[UIColor grayColor] CGColor];
front.doubleSided = NO;
return [front autorelease];
}
- (CALayer *)createBack {
NSLog(@"--createBack:CardView");
CALayer *back = [[CALayer alloc] init];
back.bounds = CGRectMake(0.0f, 0.0f, kCardWidth, kCardHeight);
back.position = CGPointMake(kCardWidth/2, kCardHeight/2);
back.backgroundColor = [[UIColor blueColor] CGColor];
back.contentsGravity = kCAGravityResize;
back.masksToBounds = YES;
back.borderWidth = 4 * (kCardHeight/150);
back.borderColor = [[UIColor grayColor] CGColor];;
back.cornerRadius = 8 * (kCardHeight/150);
back.edgeAntialiasingMask = 0;
back.doubleSided = NO;
CATextLayer *textLayer = [CATextLayer layer];
textLayer.font = [UIFont boldSystemFontOfSize:48];
textLayer.bounds = CGRectMake(0.0f, 0.0f, kCardWidth, kCardHeight);
textLayer.position = CGPointMake(50.0f, 75.0f);
textLayer.string = @"Steve";
[back addSublayer:textLayer];
back.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
return [back autorelease];
}
@end
Se stai cercando un bel tutorial su questo, questo mi ha davvero aiutato: http://invasivecode.tumblr.com/post/29307073330/core-animation-transform-layer – Marc
Ho dovuto sopravvalutare questo non solo perché è il risposta corretta ma anche per l'utilizzo di "sei modi da domenica" in esso – ribeto