2016-06-13 8 views
6

1つの3Dブロックを描画しようとしています!CATransform3Dを使用してCATransformLayer iOSで右から左にパースペクティブアングルを変更

2層を使用しています。の右側がのように見えます。添付の画像を参照してください。

enter image description here私は左側のマイナー修正のために同じコードを再利用しようとしています!それは奇妙に見えます。私たちは視点角度を変更することができれば、我々は改善できると感じ、

enter image description here

を添付画像怒鳴るを参照してください、それを達成する方法がわかりません。

transform.m34を使用します。どんな助けでも大歓迎です。

ここに私が使用しているソースコードがあります。

// left Bar 
/*{ 
    CGFloat aViewWidth = 1000; 

    CAGradientLayer *blueLayer = [CAGradientLayer layer]; 
    blueLayer.anchorPoint = P(0,0.5); 

    blueLayer.colors = @[ 
         (id)aBarColor.CGColor, 
         (id)[UIColor blackColor].CGColor 
         ]; 
    blueLayer.startPoint = CGPointMake(0.0, 0.5); 
    blueLayer.endPoint = CGPointMake(1.5, 0.5); 

    blueLayer.frame = CGRectMake(125, 0, aViewWidth, 250); 
    blueLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI_2, 0.0f, 1.0f, 0.0f); 


    [baseLayer addSublayer:blueLayer]; 

    CAGradientLayer *redLayer = [CAGradientLayer layer]; 

    redLayer.colors = @[ 
         (id)[UIColor whiteColor].CGColor, 
         (id)aBarColor.CGColor 
         ]; 
    redLayer.startPoint = CGPointMake(-10.0, 0.5); 
    redLayer.endPoint = CGPointMake(1.0, 0.5); 
    redLayer.anchorPoint = P(0.5,0.5); 

    redLayer.frame = CGRectMake(0, 0, 125, 250); 

    [baseLayer addSublayer:redLayer]; 

    CGFloat perspective = -1000; //This relates to the m34 perspective matrix. 

    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
    rotationAndPerspectiveTransform.m34 = 1.0/perspective; 
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, degreesToRadians(-10.0) , 0.0f, 1.0f, 0.0f); 

    baseLayer.sublayerTransform = rotationAndPerspectiveTransform; 

    [self.view.layer addSublayer:baseLayer]; 

    return; 

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"sublayerTransform"]; 
    transformAnimation.fillMode = kCAFillModeForwards; 
    transformAnimation.removedOnCompletion = NO; 
    { 

     CGFloat w0 = 0; 
     CGFloat w1 = aViewWidth; 

     w0 = w0 * sin(degreesToRadians(10)) - perspective * cos(degreesToRadians(10)); 
     w1 = w1 * sin(degreesToRadians(10)) - perspective * cos(degreesToRadians(10)); 

     CGFloat newWidth = w1 - w0; 
     newWidth = newWidth * 1.2; 

     CATransform3D newTransform = CATransform3DIdentity; 
     newTransform.m34 = 1.0/perspective; 
     newTransform = CATransform3DRotate(newTransform, degreesToRadians(-10) , 0.0f, 1.0f, 0.0f); 
     newTransform = CATransform3DScale(newTransform, 1.0f, 1.0f, 1.0f); 
     newTransform = CATransform3DTranslate(newTransform, newWidth, 0.0f, 0.0f); 

     transformAnimation.toValue = [NSValue valueWithCATransform3D:newTransform]; 
     transformAnimation.duration = 10.0; 
    } 
    [baseLayer addAnimation:transformAnimation forKey:@"transform"]; 
}*/ 

