Skip to content
This repository
Browse code

Coverflow now fluidly lands on content offset point where the frontmo…

…st cover will be centered. Before it would snap back after the scrolling did-stop-decelerating delegate method fired.
  • Loading branch information...
commit db5816b6c3ee8e7d7f5bb4d853ac5e8d7dafac21 1 parent 322d088
Devin Ross authored July 23, 2012
14  demo/iPhone/CoverflowViewController.m
@@ -197,13 +197,10 @@ - (TKCoverflowCoverView*) coverflowView:(TKCoverflowView*)coverflowView coverAtI
197 197
 	
198 198
 	TKCoverflowCoverView *cover = [coverflowView dequeueReusableCoverView];
199 199
 	
200  
-	
201  
-	
202 200
 	if(cover == nil){
203 201
 		BOOL phone = [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone;
204 202
 		CGRect rect = phone ? CGRectMake(0, 0, 224, 300) : CGRectMake(0, 0, 300, 600);
205 203
 
206  
-		
207 204
 		cover = [[[TKCoverflowCoverView alloc] initWithFrame:rect] autorelease]; // 224
208 205
 		cover.baseline = 224;
209 206
 		
@@ -213,9 +210,13 @@ - (TKCoverflowCoverView*) coverflowView:(TKCoverflowView*)coverflowView coverAtI
213 210
 	return cover;
214 211
 	
215 212
 }
216  
-- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{
  213
+
  214
+- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasTappedInFront:(int)index tapCount:(NSInteger)tapCount{
217 215
 	
  216
+	TKLog(@"Index: %d",index);
218 217
 	
  218
+	if(tapCount<2) return;
  219
+
219 220
 	TKCoverflowCoverView *cover = [coverflowView coverAtIndex:index];
220 221
 	if(cover == nil) return;
221 222
 	[UIView beginAnimations:nil context:nil];
@@ -223,10 +224,7 @@ - (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapp
223 224
 	[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cover cache:YES];
224 225
 	[UIView commitAnimations];
225 226
 	
226  
-	NSLog(@"Index: %d",index);
227  
-	
228  
-
229  
-	
230 227
 }
231 228
 
  229
+
232 230
 @end
102  src/TapkuLibrary/TKCoverflowView.m
@@ -292,31 +292,23 @@ - (id) initWithFrame:(CGRect)frame {
292 292
     return self;
293 293
 }
294 294
 
295  
-
296  
-- (void) layoutSubviews{
  295
+- (void) _adjustToBounds{
297 296
 	
298  
-	if(self.frame.size.width == currentSize.width && self.frame.size.height == currentSize.height) return;
299 297
 	currentSize = self.frame.size;
300 298
 	
301  
-	
302  
-
303  
-	
304 299
 	margin = (self.frame.size.width / 2);
305 300
 	self.contentSize = CGSizeMake( (coverSpacing) * (numberOfCovers-1) + (margin*2) , self.frame.size.height);
306 301
 	coverBuffer = (int)((currentSize.width - coverSize.width) / coverSpacing) + 3;
307 302
 	
308 303
 	
309  
-
310 304
 	for(UIView *v in views){
311 305
 		v.layer.transform = CATransform3DIdentity;
312 306
 		CGRect r = v.frame;
313 307
 		r.origin.y = currentSize.height / 2 - (coverSize.height/2) - (coverSize.height/16);
314 308
 		v.frame = r;
315  
-
316 309
 	}
317  
-
  310
+	
318 311
 	for(int i= deck.location; i < deck.location + deck.length; i++){
319  
-		
320 312
 		if([coverViews objectAtIndex:i] != [NSNull null]){
321 313
 			UIView *cover = [coverViews objectAtIndex:i];
322 314
 			CGRect r = cover.frame;
@@ -325,11 +317,57 @@ - (void) layoutSubviews{
325 317
 		}
326 318
 	}
327 319
 	
  320
+	[self newrange];
  321
+	[self animateToIndex:currentIndex animated:NO];
  322
+}
  323
+- (void) setFrame:(CGRect)frame{
  324
+	[super setFrame:frame];
  325
+	[self _adjustToBounds];
  326
+}
  327
+- (void) setBounds:(CGRect)bounds{
  328
+	[super setBounds:bounds];
  329
+	[self _adjustToBounds];
  330
+}
  331
+
328 332
 
  333
+- (NSInteger) calculatedIndexWithContentOffset:(CGPoint)point{
  334
+	CGFloat num = numberOfCovers;
  335
+	CGFloat per = point.x / (self.contentSize.width - currentSize.width);
  336
+	CGFloat ind = num * per;
  337
+	CGFloat mi = ind / (numberOfCovers/2);
  338
+	mi = 1 - mi;
  339
+	mi = mi / 2;
  340
+	int index = (int)(ind+mi);
  341
+	index = MIN(MAX(0,index),numberOfCovers-1);
  342
+	
  343
+	return index;
  344
+}
329 345
 
  346
+- (void) layoutSubviews{
  347
+	
  348
+	velocity = abs(pos - self.contentOffset.x);
  349
+	pos = self.contentOffset.x;
  350
+	movingRight = self.contentOffset.x - origin > 0 ? YES : NO;
  351
+	origin = self.contentOffset.x;
  352
+	
  353
+	CGFloat num = numberOfCovers;
  354
+	CGFloat per = self.contentOffset.x / (self.contentSize.width - currentSize.width);
  355
+	CGFloat ind = num * per;
  356
+	CGFloat mi = ind / (numberOfCovers/2);
  357
+	mi = 1 - mi;
  358
+	mi = mi / 2;
  359
+	int index = (int)(ind+mi);
  360
+	index = MIN(MAX(0,index),numberOfCovers-1);
  361
+	
  362
+	
  363
+	if(index == currentIndex) return;
  364
+	
  365
+	currentIndex = index;
330 366
 	[self newrange];
331  
-	[self animateToIndex:currentIndex animated:NO];
332 367
 	
  368
+	
  369
+	if(velocity < 180 || currentIndex < 15 || currentIndex > (numberOfCovers - 16))
  370
+		[self animateToIndex:index animated:YES];
333 371
 
334 372
 }
335 373
 
@@ -398,46 +436,8 @@ - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
398 436
 }
399 437
 
400 438
 #pragma mark UIScrollView Delegate
401  
-- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
402  
-    
403  
-    
404  
-	
405  
-	velocity = abs(pos - scrollView.contentOffset.x);
406  
-	pos = scrollView.contentOffset.x;
407  
-	movingRight = self.contentOffset.x - origin > 0 ? YES : NO;
408  
-	origin = self.contentOffset.x;
409  
-
410  
-	CGFloat num = numberOfCovers;
411  
-	CGFloat per = scrollView.contentOffset.x / (self.contentSize.width - currentSize.width);
412  
-	CGFloat ind = num * per;
413  
-	CGFloat mi = ind / (numberOfCovers/2);
414  
-	mi = 1 - mi;
415  
-	mi = mi / 2;
416  
-	int index = (int)(ind+mi);
417  
-	index = MIN(MAX(0,index),numberOfCovers-1);
418  
-	
419  
-
420  
-	if(index == currentIndex) return;
421  
-	
422  
-	currentIndex = index;
423  
-	[self newrange];
424  
-	
425  
-	
426  
-	if(velocity < 180 || currentIndex < 15 || currentIndex > (numberOfCovers - 16))
427  
-		[self animateToIndex:index animated:YES];
428  
-	
429  
-}
430  
-- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
431  
-	if(!scrollView.tracking && !scrollView.decelerating){
432  
-		[self snapToAlbum:YES];
433  
-		[self adjustViewHeirarchy];
434  
-	} 
435  
-}
436  
-- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
437  
-	if(!self.decelerating && !decelerate){
438  
-		[self snapToAlbum:YES];
439  
-		[self adjustViewHeirarchy];
440  
-	}
  439
+- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
  440
+	*targetContentOffset = CGPointMake(coverSpacing * [self calculatedIndexWithContentOffset:*targetContentOffset], 0);
441 441
 }
442 442
 
443 443
 

0 notes on commit db5816b

Please sign in to comment.
Something went wrong with that request. Please try again.