反转动画Transition

本文介绍了一个使用Objective-C实现的视图控制器,该控制器通过按钮触发两种动画效果:翻转动画和卷曲动画,实现了两个图片视图之间的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值