9
9
#import " EasyGalleryViewController.h"
10
10
#import " CustomImageView.h"
11
11
12
+ enum {
13
+ kIndexOfPreviousScrollView = 0 ,
14
+ kIndexOfCurrentScrollView ,
15
+ kIndexOfNextScrollView ,
16
+ kMaxOfScrollView
17
+ };
18
+
12
19
@implementation EasyGalleryViewController
13
20
14
21
@synthesize imageFiles = imageFiles_;
22
+ @synthesize currentImageIndex = currentImageIndex_;
15
23
16
24
@synthesize scrollView = scrollView_;
25
+ @synthesize contentOffsetIndex = contentOffsetIndex_;
17
26
18
- @synthesize previousScrollView = previousScrollView_;
19
- @synthesize currentScrollView = currentScrollView_;
20
- @synthesize nextScrollView = nextScrollView_;
21
-
22
- @synthesize currentIndex = currentIndex_;
23
-
27
+ @synthesize imageScrollViews = imageScrollViews_;
24
28
25
29
#pragma mark -
26
30
#pragma mark Controle scroll views
@@ -41,24 +45,20 @@ - (void)setImageAtIndex:(NSInteger)index toScrollView:(UIScrollView*)scrollView
41
45
}
42
46
43
47
44
- - (void )scrollToIndex : ( NSInteger ) index animated : ( BOOL ) animated
48
+ - (void )layoutScrollViews
45
49
{
46
- CGPoint contentOffset =
47
- CGPointMake (index * self.scrollView .frame .size .width , 0 );
48
- [self .scrollView setContentOffset: contentOffset animated: animated];
50
+ CGSize newSize = self.view .bounds .size ;
49
51
50
- }
51
-
52
- - (void )adjustViews
53
- {
54
- CGSize contentSize = CGSizeMake (
55
- self.currentScrollView .frame .size .width * [self .imageFiles count ],
56
- self.currentScrollView .frame .size .height );
57
- self.scrollView .contentSize = contentSize;
52
+ CGFloat x = (self.contentOffsetIndex -1 ) * newSize.width ;
53
+ for (UIScrollView* scrollView in self.imageScrollViews ) {
54
+ scrollView.frame = CGRectMake (x, 0 , newSize.width , newSize.height );
55
+ scrollView.contentSize = newSize;
56
+ x += newSize.width ;
57
+ }
58
58
59
- [ self setImageAtIndex: self .currentIndex- 1 toScrollView: self .previousScrollView];
60
- [self setImageAtIndex: self .currentIndex toScrollView: self .currentScrollView] ;
61
- [ self setImageAtIndex: self .currentIndex+ 1 toScrollView: self .nextScrollView] ;
59
+ self. scrollView . contentSize = CGSizeMake (
60
+ [self .imageFiles count ]*newSize. width , newSize. height ) ;
61
+ self. scrollView . contentOffset = CGPointMake ( self.contentOffsetIndex *newSize. width , 0 ) ;
62
62
}
63
63
64
64
@@ -68,33 +68,32 @@ - (void)adjustViews
68
68
- (void )viewDidLoad {
69
69
[super viewDidLoad ];
70
70
71
- // setup scroll views
71
+ // setup internal scroll views
72
72
CGRect imageScrollViewFrame = CGRectZero ;
73
- imageScrollViewFrame.size = self.scrollView .frame .size ;
74
- imageScrollViewFrame.origin .x = (self.currentIndex -1 ) * imageScrollViewFrame.size .width ;
73
+ imageScrollViewFrame.size = self.scrollView .bounds .size ;
74
+ imageScrollViewFrame.origin .x = (self.contentOffsetIndex -1 ) * imageScrollViewFrame.size .width ;
75
+
76
+ self.imageScrollViews = [NSMutableArray array ];
75
77
76
78
CGRect imageViewFrame = CGRectZero ;
77
- imageViewFrame.size = self.scrollView .frame .size ;
79
+ imageViewFrame.size = self.scrollView .bounds .size ;
78
80
79
- for (int i=0 ; i < 3 ; i++) {
81
+ for (int i=0 ; i < kMaxOfScrollView ; i++) {
80
82
81
83
// image view
82
84
CustomImageView* imageView =
83
85
[[CustomImageView alloc ] initWithFrame: imageViewFrame];
84
- /*
85
86
imageView.autoresizingMask =
86
87
UIViewAutoresizingFlexibleLeftMargin |
87
88
UIViewAutoresizingFlexibleWidth |
88
89
UIViewAutoresizingFlexibleRightMargin |
89
90
UIViewAutoresizingFlexibleTopMargin |
90
91
UIViewAutoresizingFlexibleHeight |
91
92
UIViewAutoresizingFlexibleBottomMargin;
92
- */
93
- // imageView.delegate = self;
94
93
95
94
// scroll view
96
95
UIScrollView* imageScrollView =
97
- [[UIScrollView alloc ] initWithFrame: imageScrollViewFrame];
96
+ [[UIScrollView alloc ] initWithFrame: imageScrollViewFrame];
98
97
imageScrollView.minimumZoomScale = 1.0 ;
99
98
imageScrollView.maximumZoomScale = 5.0 ;
100
99
imageScrollView.showsHorizontalScrollIndicator = NO ;
@@ -105,44 +104,41 @@ - (void)viewDidLoad {
105
104
[imageScrollView addSubview: imageView];
106
105
[self .scrollView addSubview: imageScrollView];
107
106
108
- // assign to iVars
109
- switch (i) {
110
- case 0 :
111
- self.previousScrollView = imageScrollView;
112
- break ;
113
- case 1 :
114
- self.currentScrollView = imageScrollView;
115
- break ;
116
- case 2 :
117
- self.nextScrollView = imageScrollView;
118
- break ;
119
- }
107
+ // store scrollViews
108
+ [self .imageScrollViews addObject: imageScrollView];
120
109
121
110
// release all
122
111
[imageView release ];
123
112
[imageScrollView release ];
124
-
113
+
114
+ // setup initial image
115
+ [self setImageAtIndex: i-1 toScrollView: imageScrollView];
116
+
125
117
// next image
126
118
imageScrollViewFrame.origin .x += imageScrollViewFrame.size .width ;
127
119
}
128
120
121
+ // setup base scroll view
129
122
self.scrollView .pagingEnabled = YES ;
130
123
self.scrollView .showsHorizontalScrollIndicator = NO ;
131
124
self.scrollView .showsVerticalScrollIndicator = NO ;
132
125
self.scrollView .scrollsToTop = NO ;
133
-
134
- [ self adjustViews ];
135
- [self scrollToIndex: self .currentIndex animated: NO ];
126
+
127
+ // final init
128
+ [self layoutScrollViews ];
136
129
}
137
130
138
131
139
- /*
140
- // Override to allow orientations other than the default portrait orientation.
141
132
- (BOOL )shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation)interfaceOrientation {
142
- // Return YES for supported orientations
143
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
133
+ return YES ;
134
+ }
135
+
136
+ - (void )willAnimateRotationToInterfaceOrientation : (UIInterfaceOrientation)interfaceOrientation duration : (NSTimeInterval )duration
137
+ {
138
+ [self layoutScrollViews ];
144
139
}
145
- */
140
+
141
+
146
142
147
143
- (void )didReceiveMemoryWarning {
148
144
// Releases the view if it doesn't have a superview.
@@ -155,11 +151,7 @@ - (void)viewDidUnload {
155
151
self.imageFiles = nil ;
156
152
157
153
self.scrollView = nil ;
158
-
159
- self.previousScrollView = nil ;
160
- self.currentScrollView = nil ;
161
- self.nextScrollView = nil ;
162
-
154
+ self.imageScrollViews = nil ;
163
155
}
164
156
165
157
- (void )dealloc {
@@ -172,30 +164,42 @@ - (void)dealloc {
172
164
173
165
-(void )setupPreviousImage
174
166
{
175
- UIScrollView* tmpView = self.currentScrollView ;
176
-
177
- self.currentScrollView = self.previousScrollView ;
178
- self.previousScrollView = self.nextScrollView ;
179
- self.nextScrollView = tmpView;
180
-
181
- CGRect frame = self.currentScrollView .frame ;
167
+ UIScrollView* previousScrollView =
168
+ [self .imageScrollViews objectAtIndex: kIndexOfPreviousScrollView ];
169
+ UIScrollView* currentScrollView =
170
+ [self .imageScrollViews objectAtIndex: kIndexOfCurrentScrollView ];
171
+ UIScrollView* nextScrollView =
172
+ [self .imageScrollViews objectAtIndex: kIndexOfNextScrollView ];
173
+
174
+ [self .imageScrollViews removeAllObjects ];
175
+ [self .imageScrollViews addObject: nextScrollView];
176
+ [self .imageScrollViews addObject: previousScrollView];
177
+ [self .imageScrollViews addObject: currentScrollView];
178
+
179
+ CGRect frame = previousScrollView.frame ;
182
180
frame.origin .x -= frame.size .width ;
183
- self. previousScrollView .frame = frame;
184
- [self setImageAtIndex: self .currentIndex -1 toScrollView: self .previousScrollView ];
181
+ nextScrollView .frame = frame;
182
+ [self setImageAtIndex: self .currentImageIndex -1 toScrollView: nextScrollView ];
185
183
}
186
184
187
185
-(void )setupNextImage
188
186
{
189
- UIScrollView* tmpView = self.currentScrollView ;
190
-
191
- self.currentScrollView = self.nextScrollView ;
192
- self.nextScrollView = self.previousScrollView ;
193
- self.previousScrollView = tmpView;
194
-
195
- CGRect frame = self.currentScrollView .frame ;
187
+ UIScrollView* previousScrollView =
188
+ [self .imageScrollViews objectAtIndex: kIndexOfPreviousScrollView ];
189
+ UIScrollView* currentScrollView =
190
+ [self .imageScrollViews objectAtIndex: kIndexOfCurrentScrollView ];
191
+ UIScrollView* nextScrollView =
192
+ [self .imageScrollViews objectAtIndex: kIndexOfNextScrollView ];
193
+
194
+ [self .imageScrollViews removeAllObjects ];
195
+ [self .imageScrollViews addObject: currentScrollView];
196
+ [self .imageScrollViews addObject: nextScrollView];
197
+ [self .imageScrollViews addObject: previousScrollView];
198
+
199
+ CGRect frame = nextScrollView.frame ;
196
200
frame.origin .x += frame.size .width ;
197
- self. nextScrollView .frame = frame;
198
- [self setImageAtIndex: self .currentIndex +1 toScrollView: self .nextScrollView ];
201
+ previousScrollView .frame = frame;
202
+ [self setImageAtIndex: self .currentImageIndex +1 toScrollView: previousScrollView ];
199
203
}
200
204
201
205
@@ -204,22 +208,26 @@ -(void)setupNextImage
204
208
- (void )scrollViewDidScroll : (UIScrollView *)scrollView
205
209
{
206
210
CGFloat position = scrollView.contentOffset .x / scrollView.bounds .size .width ;
207
- CGFloat delta = position - (CGFloat )self.currentIndex ;
211
+ CGFloat delta = position - (CGFloat )self.currentImageIndex ;
208
212
209
213
if (fabs (delta) >= 1 .0f ) {
210
- self.currentScrollView .zoomScale = 1.0 ;
211
- self.currentScrollView .contentOffset = CGPointZero ;
214
+ UIScrollView* currentScrollView =
215
+ [self .imageScrollViews objectAtIndex: kIndexOfCurrentScrollView ];
216
+ currentScrollView.zoomScale = 1.0 ;
217
+ currentScrollView.contentOffset = CGPointZero ;
212
218
213
- // NSLog(@"%f (%d=>%d)", delta, self.currentIndex , index);
219
+ // NSLog(@"%f (%d=>%d)", delta, self.currentImageIndex , index);
214
220
215
221
if (delta > 0 ) {
216
222
// the current page moved to right
217
- self.currentIndex = self.currentIndex +1 ; // no check (no over case)
223
+ self.currentImageIndex = self.currentImageIndex +1 ;
224
+ self.contentOffsetIndex = self.contentOffsetIndex +1 ;
218
225
[self setupNextImage ];
219
226
220
227
} else {
221
228
// the current page moved to left
222
- self.currentIndex = self.currentIndex -1 ; // no check (no over case)
229
+ self.currentImageIndex = self.currentImageIndex -1 ;
230
+ self.contentOffsetIndex = self.contentOffsetIndex -1 ;
223
231
[self setupPreviousImage ];
224
232
}
225
233
0 commit comments