-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathBFRImageViewController.m
157 lines (122 loc) · 5.83 KB
/
BFRImageViewController.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// BFRImageViewController.m
// Buffer
//
// Created by Jordan Morgan on 11/13/15.
//
//
#import "BFRImageViewController.h"
#import "BFRImageContainerViewController.h"
@interface BFRImageViewController () <UIPageViewControllerDataSource>
/*! This view controller just acts as a container to hold a page view controller, which pages between the view controllers that hold an image. */
@property (strong, nonatomic) UIPageViewController *pagerVC;
/*! Each image displayed is shown in its own instance of a BFRImageViewController. This array holds all of those view controllers, one per image. */
@property (strong, nonatomic) NSMutableArray *imageViewControllers;
/*! Each image is represented via a @c NSURL or an actual @c UIImage. */
@property (strong, nonatomic) NSArray *images;
/*! This will automatically hide the "Done" button after five seconds. */
@property (strong, nonatomic) NSTimer *timerHideUI;
/*! The button that sticks to the top left of the view that is responsible for dismissing this view controller. */
@property (strong, nonatomic) UIButton *doneButton;
@end
@implementation BFRImageViewController
#pragma mark - Initializers
- (instancetype)initWithImageSource:(NSArray *)images {
self = [super init];
if (self) {
NSAssert(images.count > 0, @"You must supply at least one image source to use this class.");
self.images = images;
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.enableDoneButton = YES;
}
return self;
}
#pragma mark - View Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
//View setup
self.view.backgroundColor = self.isUsingTransparentBackground ? [UIColor clearColor] : [UIColor blackColor];
if (self.shouldHideStatusBar) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
//Viewer done button setup
[self setupDoneButton];
//Setup image view controllers
self.imageViewControllers = [NSMutableArray new];
for (id imgSrc in self.images) {
BFRImageContainerViewController *imgVC = [BFRImageContainerViewController new];
imgVC.imgSrc = imgSrc;
imgVC.useTransparentBackground = self.isUsingTransparentBackground;
imgVC.disableHorizontalDrag = (self.images.count > 1);
[self.imageViewControllers addObject:imgVC];
}
//Set up pager
self.pagerVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
if (self.imageViewControllers.count > 1) {
self.pagerVC.dataSource = self;
}
[self.pagerVC setViewControllers:@[self.imageViewControllers.firstObject] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
//Add pager to view hierarchy
[self addChildViewController:self.pagerVC];
[[self view] addSubview:[self.pagerVC view]];
[self.pagerVC didMoveToParentViewController:self];
//Register for touch events on the images/scrollviews to hide UI chrome
[self registerNotifcations];
}
- (void)setupDoneButton {
if (self.enableDoneButton) {
UIImage *crossImage = [UIImage imageNamed:@"cross"];
self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.doneButton setImage:crossImage forState:UIControlStateNormal];
[self.doneButton addTarget:self action:@selector(handleDoneAction) forControlEvents:UIControlEventTouchUpInside];
self.doneButton.frame = CGRectMake(20, 20, 17, 17);
[self.view addSubview:self.doneButton];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Pager Datasource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = ((BFRImageContainerViewController *)viewController).pageIndex;
if (index == 0) {
return nil;
}
//Update index
index--;
BFRImageContainerViewController *vc = self.imageViewControllers[index];
vc.pageIndex = index;
return vc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = ((BFRImageContainerViewController *)viewController).pageIndex;
if (index == self.imageViewControllers.count - 1) {
return nil;
}
//Update index
index++;
BFRImageContainerViewController *vc = self.imageViewControllers[index];
vc.pageIndex = index;
return vc;
}
#pragma mark - Utility methods
- (void)dismiss {
self.pagerVC.dataSource = nil;
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)handlePop {
self.view.backgroundColor = [UIColor blackColor];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
- (void)handleDoneAction {
[self dismissViewControllerAnimated:YES completion:nil];
}
/*! The images and scrollview are not part of this view controller, so instances of @c BFRimageContainerViewController will post notifications when they are touched for things to happen. */
- (void)registerNotifcations {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss) name:@"DismissUI" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss) name:@"ImageLoadingError" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePop) name:@"ViewControllerPopped" object:nil];
}
@end