Created
December 13, 2013 08:27
-
-
Save polikeiji/7941321 to your computer and use it in GitHub Desktop.
UIImageViewを3つ使い回して、iOSデフォルトの「写真」アプリみたいに、たくさんの写真を横向きに見ていく為のコード。
「scrollViewWillEndDragging」のUIScrollViewDelegateを使って書いた。
iOS Scroll Viewプログラミングガイドには、「scrollViewDidScroll」のデリゲートを使うって書いてあったけど、それだと早く連続で写真をめくるときの処理がうまくかけなかった。 なお、UIImageのloadImageFromPathは、自分でカテゴリを使って追加していて、非同期で画像を落としてくるスタティックメソッド。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implement MyScrollViewDelegate | |
{ | |
UIImageView *currentImageView; | |
UIImageView *nextImageView; | |
UIImageView *previousImageView; | |
NSInteger currentPhotoIndex; | |
NSArray *photos; | |
} | |
- (void)resetPhotoSlideImageViews | |
{ | |
CGFloat pageWidth = self.photoSlideScrollView.frame.size.width; | |
CGFloat currentImageViewX = pageWidth * currentPhotoIndex; | |
self.photoSlideScrollView.contentOffset = CGPointMake(currentImageViewX, 0); | |
currentImageView.frame = CGRectMake(currentImageViewX, 0, currentImageView.frame.size.width, currentImageView.frame.size.height); | |
[UIImage loadImageFromPath:[[photos objectAtIndex:currentPhotoIndex] objectForKey:@"image_path"] callback:^(UIImage *loadedImage) { | |
currentImageView.image = loadedImage; | |
}]; | |
if (currentPhotoIndex > 0) { | |
previousImageView.frame = CGRectMake(currentImageViewX - pageWidth, 0, previousImageView.frame.size.width, previousImageView.frame.size.height); | |
[UIImage loadImageFromPath:[[photos objectAtIndex:currentPhotoIndex - 1] objectForKey:@"image_path"] callback:^(UIImage *loadedImage) { | |
previousImageView.image = loadedImage; | |
}]; | |
} | |
if (currentPhotoIndex + 1 < photos.count) { | |
nextImageView.frame = CGRectMake(currentImageViewX + pageWidth, 0, nextImageView.frame.size.width, nextImageView.frame.size.height); | |
[UIImage loadImageFromPath:[[photos objectAtIndex:currentPhotoIndex + 1] objectForKey:@"image_path"] callback:^(UIImage *loadedImage) { | |
nextImageView.image = loadedImage; | |
}]; | |
} | |
[self updatePhotoSlideInformation]; | |
} | |
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset | |
{ | |
CGFloat originalX = scrollView.frame.size.width * currentPhotoIndex; | |
if (currentPhotoIndex + 1 < photos.count && scrollView.contentOffset.x > originalX && velocity.x > 0) { | |
NSLog(@"next"); | |
currentPhotoIndex++; | |
if (currentPhotoIndex + 1 < photos.count) { | |
previousImageView.frame = CGRectMake(previousImageView.frame.size.width * (currentPhotoIndex + 1), 0, previousImageView.frame.size.width, previousImageView.frame.size.height); | |
[UIImage loadImageFromPath:[[photos objectAtIndex:currentPhotoIndex + 1] objectForKey:@"image_path"] callback:^(UIImage *loadedImage) { | |
previousImageView.image = loadedImage; | |
}]; | |
} | |
UIImageView *temporaryNextImageView = previousImageView; | |
previousImageView = currentImageView; | |
currentImageView = nextImageView; | |
nextImageView = temporaryNextImageView; | |
} | |
else if (currentPhotoIndex > 0 && scrollView.contentOffset.x < originalX && velocity.x < 0) { | |
NSLog(@"prev"); | |
currentPhotoIndex--; | |
if (currentPhotoIndex > 0) { | |
nextImageView.frame = CGRectMake(nextImageView.frame.size.width * (currentPhotoIndex - 1), 0, nextImageView.frame.size.width, nextImageView.frame.size.height); | |
[UIImage loadImageFromPath:[[photos objectAtIndex:currentPhotoIndex - 1] objectForKey:@"image_path"] callback:^(UIImage *loadedImage) { | |
nextImageView.image = loadedImage; | |
}]; | |
} | |
UIImageView *temporaryPreviousImageView = nextImageView; | |
nextImageView = currentImageView; | |
currentImageView = previousImageView; | |
previousImageView = temporaryPreviousImageView; | |
} | |
else { | |
NSLog(@"stay"); | |
} | |
self.photoSlideAdjuster.value = currentPhotoIndex; | |
[self updatePhotoSlideInformation]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment