您的位置:首页 > 产品设计 > UI/UE

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

2015-01-12 22:29 435 查看
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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息