forked from nytimes/NYTPhotoViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNYTViewController.m
178 lines (143 loc) · 8.26 KB
/
NYTViewController.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// NYTViewController.m
// ios-photo-viewer
//
// Created by Brian Capps on 02/11/2015.
// Copyright (c) 2014 Brian Capps. All rights reserved.
//
#import "NYTViewController.h"
#import <NYTPhotoViewer/NYTPhotosViewController.h>
#import "NYTExamplePhoto.h"
typedef NS_ENUM(NSUInteger, NYTViewControllerPhotoIndex) {
NYTViewControllerPhotoIndexCustomEverything = 1,
NYTViewControllerPhotoIndexLongCaption = 2,
NYTViewControllerPhotoIndexDefaultLoadingSpinner = 3,
NYTViewControllerPhotoIndexNoReferenceView = 4,
NYTViewControllerPhotoIndexCustomMaxZoomScale = 5,
NYTViewControllerPhotoIndexGif = 6,
NYTViewControllerPhotoCount,
};
@interface NYTViewController () <NYTPhotosViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *imageButton;
@property (nonatomic) NSArray *photos;
@end
@implementation NYTViewController
- (IBAction)imageButtonTapped:(id)sender {
self.photos = [[self class] newTestPhotos];
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:self.photos];
photosViewController.delegate = self;
[self presentViewController:photosViewController animated:YES completion:nil];
[self updateImagesOnPhotosViewController:photosViewController afterDelayWithPhotos:self.photos];
}
// This method simulates previously blank photos loading their images after some time.
- (void)updateImagesOnPhotosViewController:(NYTPhotosViewController *)photosViewController afterDelayWithPhotos:(NSArray *)photos {
CGFloat updateImageDelay = 5.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(updateImageDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (NYTExamplePhoto *photo in photos) {
if (!photo.image && !photo.imageData) {
photo.image = [UIImage imageNamed:@"NYTimesBuilding"];
[photosViewController updateImageForPhoto:photo];
}
}
});
}
+ (NSArray *)newTestPhotos {
NSMutableArray *photos = [NSMutableArray array];
for (NSUInteger i = 0; i < NYTViewControllerPhotoCount; i++) {
NYTExamplePhoto *photo = [[NYTExamplePhoto alloc] init];
if (i == NYTViewControllerPhotoIndexGif) {
photo.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"giphy" ofType:@"gif"]];
} else if (i == NYTViewControllerPhotoIndexCustomEverything || i == NYTViewControllerPhotoIndexDefaultLoadingSpinner) {
// no-op, left here for clarity:
photo.image = nil;
} else {
photo.image = [UIImage imageNamed:@"NYTimesBuilding"];
}
if (i == NYTViewControllerPhotoIndexCustomEverything) {
photo.placeholderImage = [UIImage imageNamed:@"NYTimesBuildingPlaceholder"];
}
NSString *caption = @"summary";
switch ((NYTViewControllerPhotoIndex)i) {
case NYTViewControllerPhotoIndexCustomEverything:
caption = @"photo with custom everything";
break;
case NYTViewControllerPhotoIndexLongCaption:
caption = @"photo with long caption. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum maximus laoreet vehicula. Maecenas elit quam, pellentesque at tempor vel, tempus non sem. Vestibulum ut aliquam elit. Vivamus rhoncus sapien turpis, at feugiat augue luctus id. Nulla mi urna, viverra sed augue malesuada, bibendum bibendum massa. Cras urna nibh, lacinia vitae feugiat eu, consectetur a tellus. Morbi venenatis nunc sit amet varius pretium. Duis eget sem nec nulla lobortis finibus. Nullam pulvinar gravida est eget tristique. Curabitur faucibus nisl eu diam ullamcorper, at pharetra eros dictum. Suspendisse nibh urna, ultrices a augue a, euismod mattis felis. Ut varius tortor ac efficitur pellentesque. Mauris sit amet rhoncus dolor. Proin vel porttitor mi. Pellentesque lobortis interdum turpis, vitae tincidunt purus vestibulum vel. Phasellus tincidunt vel mi sit amet congue.";
break;
case NYTViewControllerPhotoIndexDefaultLoadingSpinner:
caption = @"photo with loading spinner";
break;
case NYTViewControllerPhotoIndexNoReferenceView:
caption = @"photo without reference view";
break;
case NYTViewControllerPhotoIndexCustomMaxZoomScale:
caption = @"photo with custom maximum zoom scale";
break;
case NYTViewControllerPhotoIndexGif:
caption = @"animated GIF";
break;
case NYTViewControllerPhotoCount:
// this case statement intentionally left blank.
break;
}
photo.attributedCaptionTitle = [[NSAttributedString alloc] initWithString:@(i + 1).stringValue attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}];
photo.attributedCaptionSummary = [[NSAttributedString alloc] initWithString:caption attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor], NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}];
photo.attributedCaptionCredit = [[NSAttributedString alloc] initWithString:@"NYT Building Photo Credit: Nic Lehoux" attributes:@{NSForegroundColorAttributeName: [UIColor grayColor], NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]}];
[photos addObject:photo];
}
return photos;
}
#pragma mark - NYTPhotosViewControllerDelegate
- (UIView *)photosViewController:(NYTPhotosViewController *)photosViewController referenceViewForPhoto:(id <NYTPhoto>)photo {
if ([photo isEqual:self.photos[NYTViewControllerPhotoIndexNoReferenceView]]) {
return nil;
}
return self.imageButton;
}
- (UIView *)photosViewController:(NYTPhotosViewController *)photosViewController loadingViewForPhoto:(id <NYTPhoto>)photo {
if ([photo isEqual:self.photos[NYTViewControllerPhotoIndexCustomEverything]]) {
UILabel *loadingLabel = [[UILabel alloc] init];
loadingLabel.text = @"Custom Loading...";
loadingLabel.textColor = [UIColor greenColor];
return loadingLabel;
}
return nil;
}
- (UIView *)photosViewController:(NYTPhotosViewController *)photosViewController captionViewForPhoto:(id <NYTPhoto>)photo {
if ([photo isEqual:self.photos[NYTViewControllerPhotoIndexCustomEverything]]) {
UILabel *label = [[UILabel alloc] init];
label.text = @"Custom Caption View";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor redColor];
return label;
}
return nil;
}
- (CGFloat)photosViewController:(NYTPhotosViewController *)photosViewController maximumZoomScaleForPhoto:(id <NYTPhoto>)photo {
if ([photo isEqual:self.photos[NYTViewControllerPhotoIndexCustomMaxZoomScale]]) {
return 10.0f;
}
return 1.0f;
}
- (NSDictionary *)photosViewController:(NYTPhotosViewController *)photosViewController overlayTitleTextAttributesForPhoto:(id <NYTPhoto>)photo {
if ([photo isEqual:self.photos[NYTViewControllerPhotoIndexCustomEverything]]) {
return @{NSForegroundColorAttributeName: [UIColor grayColor]};
}
return nil;
}
- (NSString *)photosViewController:(NYTPhotosViewController *)photosViewController titleForPhoto:(id<NYTPhoto>)photo atIndex:(NSUInteger)photoIndex totalPhotoCount:(NSUInteger)totalPhotoCount {
if ([photo isEqual:self.photos[NYTViewControllerPhotoIndexCustomEverything]]) {
return [NSString stringWithFormat:@"%lu/%lu", (unsigned long)photoIndex+1, (unsigned long)totalPhotoCount];
}
return nil;
}
- (void)photosViewController:(NYTPhotosViewController *)photosViewController didNavigateToPhoto:(id <NYTPhoto>)photo atIndex:(NSUInteger)photoIndex {
NSLog(@"Did Navigate To Photo: %@ identifier: %lu", photo, (unsigned long)photoIndex);
}
- (void)photosViewController:(NYTPhotosViewController *)photosViewController actionCompletedWithActivityType:(NSString *)activityType {
NSLog(@"Action Completed With Activity Type: %@", activityType);
}
- (void)photosViewControllerDidDismiss:(NYTPhotosViewController *)photosViewController {
NSLog(@"Did Dismiss Photo Viewer: %@", photosViewController);
}
@end