美文网首页CoreAnimation
CoreAnimation 实现立方体旋转

CoreAnimation 实现立方体旋转

作者: MonKey_Money | 来源:发表于2020-07-25 22:14 被阅读0次

实现的效果

res.gif

创建各个layer

-(CALayer *)makeLayerWithTranform:(CATransform3D)transform {
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 100, 100);
    layer.contents =(__bridge id) [UIImage imageNamed:@"huhu.jpg"].CGImage;
    layer.transform = transform;
    return layer;   
}

分别添加6个面

CATransformLayer *transformLayer = [CATransformLayer layer];
    transformLayer.position = self.view.center;
    //正前方

    CATransform3D tranform3D = CATransform3DMakeTranslation(0, 0, 50);
    [transformLayer addSublayer:[self makeLayerWithTranform:tranform3D]];
    //背后
    tranform3D = CATransform3DMakeTranslation(0, 0, -50);
  tranform3D = CATransform3DRotate(tranform3D, M_PI, 0, 1, 0);
    [transformLayer addSublayer:[self makeLayerWithTranform:tranform3D]];
    
    //左边
    tranform3D = CATransform3DMakeTranslation(-50, 0, 0);
    tranform3D = CATransform3DRotate(tranform3D, M_PI_2, 0, 1, 0);
    [transformLayer addSublayer:[self makeLayerWithTranform:tranform3D]];

    //右边
    tranform3D = CATransform3DMakeTranslation(50, 0, 0);
    tranform3D = CATransform3DRotate(tranform3D, -M_PI_2, 0, 1, 0);
    [transformLayer addSublayer:[self makeLayerWithTranform:tranform3D]];
    
    //上面
    tranform3D = CATransform3DMakeTranslation(0, 50, 0);
  tranform3D=  CATransform3DRotate(tranform3D, -M_PI_2, 1, 0, 0);
    [transformLayer addSublayer:[self makeLayerWithTranform:tranform3D]];
    //下面
    tranform3D = CATransform3DMakeTranslation(0, -50, 0);
   tranform3D=    CATransform3DRotate(tranform3D, M_PI_2, 1, 0, 0);
    [transformLayer addSublayer:[self makeLayerWithTranform:tranform3D]];
    [self.view.layer addSublayer:transformLayer];

实现动画效果

{ CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate:)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
//方法
-(void)rotate:(CADisplayLink *)link {
    static float angle = 0;
    angle += 5;
    self.transformLayer.sublayerTransform = CATransform3DMakeRotation(angle*M_PI_2/90, 1, 1, 1);
}

相关文章

网友评论

    本文标题:CoreAnimation 实现立方体旋转

    本文链接:https://www.haomeiwen.com/subject/mlqvlktx.html