iOS之UIPageController的使用——纯代码实现翻页效果

1、FKPageController类

//.h
#import <UIKit/UIKit.h>

@interface FKPageController : UIViewController
@property (assign, nonatomic) NSUInteger pageIndex;
- (id)initWithPageNumber:(NSInteger)pageNumber;
@end


//.m
#import "FKPageController.h"

@interface FKPageController ()

@end

@implementation FKPageController
NSArray * contentList;
NSArray * coverList;
- (id)initWithPageNumber:(NSInteger)pageNumber
{
	contentList = [NSArray arrayWithObjects:@"疯狂Android讲义",
		@"疯狂Ajax讲义",
		@"疯狂XML讲义",
		@"疯狂HTML5/CSS3/JavaScript讲义" , nil];
	coverList = [NSArray arrayWithObjects:@"android.png",
		@"ajax.png",
		@"xml.png",
		@"html.png" , nil];
	self.pageIndex = pageNumber;
	self = [super initWithNibName:nil bundle:nil];
	if (self)
	{
		// 设置背景
		self.view.backgroundColor = [UIColor grayColor];
		// 创建UILabel控件
		UILabel* label = [[UILabel alloc] initWithFrame:
			CGRectMake(260 , 10 , 60 , 35)];
		label.backgroundColor = [UIColor clearColor];
		label.textColor = [UIColor redColor];
		// 设置UILabel控件显示的文本
		label.text = [NSString stringWithFormat:@"第[%ld]页"
			, pageNumber + 1];
		// 将UILabel添加到程序界面中
		[self.view addSubview:label];
		// 创建UILabel控件
		UILabel* bookLabel = [[UILabel alloc] initWithFrame:
			CGRectMake(0 , 30,
			CGRectGetWidth(self.view.frame) , 60)];
		bookLabel.textAlignment = NSTextAlignmentCenter;
		bookLabel.numberOfLines = 2;
		bookLabel.font = [UIFont systemFontOfSize:24];
		bookLabel.backgroundColor = [UIColor clearColor];
		bookLabel.textColor = [UIColor blueColor];
		// 设置UILabel控件显示的文本
		bookLabel.text = [contentList objectAtIndex:pageNumber % 4];
		// 将UILabel添加到程序界面中
		[self.view addSubview:bookLabel];
		// 创建UIImageView控件
		UIImageView* bookImage = [[UIImageView alloc] initWithFrame:
			CGRectMake(0, 90, CGRectGetWidth(self.view.frame), 320)];
		// 设置该控件的图片缩放模式
		bookImage.contentMode = UIViewContentModeScaleAspectFit;
		// 设置该UIImageView所显示的图片
		bookImage.image = [UIImage imageNamed:[
			coverList objectAtIndex:pageNumber % 4]];
		// 将UIImageView添加到程序界面中
		[self.view addSubview:bookImage];
	}
	return self;
}
@end

2、FKAppDelegate类

//.h
#import <UIKit/UIKit.h>

@interface FKAppDelegate : UIResponder <UIApplicationDelegate
	, UIPageViewControllerDataSource>

@property (strong, nonatomic) UIWindow *window;

@end


//.m
#import "FKAppDelegate.h"

#import "FKPageController.h"

@implementation FKAppDelegate

- (BOOL)application:(UIApplication *)application
	didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	// 创建程序窗口
	self.window = [[UIWindow alloc] initWithFrame:
		[[UIScreen mainScreen] bounds]];
	// 创建FKPageController控制器
	FKPageController *pageZero = [[FKPageController alloc]
								  initWithPageNumber:0];
	// 创建一个NSDictionary对象,作为创建UIPageViewController的选项。
	// 该选项只支持2个key:
	// UIPageViewControllerOptionSpineLocationKey:指定翻页效果中“书脊”的位置
	// UIPageViewControllerOptionInterPageSpacingKey:指定2个页面之间间距
	NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
	[NSNumber numberWithInt:UIPageViewControllerSpineLocationMin],
	UIPageViewControllerOptionSpineLocationKey,
	[NSNumber numberWithFloat:0],
	UIPageViewControllerOptionInterPageSpacingKey , nil];
	// 创建UIPageViewController
	UIPageViewController *pageViewController =
		[[UIPageViewController alloc]
		// 设置页面过度效果:此处使用书页卷动的翻页效果
		initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
		// 设置页面的翻页方向:此处使用水平翻页
		navigationOrientation:
		UIPageViewControllerNavigationOrientationHorizontal
		options:options];
	// 设置使用支持双面
	pageViewController.doubleSided = YES;
	// 为UIPageViewController设置delegate
	pageViewController.dataSource = self;
	// 设置UIPageViewController管理的视图控制器
	[pageViewController setViewControllers:@[pageZero]
	 	// 指定向前翻页
		direction:UIPageViewControllerNavigationDirectionForward
		animated:YES
		completion:nil];
	// 设置程序窗口的根控制器为pageViewController
	self.window.rootViewController = pageViewController;
	[self.window makeKeyAndVisible];
	return YES;
}
// 当用户控制UIPageViewController向前翻页时调用该方法
- (FKPageController *)pageViewController:(UIPageViewController *)pvc
	viewControllerBeforeViewController:(FKPageController *)vc
{
	// 如果当前pageIndex大于0,将pageIndex-1作为参数创建FKPageController
	if (vc.pageIndex > 0)
	{

		NSUInteger index = vc.pageIndex;
		return [[FKPageController alloc]
			initWithPageNumber:index - 1];
	}
	else
	{
		NSUInteger index = vc.pageIndex;
		return [[FKPageController alloc]
			initWithPageNumber:index];
	}
}
// 当用户控制UIPageViewController向后翻页时调用该方法
- (FKPageController *)pageViewController:(UIPageViewController *)pvc
	viewControllerAfterViewController:(FKPageController *)vc
{
	// 将pageIndex-1作为参数创建FKPageController
	NSUInteger index = vc.pageIndex;
	return [[FKPageController alloc]
		initWithPageNumber:index + 1];
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值