テキストをなんちゃって3DでトンネルっぽくしてみたかったiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *textArray;
@property (nonatomic, weak) UIView *contentView;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
[self createText];
[self createConsole];
}
– (void)createText
{
self.textArray = [NSMutableArray array];
UIView *v = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:v];
self.contentView = v;
CGPoint o = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));
float r = 80;
float dAngle = M_PI / 2.5;
for (int i=0; i<5; i++) {
float x = o.x + r * cos(dAngle * i);
float y = o.y + r * sin(dAngle * i);
UILabel *l = [[UILabel alloc] init];
l.text = @”ABCCSDFADFADSFASDFASDFASDFASDFA”;
l.font = [UIFont fontWithName:@”Menlo-Bold” size:30];
l.backgroundColor = [UIColor orangeColor];
[l sizeToFit];
l.center = CGPointMake(x, y);
[v addSubview:l];
CALayer *layer = l.layer;
CATransform3D trans = CATransform3DIdentity;
trans.m34 = 1.0 / –1000;
trans = CATransform3DRotate(trans, M_PI + dAngle * i, 0.0f, 0.0f, 1.0f);
trans = CATransform3DRotate(trans, 0.3 * M_PI, 0.0f, 1.0f, 0.0f);
layer.transform = trans;
[self.textArray addObject:l];
}
}
– (void)createConsole
{
UIView *console = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 80)];
console.center = CGPointMake(160, CGRectGetMaxY(self.view.frame) – 60);
console.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
console.layer.zPosition = 1000;
console.layer.cornerRadius = 10;
[self.view addSubview:console];
UISlider *slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 160, 23)];
slider1.center = CGPointMake(90, 20);
slider1.minimumValue = –1000;
slider1.maximumValue = –30;
slider1.value = slider1.minimumValue;
[console addSubview:slider1];
[slider1 addTarget:self action:@selector(slideA:) forControlEvents:UIControlEventValueChanged];
UIButton *turnButton = [UIButton buttonWithType:UIButtonTypeCustom];
turnButton.titleLabel.font = [UIFont boldSystemFontOfSize:30];
turnButton.titleLabel.textColor = [UIColor whiteColor];
[turnButton setTitle:@”TURN” forState:UIControlStateNormal];
[turnButton sizeToFit];
turnButton.center = CGPointMake(90, 60);
[console addSubview:turnButton];
[turnButton addTarget:self action:@selector(turn) forControlEvents:UIControlEventTouchDown];
}
– (void)turn
{
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];
rotate.fromValue = @(0);
rotate.toValue = @(2.0*M_PI);
rotate.duration = 4.0;
rotate.repeatCount = 2;
[self.contentView.layer addAnimation:rotate forKey:nil];
}
– (void)slideA:(UISlider *)slider
{
[self.textArray enumerateObjectsUsingBlock:^(UILabel *l, NSUInteger idx, BOOL *stop) {
CALayer *layer = l.layer;
float zRotation = [[layer valueForKeyPath:@”transform.rotation.z”] floatValue];
CATransform3D trans = CATransform3DIdentity;
trans.m34 = 1.0 / slider.value;
trans = CATransform3DRotate(trans, zRotation, 0.0f, 0.0f, 1.0f);
trans = CATransform3DRotate(trans, 0.3 * M_PI, 0.0f, 1.0f, 0.0f);
layer.transform = trans; }];
}
@end