-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJZAlbumViewController.m
166 lines (129 loc) · 5.04 KB
/
JZAlbumViewController.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
//
// JZAlbumViewController.m
// aoyouHH
//
// Created by jinzelu on 15/4/27.
// Copyright (c) 2015年 jinzelu. All rights reserved.
//
#import "JZAlbumViewController.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "PhotoView.h"
#import "BHProgressHUD.h"
@interface JZAlbumViewController ()<UIScrollViewDelegate,PhotoViewDelegate>
{
CGFloat lastScale;
MBProgressHUD *HUD;
NSMutableArray *_subViewList;
}
@property(nonatomic, strong) UIImageView *saveImage;
@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];
// UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapView)];
// [self.view addGestureRecognizer:tap];
[self initScrollView];
[self addLabels];
[self setPicCurrentIndex:self.currentIndex];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initScrollView{
// [[SDImageCache sharedImageCache] cleanDisk];
// [[SDImageCache sharedImageCache] clearMemory];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
self.scrollView.pagingEnabled = YES;
self.scrollView.userInteractionEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(self.imgArr.count*kScreenWidth, kScreenHeight);
self.scrollView.delegate = self;
self.scrollView.contentOffset = CGPointMake(0, 0);
//设置放大缩小的最大,最小倍数
// self.scrollView.minimumZoomScale = 1;
// self.scrollView.maximumZoomScale = 2;
[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, kScreenWidth-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(kScreenWidth*currentIndex, 0);
[self loadPhote:_currentIndex];
[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]]) {
//url数组
CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
PhotoView *photoV;
if ([self.imgArr[index] isKindOfClass:[UIImage class]]) {
photoV = [[PhotoView alloc] initWithFrame:frame withPhotoImage:[self.imgArr objectAtIndex:index]];
}else{
photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:[self.imgArr objectAtIndex:index]];
}
photoV.delegate = self;
self.saveImage = photoV.imageView;
[self.scrollView insertSubview:photoV atIndex:0];
[_subViewList replaceObjectAtIndex:index withObject:photoV];
}else{
PhotoView *photoV = (PhotoView *)currentPhotoView;
}
}
#pragma mark - PhotoViewDelegate
-(void)TapHiddenPhotoView{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)OnTapView{
NSLog(@"xiaoshi");
[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*kScreenWidth, kScreenHeight*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/kScreenWidth+1;
[self loadPhote:i-1];
self.sliderLabel.text = [NSString stringWithFormat:@"%d/%lu",i,(unsigned long)self.imgArr.count];
}
@end