【转】找了好久,IOS实现半翻页功能
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
PageViewController *pvController = [[PageViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:pvController]; //加载有标题的view
navController.navigationBar.tintColor=[UIColor redColor]; //颜色设置
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (id)init
{
if (!(self = [super init]))
return self;
self.title = @"翻页 Demo";
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView; //没有xib的时候需要
UIView *frontView1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 416.0f)];
//frontView1.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"back1.png"]];
frontView1 = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
frontView1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back1.jpg"]];
[self.view addSubview:frontView1];
UIView *frontView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 416.0f)];
frontView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"back2.jpg"]];
[self.view addSubview:frontView];
switchView = [[[UISwitch alloc] init] autorelease];
[switchView setCenter:CGPointMake(200.0f,350.0f)];
[frontView1 addSubview:switchView];
switchStatusLabel = [[[UILabel alloc] initWithFrame:CGRectMake(250.0f, 250.0f, 50.0f, 30.0f)] autorelease];
[frontView addSubview:switchStatusLabel];
[switchStatusLabel setTextAlignment:UITextAlignmentCenter];
switchStatusLabel.text=@"OFF";
isCurl=NO;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] //按钮的初始化及触发条件设置
initWithTitle:@"Curl"
style:UIBarButtonItemStylePlain
target:self
action:@selector(doCurl)] autorelease];
}
- (void) doCurl
{
//创建CATransition对象
CATransition *animation = [CATransition animation];
//相关参数设置
[animation setDelegate:self];
[animation setSpeed:3.0f];
[animation setDuration:1.0f];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
//向上卷的参数
if(!isCurl)
{
//设置动画类型为pageCurl,并只卷一半
[animation setType:@"pageCurl"];
animation.endProgress=0.7;
}
//向下卷的参数
else
{
//设置动画类型为pageUnCurl,并从一半开始向下卷
[animation setType:@"pageUnCurl"];
animation.startProgress=0.7;
}
//卷的过程完成后停止,并且不从层中移除动画
[animation setFillMode:kCAFillModeForwards];
[animation setSubtype:kCATransitionFromBottom];
[animation setRemovedOnCompletion:NO];
isCurl=!isCurl;
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[self.view layer] addAnimation:animation forKey:@"pageCurlAnimation"]; //翻页效果
if([switchView isOn])
{
switchStatusLabel.text=@"ON";
}
else
{
switchStatusLabel.text=@"OFF";
}
}