Archive for December 8th, 2012
Scrolling Images in Image Gallery iphone
.h file
#import <UIKit/UIKit.h>
@interface ScrollingImageViewController : UIViewController {
IBOutlet UIScrollView *FirstScrollView;
}
@property (nonatomic, retain) UIView *FirstScrollView;
@end
.m file
#import “ScrollingImageViewController.h”
@implementation ScrollingImageViewController
@synthesize FirstScrollView;
const CGFloat kScrollObjHeight = 353.0;
const CGFloat kScrollObjWidth = 258.0;
const NSUInteger kNumImages = 3;
– (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [FirstScrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[FirstScrollView setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [FirstScrollView bounds].size.height)];
}
– (void)viewDidLoad
{
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[FirstScrollView setBackgroundColor:[UIColor blackColor]];
[FirstScrollView setCanCancelContentTouches:NO];
FirstScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
FirstScrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
FirstScrollView.scrollEnabled = YES;
FirstScrollView.pagingEnabled = YES;
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@”Snap%d.jpg”, i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[FirstScrollView addSubview:imageView];
[imageView release];
}
[self layoutScrollImages];
}
– (void)dealloc
{
[FirstScrollView release];
[super dealloc];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
in .xib file add a scroll view with (353.0,258.0) frame