CATransform3DRotate 实现3维立体效果

@interface ViewController ()

@property(nonatomic,assign)CGSize viewFrameSize;

@property(nonatomic,assign)NSInteger center_y;

@property(nonatomic,assign)NSInteger center_X;

@property(nonatomic,assign)CGPoint angel;

@property(nonatomic,strong)UIView *testView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    // Do any additional setup after loading the view.

    self.viewFrameSize = self.view.bounds.size;

    self.center_y = self.viewFrameSize.height/2;

    self.center_X = self.viewFrameSize.width/2;

    self.angel = CGPointZero;

    //CATransform3DTranslate 在X、Y、Z 轴上平移

    // CATransform3DRotate 在X、Y、Z 轴上翻转 1表示变化  0不变

    

    UIView *testView = [[UIView alloc]initWithFrame:CGRectMake(0, self.center_y-50, self.viewFrameSize.width, 100)];

    testView.backgroundColor = [UIColor redColor];

    [self.view addSubview:testView];

    self.testView = testView;

    CATransform3D dictransForm = CATransform3DIdentity;

    //前

    UIImageView *imgView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];

    imgView1.backgroundColor = [UIColor blueColor];

    imgView1.frame = CGRectMake(self.center_X-50, 0, 100, 100);

    dictransForm = CATransform3DTranslate(dictransForm, 0, 0, 50);

    imgView1.layer.transform = dictransForm;

 //后    

    UIImageView *imgView6 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2"]];

    imgView6.backgroundColor = [UIColor redColor];

    imgView6.frame = CGRectMake(self.center_X-50, 0, 100, 100);

    dictransForm = CATransform3DTranslate(dictransForm, 0, 0, -100);

    imgView6.layer.transform = dictransForm;

 //左    

    UIImageView *imgView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2"]];

    imgView2.frame = CGRectMake(self.center_X-50, 0, 100, 100);

    imgView2.backgroundColor = [UIColor yellowColor];

    dictransForm = CATransform3DRotate(CATransform3DIdentity, -M_PI_2, 0, 1, 0);

    

    dictransForm = CATransform3DTranslate(dictransForm, 0, 0, 50);

   

    imgView2.layer.transform = dictransForm;

    // 右

       UIImageView *imgView5 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2"]];

       imgView5.frame = CGRectMake(self.center_X-50, 0, 100, 100);

    imgView5.backgroundColor = [UIColor linkColor];

    dictransForm = CATransform3DRotate(CATransform3DIdentity, -M_PI_2, 0, 1, 0);

    dictransForm = CATransform3DTranslate(dictransForm, 0, 0, -50);

      imgView5.layer.transform = dictransForm;

       

       // 下

       UIImageView *imgView3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];

       imgView3.frame = CGRectMake(self.center_X-50, 0, 100, 100);

    imgView3.backgroundColor = [UIColor purpleColor];

    dictransForm = CATransform3DRotate(CATransform3DIdentity, -M_PI_2, 1, 0, 0);

    dictransForm = CATransform3DTranslate(dictransForm, 0, 0, 50);

       imgView3.layer.transform = dictransForm;

       

       // 上

       UIImageView *imgView4 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2"]];

       imgView4.frame = CGRectMake(self.center_X-50, 0, 100, 100);

    imgView4.backgroundColor = [UIColor grayColor];

    dictransForm = CATransform3DRotate(CATransform3DIdentity, M_PI_2, 1, 0, 0);

    dictransForm = CATransform3DTranslate(dictransForm, 0, 0, 50);

       imgView4.layer.transform = dictransForm;

       

    [testView addSubview:imgView1];

    [testView addSubview:imgView2];

    [testView addSubview:imgView3];

    [testView addSubview:imgView4];

    [testView addSubview:imgView5];

    [testView addSubview:imgView6];

    

    

    

    UIPanGestureRecognizer *panGetster = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

    [testView addGestureRecognizer: panGetster];

}

- (void)panAction:(UIPanGestureRecognizer *)panGesture{

    

    CGPoint point = [panGesture translationInView:self.testView];

    CGFloat angel_x = self.angel.x + point.x/30;

    CGFloat angel_y = self.angel.y - point.y/30;

    

    CATransform3D transform = CATransform3DIdentity;

    transform.m34 = -1 /500;

    transform = CATransform3DRotate(transform,angel_x , 0, 1, 0);

    transform = CATransform3DRotate(transform, angel_y, 1, 0, 0);

    self.testView.layer.sublayerTransform = transform;

    

    if (panGesture.state == UIGestureRecognizerStateEnded) {

        self.angel = CGPointMake(angel_x, angel_y);

    }

    

}

@end

​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值