gpt4 book ai didi

objective-c - 尝试用 CALayers 制作一张可以翻转的卡片

转载 作者:可可西里 更新时间:2023-11-01 03:04:47 25 4
gpt4 key购买 nike

我一直在尝试开发一个基于 CALayer 的“CardView”对象,该对象有两个彼此背对的层以显示卡片的两侧。我一直在处理 CALayerdoublesided 属性之类的东西,发现结果令人困惑。我的基类是 CALayer,我向它添加了两个 子层,一个带有 M_PI/2 转换,两个都带有 doublesided = NO 但是就目前而言,无论我如何旋转卡片,我都会看到正面,而背面根本不显示。如果我不创建正面,则背面会从两侧显示出来,并且文本在一个角上,而不是 48 点和模糊。

这是一个屏幕截图的链接,该屏幕截图显示了卡片刚刚在 UIViewController 的 View 中完成了完整的旋转。你看到的是正面的背面。这个应该是看不见的,后面应该是露出来的……我觉得……

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

最佳答案

技巧最终是使用 CATransformLayer。 这是一个仅用于包含其他层的层(它不能有像 backgroundColorborderWidth 这样的东西)。然而,它确实保持其子层的真实 3D 关系(即,它不像常规 CALayer 那样将它们展平)。所以你可以制作两个层,翻转一个并将它的 zPosition 偏移一根头发,然后将它们都放在 CATransformLayer 中,现在你可以从星期日到 6 种方式翻转这个父层子层保持锁定在一起并始终正确渲染。非常贴心且开销低!

关于objective-c - 尝试用 CALayers 制作一张可以翻转的卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3831867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com