0

I'm reading a book on animations iOS Core Animation: Advanced Techniques. The book is in Objective C. I'm not fluent in Objective C. I understand what the .m34 property does but when I apply the book's code to Swift the perspective isn't changing. The problem is I cannot seem to add to an existing transform like explained here.

My code:

var tranform = CATransform3DIdentiy
transfrom.m34 = -1 / 500
transform = CATransform3DMakerotation(CGFloat(Double.pi/4), 0, 1, 0) // this is just creating a new transform instead of adding to the existing one
viewIwantTransformed.layer.transfrom = transform

Books Code:

@implementation ViewController 
- (void) viewDidLoad 

 { [super viewDidLoad]; 

   // create a new transform CATransform3D 
   transform = CATransform3DIdentity;

   // apply perspective 
   transform.m34 = - 1.0 / 500.0; 

  // rotate by 45 degrees along the Y axis
  transform = CATransform3DRotate( transform, M_PI_4, 0, 1, 0);

  // apply to layer 
  self.viewIwantTransformed.layer.transform = transform; 

} @end

Result

enter image description here

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

1

try this

transform = CATransform3DRotate(transform, M_PI_4, 0, 1, 0) 

instead by

transform = CATransform3DMakerotation(CGFloat(Double.pi/4, 0, 1, 0)
Jigar
  • 1,801
  • 1
  • 14
  • 29
  • Swift 4 says to use Double.pi instead of M_PI. The answer is correct either way though, thanks. Here is what the warning suggested, Change M_PI_4 to Double.pi/4. So the code would be transform = CATransform3DRotate(transform, CGFloat(Double.pi/4), 0, 1, 0) – Lance Samaria May 31 '18 at 13:34
  • hmm there is some syntax warning – Jigar May 31 '18 at 13:36