首页 > 代码库 > iOS:翻页效果

iOS:翻页效果

 

////  main.m//  Hello////  Created by lishujun on 14-8-28.//  Copyright (c) 2014年 lishujun. All rights reserved.////  翻页太快会有问题,未解决//Unbalanced calls to begin/end appearance transitions for <ContentViewControler: 0x8f3d570>.#import <UIKit/UIKit.h>// -----------------------------内容视图控制器-----------------------------@interface ContentViewControler : UIViewController@property (nonatomic,strong) UILabel *label;@property NSString *message;@end@implementation ContentViewControler@synthesize label ;@synthesize message = _message;-(id)initWithMessage:(NSString *)aMessage{    _message = aMessage;    return self;}-(void) viewDidLoad{    //[super viewDidLoad];        //创建label对象    label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];    label.text = _message;    label.center = self.view.center;    label.textAlignment = UITextAlignmentCenter;    label.backgroundColor = [UIColor clearColor];    label.textColor = [UIColor redColor];        //在视图上添加label    [self.view addSubview:label];    self.view.backgroundColor = [UIColor lightGrayColor];}-(void) updateMessage:(NSString*)aMessage{    _message = aMessage;    label.text = _message;}@end// -----------------------------视图控制器对象-----------------------------@interface MyPageViewController : UIViewController <UIPageViewControllerDataSource>{    int pageIndex;}@property (strong, nonatomic) UIPageViewController *pageViewControler;@property (strong, nonatomic) NSArray *array;@end@implementation MyPageViewController@synthesize pageViewControler = _pageViewControler;@synthesize array = _array;-(void) viewDidLoad{    [super viewDidLoad];        self.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 440.0f);        ContentViewControler *content1 = [[ContentViewControler alloc]initWithMessage:@"Hello"];    ContentViewControler *content2 = [[ContentViewControler alloc]initWithMessage:@""];    NSArray *array = @[content1];    NSLog(@"%@",content1);    _array = [[NSArray alloc]initWithObjects:content1, content2, nil];        _pageViewControler = [[UIPageViewController alloc]                               initWithTransitionStyle: UIPageViewControllerTransitionStylePageCurl                                 navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal                                               options: nil];        [_pageViewControler setViewControllers:array                                 direction:UIPageViewControllerNavigationDirectionForward                                  animated:YES                                completion:NULL];        _pageViewControler.dataSource = self;        [self addChildViewController:_pageViewControler];    [self.view addSubview:_pageViewControler.view];        pageIndex = 0;}//--------------- data source 方法 ------------------ (UIViewController *)    pageViewController:(UIPageViewController *)pageViewController    viewControllerBeforeViewController:(UIViewController *)viewController{    ContentViewControler *aViewController = (ContentViewControler*)[self changeViewControler:viewController];    [aViewController updateMessage:@"i‘m lishujun too"];    return aViewController;}- (UIViewController *)    pageViewController:(UIPageViewController *)pageViewController    viewControllerAfterViewController:(UIViewController *)viewController{    ContentViewControler *aViewController = (ContentViewControler*)[self changeViewControler:viewController];    [aViewController updateMessage:@"i‘m lishujun"];    return aViewController;}// --- 自定义方法,在只有两个元素的数组里切换元素----(UIViewController *) changeViewControler:(UIViewController*)viewController{    int index = [_array indexOfObject:viewController];    switch(index)    {        case 0:            return _array[1];        case 1:            return _array[0];    }    return nil;}@end// -----------------------------委托对象-------------------------------@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>{    IBOutlet UIWindow *window;}@property (nonatomic, retain) UIWindow *window;@property (nonatomic, retain) MyPageViewController *viewController;@property (nonatomic, retain) UINavigationController *nav;@end@implementation HelloWorldAppDelegate@synthesize window;@synthesize viewController;-(void) applicationDidFinishLaunching:(UIApplication *)application{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];    self.viewController = [[MyPageViewController alloc]init];    //self.window.rootViewController = self.viewController;        self.nav = [[UINavigationController alloc]initWithRootViewController: viewController];    [self.nav setNavigationBarHidden:YES];        //隐藏导航栏,位于视图顶部    [self.nav setToolbarHidden:YES];              //隐藏工具栏,位于视图底部        self.window.rootViewController = self.nav;        [self.window makeKeyAndVisible];}@end// -----------------------------程序入口-----------------------------int main(int argc, char * argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");    }}

 

iOS:翻页效果