本文实例为大家分享了uipageviewcontroller实现左右滑动界面展示的具体代码,供大家参考,具体内容如下
.h
1
2
3
4
|
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller @end |
.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#import "viewcontroller.h" #import "subpage1viewcontroller.h" #import "subpage2viewcontroller.h" @interface viewcontroller ()<uipageviewcontrollerdatasource,uipageviewcontrollerdelegate> @property nsarray *contentviewcontrollers; //viewcontrollers @property (nonatomic, strong) uipageviewcontroller *pageviewcontroller; @end @implementation viewcontroller - ( void )viewdidload { [super viewdidload]; [self pageviewcontroller]; } - ( void )didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } #pragma mark #pragma mark ----- uipageviewcontrollerdatasource ----- - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger index = [self indexforviewcontroller:viewcontroller]; if (index == 0) { index = [self.contentviewcontrollers count] - 1; } else { index--; } return [self viewcontrolleratindex:index]; } - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger index = [self indexforviewcontroller:viewcontroller]; index++; if (index == [self.contentviewcontrollers count]) { index = 0; } return [self viewcontrolleratindex:index]; } - (nsuinteger)indexforviewcontroller:(uiviewcontroller *)viewcontroller { return [self.contentviewcontrollers indexofobject:viewcontroller]; } - (uiviewcontroller *)viewcontrolleratindex:(nsuinteger)index { if (index > [self.contentviewcontrollers count]) { return nil; } uiviewcontroller *vc = [self.contentviewcontrollers objectatindex:index]; return vc; } #pragma mark #pragma mark ----- uipageviewcontrollerdelegate ----- - ( void )pageviewcontroller:(uipageviewcontroller *)pageviewcontroller willtransitiontoviewcontrollers:(nsarray *)pendingviewcontrollers { } - ( void )pageviewcontroller:(uipageviewcontroller *)pageviewcontroller didfinishanimating:( bool )finished previousviewcontrollers:(nsarray *)previousviewcontrollers transitioncompleted:( bool )completed { } #pragma mark #pragma mark init - (uipageviewcontroller *)pageviewcontroller { if (!_pageviewcontroller) { nsdictionary *options =[nsdictionary dictionarywithobject:[nsnumber numberwithinteger:uipageviewcontrollerspinelocationmin] forkey: uipageviewcontrolleroptionspinelocationkey]; _pageviewcontroller = [[uipageviewcontroller alloc] initwithtransitionstyle:uipageviewcontrollertransitionstylescroll navigationorientation:uipageviewcontrollernavigationorientationhorizontal options:options]; [[_pageviewcontroller view] setframe:[[self view] bounds]]; _contentviewcontrollers = [nsmutablearray arraywithobjects:[subpage1viewcontroller new ],[subpage2viewcontroller new ],nil]; // 设置uipageviewcontroller的配置项 [_pageviewcontroller setviewcontrollers:[nsarray arraywithobjects:_contentviewcontrollers[0], nil] direction:uipageviewcontrollernavigationdirectionforward animated:no completion:nil]; _pageviewcontroller.delegate = self; _pageviewcontroller.datasource = self; [self addchildviewcontroller:self.pageviewcontroller]; [self.view addsubview:self.pageviewcontroller.view]; } return _pageviewcontroller; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_26341621/article/details/50202465