2011-03-04 69 views
4

我想做一個頁面翻轉動畫,並在互聯網上找到這個代碼。這工作完美。它從右向左轉動頁面,就像普通書一樣。我想修改代碼,使其從左到右,但我無法弄清楚錨點是如何工作的。請注意我認爲需要改變的兩條線。我只是不知道是什麼......IOS CATransform3DMakeRotation需要幫助矢量

viewToOpen.layer.anchorPoint = CGPointMake(0.0f, 0.5f); 
      viewToOpen.center = CGPointMake(viewToOpen.center.x - viewToOpen.bounds.size.width/2.0f, viewToOpen.center.y); 

CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f, 
                   0.0f, 
                   -1.0f, 
                   0.0f); 

全功能瀏覽:

- (void) pageOpenView:(UIView *)viewToOpen duration:(NSTimeInterval)duration { 
    // Remove existing animations before stating new animation 
    [viewToOpen.layer removeAllAnimations]; 

    // Make sure view is visible 
    viewToOpen.hidden = NO; 

    // disable the view so it’s not doing anythign while animating 
    viewToOpen.userInteractionEnabled = NO; 
    // Set the CALayer anchorPoint to the left edge and 
    // translate the button to account for the new 
    // anchorPoint. In case you want to reuse the animation 
    // for this button, we only do the translation and 
    // anchor point setting once. 
    if (viewToOpen.layer.anchorPoint.x != 0.0f) { 
     viewToOpen.layer.anchorPoint = CGPointMake(0.0f, 0.5f); 
     viewToOpen.center = CGPointMake(viewToOpen.center.x - viewToOpen.bounds.size.width/2.0f, viewToOpen.center.y); 
    } 
    // create an animation to hold the page turning 
    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    transformAnimation.removedOnCompletion = NO; 
    transformAnimation.duration = duration; 
    transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    // start the animation from the current state 
    transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
    // this is the basic rotation by 90 degree along the y-axis 
    CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f, 
                  0.0f, 
                  -1.0f, 
                  0.0f); 
    // these values control the 3D projection outlook 
    endTransform.m34 = 0.001f; 
    endTransform.m14 = -0.0015f; 
    transformAnimation.toValue = [NSValue valueWithCATransform3D:endTransform]; 
    // Create an animation group to hold the rotation 
    CAAnimationGroup *theGroup = [CAAnimationGroup animation]; 

    // Set self as the delegate to receive notification when the animation finishes 
    theGroup.delegate = self; 
    theGroup.duration = duration; 
    // CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag 
    // to identify the animation later when it finishes 
    [theGroup setValue:[NSNumber numberWithInt:viewToOpen.tag] forKey:@"viewToOpenTag"]; 
    // Here you could add other animations to the array 
    theGroup.animations = [NSArray arrayWithObjects:transformAnimation, nil]; 
    theGroup.removedOnCompletion = NO; 
    // Add the animation group to the layer 
    [viewToOpen.layer addAnimation:theGroup forKey:@"flipViewOpen"]; 
} 
+0

我是否需要更改錨點和中心? – 2011-03-04 15:09:29

回答

1

這可能取決於層和視圖設置,但本質上

viewToOpen.layer.anchorPoint = CGPointMake(1.0f, 0.5f); 
CATransform3D endTransform = CATransform3DMakeRotation(-M_PI/2.0f,0.0f,1.0f,0.0f) 

代碼完整如下:

self.layer.anchorPoint = CGPointMake(1.0f, 0.5f); 
self.layer.position = CGPointMake(self.layer.position.x + self.bounds.size.width/2.0f, self.layer.position.y); 
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
animation.duration = 0.75f; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
CATransform3D tfm = CATransform3DMakeRotation(M_PI/2.0f, 0.0f, -1.0f, 0.0f); 
tfm.m34 = 0.001f; 
tfm.m14 = -0.0015f; 
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
animation.toValue = [NSValue valueWithCATransform3D:tfm]; 
[self.layer addAnimation:animation forKey:@"flipUp"]; 
+0

這不起作用,它將視圖向右移動,並向後移動。 – 2011-03-04 20:47:01

+0

當然,如果您沒有使用視圖的「中心」屬性(如示例中)或圖層的「位置」屬性更新視圖的位置。 – MHC 2011-03-07 15:24:36

+0

查看編輯答案。 – MHC 2011-03-07 15:31:21