2010-12-08 52 views

回答

3

假設您想從右側推view2來替換view1。

// Set up view2 
view2.frame = view1.frame; 
view2.center = CGPointMake(view1.center.x + CGRectGetWidth(view1.frame), view1.center.y); 
[view1.superview addSubview: view2]; 
// Animate the push 
[UIView beginAnimations: nil context: NULL]; 
[UIView setAnimationDelegate: self]; 
[UIView setAnimationDidStopSelector: @selector(pushAnimationDidStop:finished:context:)]; 
view2.center = view1.center; 
view1.center = CGPointMake(view1.center.x - CGRectGetWidth(view1.frame), view1.center.y); 
[UIView commitAnimations]; 

然後(任選地)實現此方法,從視圖層次結構中刪除廠景:

- (void) pushAnimationDidStop: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context { 
    [view1 removeFromSuperview]; 
} 

在你也可能希望釋放廠景,並根據設置其參照零,該動畫委託方法是否需要在轉換後保持它。

+0

謝謝你的回答。非常感謝....... – Pugal 2010-12-08 13:39:46

1

要從左動畫到右邊可以使用以下代碼塊

CATransition *transition = [CATransition animation]; 
    transition.duration = 0.4; 
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromLeft; 
    [self.view.window.layer addAnimation:transition forKey:nil]; 
    [self presentViewController:YOUR_VIEWCONTROLLER animated:YES completion:nil]; 
1

另一種選擇是使用的動畫塊與碼的較小的線並且爲簡化:

下面是示例

CGRect viewLeftRect;//final frames for left view 
CGRect viewRightRect;//final frames for right view 

[UIView animateWithDuration:0.3f animations:^{ 
    [viewLeft setFrame:viewLeftRect]; 
    [viewRight setFrame:viewRightRect]; 
} completion:^(BOOL finished) { 
    //do what ever after completing animation 
}]; 
0

您也可以從右動畫添加到左是這樣的:

scrAnimation.frame=CGRectMake(248, 175, 500, 414); //your view start position 

    [UIView animateWithDuration:0.5f 
          delay:0.0f 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         [scrAnimation setFrame:CGRectMake(0, 175, 500, 414)]; // last position 
        } 
        completion:nil]; 
相關問題