1
基於較早的stackoverflow建議,我試圖創建一個使用CALayers添加到CATransformLayer的紙牌。這個想法是創建正面和背面的CALayers,並將它們結合在CATransformLayer中,然後可以旋轉,翻轉等,並自動顯示正確的一面。下面是示例代碼,試圖創建一個綠色正面和紅色背面的單卡。結合和翻轉CALayers作爲CATransformLayer的子視圖
紅背圍繞Y軸翻轉,使其背離綠色正面。綠色前線玩家的Z位置高於紅色背部。
當我進行轉換時,我只是看到卡看起來是非翻轉狀態。任何想法,我哪裏錯了?
#define DEGREES_TO_RADIANS(angle) (angle * M_PI/180.0)
- (void)viewDidLoad
{
[super viewDidLoad];
CATransformLayer *cardContainer = [CATransformLayer layer];
cardContainer.bounds = CGRectMake(0,0, 150,200);
CALayer *cardFront = [CALayer layer];
cardFront.frame = cardContainer.bounds;
cardFront.backgroundColor = [UIColor greenColor].CGColor;
cardFront.borderColor = [UIColor blackColor].CGColor;
cardFront.borderWidth = 2.0;
cardFront.cornerRadius = 30.0;
cardFront.zPosition = 2; // Put front of card on top relative to back of card
cardFront.doubleSided = NO;
[cardContainer addSublayer:cardFront];
CALayer *cardBack = [CALayer layer];
cardBack.frame = cardContainer.bounds;
cardBack.backgroundColor = [UIColor redColor].CGColor;
cardBack.zPosition = 1;
cardBack.doubleSided = NO;
// Flip cardBack image so it is facing outward and visible when flipped
cardBack.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(180),0.0,1.0,0.0);
[cardContainer addSublayer:cardBack];
UIView* cardView = [[UIView alloc] initWithFrame:cardContainer.bounds];
cardView.center = CGPointMake(self.view.center.x, self.view.center.y);
[cardView.layer addSublayer:cardContainer];
[self.view addSubview:cardView];
// Show the card flipped over (desired the red side to be showing, but instead shows green)
cardView.layer.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(180),0.0,1.0,0.0);
}