This is the simple example of SnapShow. We are using a UIScrollView and UIImageViews in this application.
Step 1: Create a new project in Xcode using View-Base application.
Step 2: Add code in your header file :
@interface SnapShowViewController : UIViewController {
}
- (id)init;
Step 3: In the viewcontroller class impliment the below code:
- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
if (inImageIndex >= [Images count])
{
return nil;
}
UIImageView * result = [[UIImageView alloc] initWithImage:[Images objectAtIndex:inImageIndex]];
result.opaque = YES;
result.userInteractionEnabled = NO;
result.backgroundColor = [UIColor blackColor];
result.contentMode = UIViewContentModeScaleAspectFit;
result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return result;
}
- (id)initWithImages:(NSArray *)inImages
{
if (self = [super initWithFrame:CGRectZero])
{
Images = [inImages retain];
NSUInteger imageCount = [inImages count];
if (imageCount > 0)
{
mCurrentImageView = [self createImageView:0];
[self addSubview:mCurrentImageView];
if (imageCount > 1)
{
RightImage = [self createImageView:1];
[self addSubview:RightImage];
}
}
self.opaque = YES;
self.backgroundColor = [UIColor blackColor];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return self;
}
Step 4: We need to impliment the touch function in the controller file for move the image.
{
if (mSwiping)
return;
CGSize contentSize = self.frame.size;
LeftImage.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
RightImage.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] != 1)
return;
mSwipeStart = [[touches anyObject] locationInView:self].x;
mSwiping = YES;
LeftImage.hidden = NO;
mCurrentImageView.hidden = NO;
RightImage.hidden = NO;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (! mSwiping || [touches count] != 1)
return;
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
CGSize contentSize = self.frame.size;
LeftImage.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, contentSize.width, contentSize.height);
RightImage.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (! mSwiping)
return;
CGSize contentSize = self.frame.size;
NSUInteger count = [Images count];
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
if (mCurrentImage > 0 && swipeDistance > 50.0f)
{
[RightImage removeFromSuperview];
[RightImage release];
RightImage = mCurrentImageView;
mCurrentImageView = LeftImage;
mCurrentImage–;
if (mCurrentImage > 0)
{
LeftImage = [self createImageView:mCurrentImage - 1];
LeftImage.hidden = YES;
[self addSubview:LeftImage];
}
else
{
LeftImage = nil;
}
}
else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
{
[LeftImage removeFromSuperview];
[LeftImage release];
LeftImage = mCurrentImageView;
mCurrentImageView = RightImage;
mCurrentImage++;
if (mCurrentImage < count - 1)
{
RightImage = [self createImageView:mCurrentImage + 1];
RightImage.hidden = YES;
[self addSubview:RightImage];
}
else
{
RightImage = nil;
}
}
[UIView beginAnimations:@"swipe" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3f];
LeftImage.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
RightImage.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
[UIView commitAnimations];
mSwiping = NO;
}
Step 5: Now we are going to add image in the code.
{@implementation SnapShowViewController
- (id)init
{
if (self = [super initWithNibName:nil bundle:nil])
{
NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"11.jpg"],
[UIImage imageNamed:@"22.jpg"],
[UIImage imageNamed:@"33.jpg"],
[UIImage imageNamed:@"44.jpg"],
[UIImage imageNamed:@"55.jpg"], nil];
self.view = [[[SnapShowView alloc] initWithImages:images] autorelease];
}
return self;
}
return self;
}
Step 6: Now compile your application and run on the simulator. (See figure below)
Figure 1: SnapShow in the Simulator
You can downloaded SourceCode from here SnapShow 2











