2010-12-08 231 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]; 
相关问题