IOS开发:使用animateWithDuration简单地控制页面切换效果
来源:互联网 发布:设置斐讯k1路由器mac 编辑:程序博客网 时间:2024/05/01 03:16
在本例子中,使用一个按钮切换两个view视图,这两个视图上面仅仅放置两张图片,当单击按钮时,第一个视图消失,第二个视图淡入屏幕,在显示完成后立马向右下角移动。
1.新建工程,建个viewbase application即可。
另外在xib文件中,添加两个view1,view2,以及在每个view上添加一个UIImageView,并且放置两张图片。在放置一个按钮。效果如下:
2.对应需要添加地controller文件的代码如下:
ViewPageDemoViewController.h代码如下:
#import <UIKit/UIKit.h>@interface ViewPageDemoViewController : UIViewController{ IBOutlet UIView *view1; IBOutlet UIView *view2;}@property (nonatomic,retain) IBOutlet UIView *view1;@property (nonatomic,retain) IBOutlet UIView *view2;-(IBAction)changePage:(id)sender;@end
ViewPageDemoViewController.m代码如下:
//// ViewPageDemoViewController.m// ViewPageDemo//// Created by kumahikarihui Baxiaxx on 12-7-10.// Copyright 2012年 __MyCompanyName__. All rights reserved.//#import "ViewPageDemoViewController.h"@implementation ViewPageDemoViewController@synthesize view1,view2;-(void)animationShow:(NSInteger)index{ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.0f]; switch (index) { case 0: [UIView animateWithDuration:1 animations:^{ view2.alpha = 1.0; view1.alpha = 0.0; }completion:^(BOOL finished){ [UIView animateWithDuration:4.0 animations:^{ view2.center = CGPointMake(500.0, 470.0); view1.center = CGPointMake(160.0, 230.0); }]; }]; break; case 1: [UIView animateWithDuration:1 animations:^{ view1.alpha = 1.0; view2.alpha = 0.0; }completion:^(BOOL finished){ [UIView animateWithDuration:4.0 animations:^{ view1.center = CGPointMake(500.0, 470.0); view2.center = CGPointMake(160.0, 230.0); }]; }]; break; default: break; }}-(IBAction)changePage:(id)sender{ static BOOL bAgain = TRUE; if(bAgain) { [view1 removeFromSuperview]; [self.view addSubview:view2]; [self animationShow:0]; } else { [view2 removeFromSuperview]; [self.view addSubview:view1]; [self animationShow:1]; } bAgain = !bAgain;}-(void)viewDidLoad{ view2.alpha = 0.0;}- (void)didReceiveMemoryWarning{ // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}#pragma mark - View lifecycle-(void)dealloc{ [view2 release]; [view1 release]; [super dealloc];}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end
这里有一个小小的问题,运行程序会发现,第一次单击按钮时,第二图片并没有淡入的效果,而是直接就显示出来了,这是由于淡入淡出的效果只用的是alpha从0到1之前慢慢过渡而实现的,仔细看代码发现其实第一次按钮显示第二张图的时候,view2.alpha的值本来就为1,所以从1变化到1当然没有效果了,解决办法很简单,只要预先初始化一下就可以了(将viewDidLoad()中绿色注释的代码打开即可)。
- IOS开发:使用animateWithDuration简单地控制页面切换效果
- IOS开发笔记UIView使用animateWithDuration控制页面效果
- IOS开发笔记(6)UIView使用animateWithDuration控制页面效果
- iOS 页面切换控制
- iOS UIView animateWithDuration 使用详解
- iOS- 动画UIView animateWithDuration 使用
- iOS页面切换动画效果
- iOS开发中使用代码控制横竖屏的切换
- ios开发切换页面特效
- IOS开发之页面切换
- IOS开发之页面切换
- 页面中简单的切换效果
- iOS-UINavigationController导航 页面切换效果
- iOS-分段控件控制页面切换
- swift用animateWithDuration实现简单view切换动画
- 使用CircularReveal动画效果切换页面
- 使用Fragment控制切换多个页面
- UIView animateWithDuration 使用详解(实现动画效果)
- C++标准库类型
- 量化和优化Android设备的用户体验
- JAX-RS
- wince修改界面字体
- oracle system权限
- IOS开发:使用animateWithDuration简单地控制页面切换效果
- vector学习笔记
- 响应式Web设计帮助移动终端访问网站
- Android异步下载网络图片(其一:Handler)
- Java编程中“为了性能”尽量要做到的一些地方
- UI设计准则
- 如何解决Cannot modify header information...问题?
- JSP的优势与劣势浅析
- Android异步下载网络图片(其二:AsyncTask)