TransitionViewController.h文件: #import <UIKit/UIKit.h> @interface TransitionViewController : UIViewController { UIView *_containerView; UIImageView *_firstView; UIImageView *_secondView; UIButton *_flip; UIButton *_curl; } @property (nonatomic, retain) UIView *containerView; @property (nonatomic, retain) UIImageView *firstView; @property (nonatomic, retain) UIImageView *secondView; @property (nonatomic, retain) UIButton *flip; @property (nonatomic, retain) UIButton *curl; @end
TransitionViewController.m @synthesize containerView = _containerView; @synthesize firstView = _firstView; @synthesize secondView = _secondView; @synthesize flip = _flip; @synthesize curl = _curl; - (void)buttonAction: (id)sender { UIButton *btnActivity = (UIButton *)sender; // 开始动画 [UIView beginAnimations:nil context:NULL]; // 设置动画时间 [UIView setAnimationDuration:0.75f]; if([btnActivity.currentTitle isEqualToString:@"Flip"]) { // 旋转动画 // 选择旋转的方向 [UIView setAnimationTransition:([self.firstView superview]? UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight) forView:self.containerView cache:YES]; // [UIView setAnimationTransition:( // UIViewAnimationTransitionFlipFromLeft) // forView:self.containerView cache:YES]; } else { // 卷曲动画 // 选择卷曲的方向 [UIView setAnimationTransition:([self.firstView superview]? UIViewAnimationTransitionCurlUp:UIViewAnimationTransitionCurlDown) forView:self.containerView cache:YES]; // [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown // forView:self.containerView cache:YES]; } if([self.firstView superview]){ [self.firstView removeFromSuperview]; [self.containerView addSubview:self.secondView]; } else { [self.secondView removeFromSuperview]; [self.containerView addSubview:self.firstView]; } // 提交动画 [UIView commitAnimations]; } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; [self.view addSubview: _containerView]; // self.view.backgroundColor = [UIColor redColor]; _firstView = [[UIImageView alloc] initWithFrame: CGRectMake( 0, 100, 320, 300)]; _firstView.image = [UIImage imageNamed: @"iphone_2.jpg"]; [_containerView addSubview: _firstView]; _secondView = [[UIImageView alloc] initWithFrame: CGRectMake( 0, 100, 320, 300)]; _secondView.image = [UIImage imageNamed: @"iphone_3.jpg"]; [_containerView addSubview: _secondView]; _flip = [UIButton buttonWithType: UIButtonTypeRoundedRect]; _flip.frame = CGRectMake( 70, 430, 60, 20) ; [_flip setTitle: @"Flip" forState: UIControlStateNormal]; [_flip addTarget: self action: @selector(buttonAction:) forControlEvents: UIControlEventTouchUpInside]; [_flip setTitleColor: [UIColor redColor] forState: UIControlStateNormal]; [self.view addSubview: _flip]; _curl = [UIButton buttonWithType: UIButtonTypeRoundedRect]; _curl.frame = CGRectMake( 200, 430, 60, 20) ; [_curl setTitle: @"Curl" forState: UIControlStateNormal]; [_curl addTarget: self action: @selector(buttonAction:) forControlEvents: UIControlEventTouchUpInside]; [self.view addSubview: _curl]; }