首页 > 代码库 > 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:翻页效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。