我是 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;
@end
视图控制器.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize imgView,scrollView;
- (void)viewDidLoad
{
NSArray *images = [[NSArray alloc] initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg", nil];
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
imgView = [images objectAtIndex:i];
imgView.frame = frame;
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
}
@end
我仍然无法滚动图像。不确定我的错误在哪里?此外,它现在给了我这个例外:
例外:
[__NSCFConstantString setFrame:]: unrecognized selector sent to instance 0x49b8
2012-10-24 16:49:42.411 SwipeImages[3330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setFrame:]: unrecognized selector sent to instance 0x49b8'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1d184bd 0x1c7cbbc 0x1c7c94e 0x2718 0xf3817 0xf3882 0x42a25 0x42dbf 0x42f55 0x4bf67 0x213b 0xf7b7 0xfda7 0x10fab 0x22315 0x2324b 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x107da 0x1265c 0x1d7d 0x1ca5)
libc++abi.dylib: terminate called throwing an exception