2011-06-30 31 views
13

Tôi đã đọc và cố gắng này article (Opening door effect using Core Animation)Làm thế nào để xoay một UIImageView với CATransform3DRotate làm cho một hiệu ứng như mở cửa?

Và tôi thực hiện sau mã trong ứng dụng của tôi:

CALayer *layer = threeHomeView.layer; 
CATransform3D initialTransform = threeHomeView.layer.transform; 
initialTransform.m34 = 1.0/-900; 

[UIView beginAnimations:@"Scale" context:nil]; 
[UIView setAnimationDuration:3]; 
layer.transform = initialTransform; 
CATransform3D rotationAndPerspectiveTransform = threeHomeView.layer.transform; 

rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform 
                 , 40.0f * M_PI/180.0f 
                 , 0.0f 
                 , 1.0f 
                 , 0.0f); 
layer.transform = rotationAndPerspectiveTransform; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(threeHomeFlyOut)]; 
[UIView commitAnimations]; 

threeHomeView là một UIImageView.

CÂU HỎI CỦA TÔI LÀ: hình ảnh chỉ có thể xoay theo đường thẳng đứng giữa của hình ảnh, nhưng tôi muốn nó xoay theo đường thẳng đứng bên trái của hình ảnh.

+0

Tôi gặp cùng một vấn đề và cố gắng thiết lập một hướng dẫn đúng đắn/giải thích sâu sắc hơn ở đây: http: // stackoverflow.com/questions/7685547/3d-door-open-animation-between-two-uiviewcontrollers –

Trả lời

15

Sử dụng CATransform3DRotate giống như bạn đã kết hợp với CATransform3DTranslate để xoay quanh cạnh, như tôi đã làm trong this answer. Dưới đây là một đoạn trích nhỏ (bạn sẽ cần phải sửa đổi này cho mục đích riêng của bạn):

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; 
+0

Cảm ơn, nó chỉ hoạt động! – ZYiOS

+0

@jtbandes Tôi đã cố gắng áp dụng điều này cho hoạt ảnh 3D Door Open, nhưng nó sẽ không thực tế. Tôi có mã mẫu của tôi ở đây: http://stackoverflow.com/questions/7685547/3d-door-open-animation-between-two-uiviewcontrollers Bạn có thể rất tử tế và có giao diện nhanh không? –

+0

Chúng ta có thể đặt điểm neo vào cạnh cần thiết và thực hiện xoay vòng mà không cần dịch, phải không? – dmitrynikolaev

3
- (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; 

} 
Các vấn đề liên quan