2012-02-07 30 views
6

所以,我试图创建一个瓷砖翻转效果,就像在Windows Phone 7层位置在(核心)动画开始跳跃

到目前为止,我有以下的代码,但我有一对夫妇查询。

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.为什么我的我的红方(一个简单的用户界面视图)转换为在动画的开始吗?

2.对于锚点,是否可以在父视图上设置一个位置?目前我相对于目前的观点任意设定它。

在此先感谢您的任何指针。

动画

Before

在动画之前(注意广场上有右移)

During

动画后(注意方已转向右)

After

视频添加:example video

回答

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]; 
} 

从其中取出,并调整了小幅..希望这有助于 Changing my CALayer's anchorPoint moves the view

+0

非常棒,谢谢! – JonB 2012-02-08 18:15:43

+0

如何让动画在动画中生成? (翻转)我已经尝试了几个小时来弄明白这一点。 – Frankrockz 2012-05-05 18:33:47

2

一个CALayeranchorPoint的可接受范围是[0,1]。在你的情况下,你试图围绕Y轴翻转,你的图层的锚点应该是[0,0.5]。

+0

我有这个工作正常,但是即使在开始动画之前,红色的UIView也正好移动。我会试着去看一段视频。有趣的是,anchorPoint的工作方式为-0.3,它围绕y轴偏移向左旋转。 – JonB 2012-02-07 23:18:44

+0

已添加视频以显示该问题。在动画的第一次运行中,正方形右移许多像素。似乎无法解决问题。 – JonB 2012-02-07 23:50:28

3

如果您平方自己的看法,翻转是内置

[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

我很欣赏它从我的描述中不太明显,但我并不是从现场翻转过来的。我想翻转我的红色方块的左边缘(这已经是一个UIView)。请参阅以下视频来了解我的效果:http://www.youtube.com/watch?v=wgoGDPF_NGo – JonB 2012-02-07 23:15:24