关闭

Xcode_7 iOS_9 平铺导航_标签导航 Objective-C (16)

标签: iosobjective-c
328人阅读 评论(0) 收藏 举报
分类:

1、新建SingleViewApplication项目,先自己下载3张图片,最好是英文或数字命名,先在原有的view视图里拖拽两个控件:ScrollView和PageControl,然后将view的背景设为黑色,ScrollView的配置如下,然后调整pageControl的大小,这样两边可以点击翻页,这两个控件是并列关系存在于view视图上,并且pageControl在scrollView上面(在storyboard中手动将pageControl拖到scrollView的下方)然后两个控件分别作为输出口,其中pageControl还要设置一个事件,详细见代码:




2、再作三个独立的ViewController,包含view,并且在view视图中拖拽一个imageView控件,但是这里存在的问题是弱三个ViewController嵌套View和imageView就无法正常显示图片内容,这里的临时解决办法是把第三个ViewController的View视图控件去掉,直接拖拽imageView控件到ViewController上,这里每个ViewController的storyboard ID分别设置一下,为page1、page2、page3,然后把之前下载好的三张图片先导入到资源文件中,再分别配置到每个imageView的图片属性中,这样就可以不通过segue来连接视图控制器,也就不用为这三个独立的视图控制器添加自己的类(当然如果是要触发事件的话还是需要自己的类)。





3、代码如下,具体显示大小自己根据实际情况调整:

//
//  ViewController.h
//  TestProject
//
//  Created by 侯家奇 on 16/8/17.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (strong, nonatomic) UIView *page1;
@property (strong, nonatomic) UIView *page2;
@property (strong, nonatomic) UIView *page3;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

- (IBAction)changePage:(id)sender;

@end

//
//  ViewController.m
//  TestProject
//
//  Created by 侯家奇 on 16/8/17.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib
    self.scrollView.delegate = self;
    
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.scrollView.frame.size.height);
    self.scrollView.frame = self.view.frame;
    
    UIStoryboard *mainStoryboard = self.storyboard;
    
    UIViewController *page1ViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page1"];
    self.page1 = page1ViewController.view;
    self.page1.frame = CGRectMake(0.0f, 0.0f, 375.0f, 667.0f);
    
    UIViewController *page2ViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page2"];
    self.page2 = page2ViewController.view;
    self.page2.frame = CGRectMake(375.0f, 0.0f, 375.0f, 667.0f);

    UIViewController *page3ViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page3"];
    self.page3 = page3ViewController.view;
    self.page3.frame = CGRectMake(2 * 375.0f, 0.0f, 375.0f, 667.0f);
    
    self.scrollView.delegate = self;
    
    [self.scrollView addSubview:self.page1];
    [self.scrollView addSubview:self.page2];
    [self.scrollView addSubview:self.page3];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    self.pageControl.currentPage = offset.x /375.0f;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changePage:(id)sender {
    [UIView animateWithDuration:0.3f animations:^{
        NSInteger whichPage = self.pageControl.currentPage;
        self.scrollView.contentOffset = CGPointMake(375.0f * whichPage, 0.0f);
    }];
}
@end



4、电子书翻页,根据上面的项目把第一个ViewController下面的View视图里的两个控件可以去掉了,其他的不变,然后修改代码:

//
//  ViewController.h
//  TestProject
//
//  Created by 侯家奇 on 16/8/17.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import <UIKit/UIKit.h>

enum DirectionForward {
    ForwardBefore = 1 //向前
    ,ForwardAfter = 2 //向后
};

@interface ViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@property (strong, nonatomic) UIPageViewController *pageViewController;

@end

//
//  ViewController.m
//  TestProject
//
//  Created by 侯家奇 on 16/8/17.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController {
    //当前页面的索引
    int pageIndex;
    //翻页的方向
    int directionForward;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib
    //创建翻页实例 initWithTransitionStyle 是翻页效果,navigationOrientation 是翻页方向,
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    UIStoryboard *mainStoryboard = self.storyboard;
    UIViewController *pageViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page1"];
    
    //第一个视图
    NSArray *viewControllers = @[pageViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
    [self.view addSubview:self.pageViewController.view];
    pageIndex = 0;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    pageIndex --;
    if (pageIndex < 0) {
        pageIndex = 0;
        return nil;
    }
    
    directionForward = ForwardBefore;
    UIStoryboard *mainStoryboard = self.storyboard;
    NSString *pageId = [NSString stringWithFormat:@"page%i", pageIndex+1];
    UIViewController *pvController = [mainStoryboard instantiateViewControllerWithIdentifier:pageId];
    return pvController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    pageIndex ++;
    if (pageIndex > 2) {
        pageIndex = 2;
        return nil;
    }
    
    directionForward = ForwardAfter;
    UIStoryboard *mainStoryboard = self.storyboard;
    NSString *pageId = [NSString stringWithFormat:@"page%i", pageIndex+1];
    UIViewController *pvController = [mainStoryboard instantiateViewControllerWithIdentifier:pageId];
    return pvController;
}

//动态设定书脊的位置
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
    self.pageViewController.doubleSided = NO;
    return UIPageViewControllerSpineLocationMin;
}

//检查用户是否成功翻页
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (!completed) {
        if (directionForward == ForwardAfter) {
            pageIndex --;
        }
        if (directionForward == ForwardBefore) {
            pageIndex ++;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end



5、这里介绍一下标签导航,用iOS的 Tabbed Application模版新建项目即可,通过主controller连接新添加ViewController的segue,然后新建cocoa touch class并且配置到新的ViewController上(其实还是很简单的,基弧不用代码)。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:246308次
    • 积分:4612
    • 等级:
    • 排名:第7061名
    • 原创:200篇
    • 转载:136篇
    • 译文:0篇
    • 评论:49条
    文章分类
    最新评论