JZAlbumViewController.m
8.21 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// JZAlbumViewController.m
// aoyouHH
//
// Created by jinzelu on 15/4/27.
// Copyright (c) 2015年 jinzelu. All rights reserved.
//
#import "JZAlbumViewController.h"
//#import "LocalFileViewController.h"
//#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "PhotoView.h"
#define IS_PAD (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)
@interface JZAlbumViewController ()<UIScrollViewDelegate,PhotoViewDelegate,UINavigationControllerDelegate>
{
CGFloat lastScale;
MBProgressHUD *HUD;
NSMutableArray *_subViewList;
}
@end
@implementation JZAlbumViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//
_subViewList = [[NSMutableArray alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
lastScale = 1.0;
self.view.backgroundColor = [UIColor blackColor];
[self initScrollView];
[self setPicCurrentIndex:self.currentIndex];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.delegate = self;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.automaticallyAdjustsScrollViewInsets = NO;
if (@available(iOS 11.0, *)) {
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
self.navigationController.navigationBarHidden = NO;
if (@available(iOS 11.0, *)) {
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
} else {
self.automaticallyAdjustsScrollViewInsets = YES;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initScrollView{
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
self.scrollView.pagingEnabled = YES;
self.scrollView.userInteractionEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(self.imgArr.count*ScreenWidth, 0);
self.scrollView.delegate = self;
self.scrollView.contentOffset = CGPointMake(0, 0);
//设置放大缩小的最大,最小倍数
[self.view addSubview:self.scrollView];
for (int i = 0; i < self.imgArr.count; i++) {
[_subViewList addObject:[NSNull class]];
}
}
-(void)addLabels{
self.sliderLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, ScreenHeight-64-49, 60, 30)];
self.sliderLabel.backgroundColor = [UIColor clearColor];
self.sliderLabel.textColor = [UIColor whiteColor];
self.sliderLabel.text = [NSString stringWithFormat:@"%ld/%lu",self.currentIndex+1,(unsigned long)self.imgArr.count];
[self.view addSubview:self.sliderLabel];
}
-(void)setPicCurrentIndex:(NSInteger)currentIndex{
_currentIndex = currentIndex;
self.scrollView.contentOffset = CGPointMake(ScreenWidth*currentIndex, 0);
[self loadPhote:_currentIndex];
if (_currentIndex == self.imgArr.count-1) {
[self loadPhote:_currentIndex-1];
}else if (_currentIndex == 0){
[self loadPhote:_currentIndex+1];
}else{
[self loadPhote:_currentIndex-1];
[self loadPhote:_currentIndex+1];
}
}
-(void)loadPhote:(NSInteger)index{
if (index<0 || index >=self.imgArr.count) {
return;
}
id currentPhotoView = [_subViewList objectAtIndex:index];
if (![currentPhotoView isKindOfClass:[PhotoView class]]) {
if ([self.imgArr[index] isKindOfClass:[NSString class]]) {
CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:[self.imgArr objectAtIndex:index]];
photoV.delegate = self;
// [self.scrollView insertSubview:photoV atIndex:0];
[self.scrollView addSubview:photoV];
[_subViewList replaceObjectAtIndex:index withObject:photoV];
}
// else if ([self.imgArr[index] isKindOfClass:[NIMMessage class]]) {
// NIMMessage *message = self.imgArr[index];
//
// if ([message.messageObject isKindOfClass:[NIMImageObject class]]) {
// NIMImageObject *image = message.messageObject;
//
// CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
// PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:image.url];
// photoV.delegate = self;
// [self.scrollView addSubview:photoV];
// [_subViewList replaceObjectAtIndex:index withObject:photoV];
//
// }else if ([message.messageObject isKindOfClass:[NIMVideoObject class]]) {
// NIMVideoObject *video = message.messageObject;
//
// CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
// PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withVideo:video];
// photoV.delegate = self;
// [self.scrollView addSubview:photoV];
// [_subViewList replaceObjectAtIndex:index withObject:photoV];
//
// NSLog(@"%@", currentPhotoView);
// }
//
// }
}else{
}
}
#pragma mark - PhotoViewDelegate
-(void)TapHiddenPhotoView{
// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)longPressGesturePhotoView:(UIAlertController *)alertVC
{
if (IS_PAD) {
UIPopoverPresentationController *popPresenter = [alertVC
popoverPresentationController];
popPresenter.sourceView = [CommonFun sharedInstance].topViewController.view; // 这就是挂靠的对象
popPresenter.sourceRect = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
[self presentViewController:alertVC animated:YES completion:nil];
}else{
[self presentViewController:alertVC animated:YES completion:nil];
};
}
-(void)OnTapView{
[self dismissViewControllerAnimated:YES completion:nil];
}
//手势
-(void)pinGes:(UIPinchGestureRecognizer *)sender{
if ([sender state] == UIGestureRecognizerStateBegan) {
lastScale = 1.0;
}
CGFloat scale = 1.0 - (lastScale -[sender scale]);
lastScale = [sender scale];
self.scrollView.contentSize = CGSizeMake(self.imgArr.count*ScreenWidth, ScreenHeight*lastScale);
NSLog(@"scale:%f lastScale:%f",scale,lastScale);
CATransform3D newTransform = CATransform3DScale(sender.view.layer.transform, scale, scale, 1);
sender.view.layer.transform = newTransform;
if ([sender state] == UIGestureRecognizerStateEnded) {
//
}
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidEndDecelerating");
int i = scrollView.contentOffset.x/ScreenWidth+1;
_currentIndex = i;
[self loadPhote:i-1];
self.sliderLabel.text = [NSString stringWithFormat:@"%d/%lu",i,(unsigned long)self.imgArr.count];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offsetX = scrollView.contentOffset.x;
NSLog(@"%f",offsetX);
}
// 将要显示控制器
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判断要显示的控制器是否是自己
BOOL isSelf = [viewController isKindOfClass:[self class]];
if(isSelf){
[self.navigationController setNavigationBarHidden:YES animated:animated];
}else{
NSLog(@"将要展示的viewController不是自己");
}
}
@end