// Right Bar 
{ 
    CGFloat aViewWidth = 1000; 

    CAGradientLayer *redLayer = [CAGradientLayer layer]; 

    redLayer.colors = @[ 
         (id)aBarColor.CGColor, 
         (id)[UIColor blackColor].CGColor 
        ]; 
    redLayer.startPoint = CGPointMake(0.0, 0.5); 
    redLayer.endPoint = CGPointMake(1.5, 0.5); 

    redLayer.frame = CGRectMake(0, 0, aViewWidth, 250); 
    redLayer.position = CGPointMake(0,0); 
    redLayer.anchorPoint = CGPointMake(0, 0.5); // right 
    redLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI_2,0.0f, 1.0f, 0.0f); 

    [baseLayer addSublayer:redLayer]; 

    CAGradientLayer *blueLayer = [CAGradientLayer layer]; 

    blueLayer.colors = @[ 
         (id)[UIColor whiteColor].CGColor, 
         (id)aBarColor.CGColor 
         ]; 
    blueLayer.startPoint = CGPointMake(-10.0, 0.5); 
    blueLayer.endPoint = CGPointMake(1.0, 0.5); 

    blueLayer.frame = CGRectMake(0, 0, 125, 250); 
    blueLayer.anchorPoint = CGPointMake(0, 0.5); // right 
    blueLayer.position = CGPointMake(0,0); 

    [baseLayer addSublayer:blueLayer]; 

    CGFloat perspective = -1 * aViewWidth; //This relates to the m34 perspective matrix. 

    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
    rotationAndPerspectiveTransform.m34 = 1.0/perspective; 
    rotationAndPerspectiveTransform = CATransform3DScale(rotationAndPerspectiveTransform, 0.1f, 0.1f, 0.1f); 

    baseLayer.sublayerTransform = rotationAndPerspectiveTransform; 

    [self.view.layer addSublayer:baseLayer]; 

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"sublayerTransform"]; 
    transformAnimation.fillMode = kCAFillModeForwards; 
    transformAnimation.removedOnCompletion = NO; 
    { 

     CGFloat w0 = 0; 
     CGFloat w1 = aViewWidth; 

     w0 = w0 * sin(degreesToRadians(10)) - perspective * cos(degreesToRadians(10)); 
     w1 = w1 * sin(degreesToRadians(10)) - perspective * cos(degreesToRadians(10)); 

     CGFloat newWidth = w1 - w0; 
     newWidth = newWidth * 1.225; 

     CATransform3D newTransform = CATransform3DIdentity; 
     newTransform.m34 = 1.0/perspective; 
     newTransform = CATransform3DRotate(newTransform, degreesToRadians(10) , 0.0f, 1.0f, 0.0f); 
     newTransform = CATransform3DScale(newTransform, 1.0f, 1.0f, 1.0f); 
     newTransform = CATransform3DTranslate(newTransform, newWidth, 0.0f, 0.0f); 

     transformAnimation.toValue = [NSValue valueWithCATransform3D:newTransform]; 
     transformAnimation.duration = 10.0; 
    } 
    [baseLayer addAnimation:transformAnimation forKey:@"transform"]; 
} 

答えて

5

ここで作業コードである:

// left Bar 
{ 
CGFloat aViewWidth = 1000; 

CAGradientLayer *blueLayer = [CAGradientLayer layer]; 
blueLayer.anchorPoint = P(0,0.5); 

blueLayer.colors = @[ 
(id)aBarColor.CGColor, 
(id)[UIColor blackColor].CGColor 
]; 
blueLayer.startPoint = CGPointMake(0.0, 0.5); 
blueLayer.endPoint = CGPointMake(1.5, 0.5); 

blueLayer.frame = CGRectMake(125, 0, aViewWidth, 250); 
blueLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI_2, 0.0f, 1.0f, 0.0f); 


[baseLayer addSublayer:blueLayer]; 

CAGradientLayer *redLayer = [CAGradientLayer layer]; 

redLayer.colors = @[ 
(id)[UIColor whiteColor].CGColor, 
(id)aBarColor.CGColor 
]; 
redLayer.startPoint = CGPointMake(-10.0, 0.5); 
redLayer.endPoint = CGPointMake(1.0, 0.5); 

redLayer.anchorPoint = P(1.0,0.5)が、

redLayer.frame = CGRectMake(0, 0, 125, 250); 

[baseLayer addSublayer:redLayer]; 

CGFloat perspective = -1000; //This relates to the m34 perspective matrix. 
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
rotationAndPerspectiveTransform.m34 = 1.0/perspective; 
rotationAndPerspectiveTransform = CATransform3DScale(rotationAndPerspectiveTransform, 0.1f, 0.1f, 0.1f); 
baseLayer.sublayerTransform = rotationAndPerspectiveTransform; 

[self.view.layer addSublayer:baseLayer]; 

CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"sublayerTransform"]; 
transformAnimation.fillMode = kCAFillModeForwards; 
transformAnimation.removedOnCompletion = NO; 
{ 

CGFloat w0 = 0; 
CGFloat w1 = aViewWidth; 
w0 = w0 * sin(degreesToRadians(-10)) - perspective * cos(degreesToRadians(-10)); 
w1 = w1 * sin(degreesToRadians(-10)) - perspective * cos(degreesToRadians(-10)); 
CGFloat newWidth = w1 - w0; 
newWidth = newWidth * 1.2; 

CATransform3D newTransform = CATransform3DIdentity; 
newTransform.m34 = 1.0/perspective; 
newTransform = CATransform3DRotate(newTransform, degreesToRadians(-10) , 0.0f, 1.0f, 0.0f); 
newTransform = CATransform3DScale(newTransform, 1.0f, 1.0f, 1.0f); 
newTransform = CATransform3DTranslate(newTransform, newWidth, 0.0f, 0.0f); 

transformAnimation.toValue = [NSValue valueWithCATransform3D:newTransform]; 
transformAnimation.duration = 10.0; 
} 
[baseLayer addAnimation:transformAnimation forKey:@"transform"]; 
} 

あなたredLayer ancherPoint to (1.0, 0.5)を変更し、負の値を使用しますw0とw1でうろつきます。newWidth(翻訳に使用される)変数は、左側のアニメーションでは負の値、右側のアニメーションでは正の値にする必要があります。

関連する問題