How to rotate an UIImageView with CATransform3DRotate make an effect like Door Opening?

折月煮酒 提交于 2019-11-28 19:38:21
jtbandes

Use CATransform3DRotate like you did in combination with CATransform3DTranslate to rotate around the edge, like I did in this answer. Here's a little excerpt (you will need to modify this for your own uses):

CATransform3D t = CATransform3DIdentity;
t = CATransform3DTranslate(t, 0, -self.view.bounds.size.height/2, 0);
t = CATransform3DRotate(t, rec.scale * M_PI, 1, 0, 0);
t = CATransform3DTranslate(t, 0, -self.view.bounds.size.height/2, 0);
self.view.layer.transform = t;
Saurabh
- (void)awakeFromNib {

  transformed = [CALayer layer];

  transformed.frame = self.bounds;

  [self.layer addSublayer:transformed];

  CALayer *imageLayer = [CALayer layer];

  imageLayer.frame = CGRectMake(10.0f, 4.0f, 300.0f, 226.0f);

  imageLayer.transform = CATransform3DMakeRotation(20.0f * M_PI / 180.0f,
                                                   1.0f, 0.0f, 0.0f);

  imageLayer.contents = (id)[[UIImage imageNamed:@"IMG_0051.png"] CGImage];

  [transformed addSublayer:imageLayer];

  imageLayer = [CALayer layer];

  imageLayer.frame = CGRectMake(10.0f, 234.0f, 300.0f, 226.0f);

  imageLayer.transform = CATransform3DMakeRotation(-20.0f * M_PI / 180.0f,
                                                   1.0f, 0.0f, 0.0f);

  imageLayer.contents = (id)[[UIImage imageNamed:@"IMG_0089.png"] CGImage];

  [transformed addSublayer:imageLayer];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    previousLocation = [[touches anyObject] locationInView:self];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

  CGPoint location = [[touches anyObject] locationInView:self];

    // BJL: The following is the code I used in Molecules to do 3-D rotation

    CATransform3D currentTransform = transformed.sublayerTransform;

    CGFloat displacementInX = location.x - previousLocation.x;

    CGFloat displacementInY = previousLocation.y - location.y;


    CGFloat totalRotation = sqrt(displacementInX * displacementInX + displacementInY * displacementInY);

    CATransform3D rotationalTransform = CATransform3DRotate(currentTransform, totalRotation * M_PI / 180.0, 
                                                                    ((displacementInX/totalRotation) * currentTransform.m12 + (displacementInY/totalRotation) * currentTransform.m11), 
                                                                    ((displacementInX/totalRotation) * currentTransform.m22 + (displacementInY/totalRotation) * currentTransform.m21), 
                                                                    ((displacementInX/totalRotation) * currentTransform.m32 + (displacementInY/totalRotation) * currentTransform.m31));

    previousLocation = location;

  transformed.sublayerTransform = rotationalTransform;

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!