I am a newbie to iphone development and i am doing a simple app for practice. In that app,there is only one view that has an image on it. I want to be able to swipe left or right and have the first image go out of the view and the second image to come in. The images are different and I want it to look like they are connected. I need it to be able to change the image or restart after a series of swipes. I am doing this using ScrollView only.
I have tried something:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIScrollViewDelegate>
{
UIImageView *imgView;
UIScrollView *scrollView;
}
@property (nonatomic,retain)IBOutlet UIImageView *imgView;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
//- (id)initWithPageNumber:(int)page;
- (IBAction)changePage:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize imgView,scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
- (id)initWithPageNumber:(int)page{
if (self = [super initWithNibName:@"MyView" bundle:nil])
{
pageNumber = page;
}
return self;
}
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview
{
scrollView.contentOffset = CGPointMake(scrollview.bounds.size.width, 0);
}
- (IBAction)changePage:(id)sender
{
NSArray *images = [[NSArray alloc] initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg", nil];
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
CGRect cRect = scrollView.bounds;
for (int i = 0; i < images.count; i++){
imgView = [images objectAtIndex:i];
imgView.frame = cRect;
[scrollView addSubview:imgView];
cRect.origin.x += cRect.size.width;
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * images.count, scrollView.bounds.size.height);
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
}
@end
When i run the app, I am still not able to swipe across the images. Am i doing something wrong? Need some guidance on this. Thanks..
This error indicates that you have a connection in Interface Builder with an
IBOutletthat does not exist in your view controller class.Make sure your IBOutlets are named correctly and that you have removed all obsolete connections in IB.