2012-02-07 24 views
6

Vì vậy, tôi đang cố gắng để tạo ra một hiệu ứng gạch lật, giống như trên Windows Phone 7.Lớp Chức vụ nhảy lúc bắt đầu (Core) Animation

Cho đến nay tôi đã đoạn mã sau, nhưng tôi có một vài truy vấn.

CALayer *layer = self.theRedSquare.layer; 
CATransform3D initialTransform = self.theRedSquare.layer.transform; 
initialTransform.m34 = 1.0/-1000; 

[UIView beginAnimations:@"Scale" context:nil]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; 
layer.transform = initialTransform; 
layer.anchorPoint = CGPointMake(-0.3, 0.5); 

CATransform3D rotationAndPerspectiveTransform = self.theRedSquare.layer.transform; 

rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI , 0 , -self.theRedSquare.bounds.size.height/2, 0); 

layer.transform = rotationAndPerspectiveTransform; 

[UIView setAnimationDelegate:self]; 
[UIView commitAnimations]; 

1. Tại sao hình vuông màu đỏ của tôi (một cái nhìn giao diện người dùng đơn giản) của tôi chuyển sang bên phải khi bắt đầu các hình ảnh động?

2. Đối với điểm neo, bạn có thể đặt vị trí trên chế độ xem gốc không? Tại thời điểm này tôi tự ý đặt nó tương ứng với chế độ xem hiện tại.

Cảm ơn trước cho bất kỳ con trỏ nào.

Trước khi hoạt hình

Before

Trong phim hoạt hình (chú ý vuông đã chuyển ngay)

During

Sau hoạt hình (chú ý vuông đã chuyển bên phải)

After

Đã thêm video: example video

Trả lời

6
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view 
{ 
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y); 
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y); 

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform); 
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform); 

    CGPoint position = view.layer.position; 

    position.x -= oldPoint.x; 
    position.x += newPoint.x; 

    position.y -= oldPoint.y; 
    position.y += newPoint.y; 

    view.layer.position = position; 
    view.layer.anchorPoint = anchorPoint; 
} 

-(void)animateView:(UIView *)theRedSquare 
{ 
    CALayer *layer = theRedSquare.layer; 
    CATransform3D initialTransform = theRedSquare.layer.transform; 
    initialTransform.m34 = 1.0/-1000; 

    [self setAnchorPoint:CGPointMake(-0.3, 0.5) forView:theRedSquare]; 


    [UIView beginAnimations:@"Scale" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; 
    layer.transform = initialTransform; 


    CATransform3D rotationAndPerspectiveTransform = theRedSquare.layer.transform; 

    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI , 0 , -theRedSquare.bounds.size.height/2, 0); 

    layer.transform = rotationAndPerspectiveTransform; 

    [UIView setAnimationDelegate:self]; 
    [UIView commitAnimations]; 
} 

Taken từ này, và hơi tinh chỉnh .. Hy vọng rằng nó sẽ giúp Changing my CALayer's anchorPoint moves the view

+0

Thật tuyệt vời, cảm ơn! – JonB

+0

Làm thế nào để bạn có được hoạt hình để tạo hiệu ứng động? (Flip trong đó) Tôi đã cố gắng hàng giờ để tìm ra điều này. – Frankrockz

2

Các phạm vi chấp nhận của anchorPoint của một CALayer là [0,1]. Trong trường hợp của bạn, nơi bạn đang cố gắng lật xung quanh trục Y, điểm neo của lớp của bạn phải là [0, 0.5].

+0

tôi đã làm việc tốt này , nhưng UIView màu đỏ chuyển ngay trước khi bắt đầu hoạt ảnh. Tôi sẽ thử và tải video. Điều thú vị là -0.3 cho anchorPoint hoạt động, nó xoay quanh độ lệch trục y ở bên trái. – JonB

+0

đã thêm video để hiển thị sự cố. vào lần chạy đầu tiên của hoạt ảnh, hình vuông dịch chuyển ngay một số pixel. dường như không thể giải quyết được. – JonB

3

Nếu bạn thực hiện quảng trường quan điểm riêng của nó, lật được xây dựng-in

[UIView beginAnimations:@"flip" context:nil]; 
[UIView setAnimationDuration:.35f]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft 
    forView:self.window cache:YES]; 

// change to the new view (make this one hidden, the other not) 

[UIView commitAnimations]; 
+0

Tôi đánh giá cao nó không quá rõ ràng từ mô tả của tôi, nhưng tôi không phải sau khi lật từ chỗ. Tôi muốn lật xung quanh cạnh bên trái của hình vuông màu đỏ của tôi (mà đã là một UIView). Xem video sau đây để loại hiệu ứng tôi làm sau: http://www.youtube.com/watch?v=wgoGDPF_NGo – JonB

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