我是 iphone 开发的新手,我正在做一个简单的应用程序来练习。在该应用程序中,只有一个视图上有图像。我希望能够向左或向右滑动,让第一个图像离开视图,第二个图像进入。图像是不同的,我希望它看起来像它们是连接的。我需要它能够在一系列滑动后更改图像或重新启动。我只使用 ScrollView 来执行此操作。
我尝试了一些东西:
视图控制器.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
视图控制器.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
当我运行该应用程序时,我仍然无法在图像上滑动。难道我做错了什么?在这方面需要一些指导。谢谢..