我想創建一個簡單的應用程序,該應用程序將在第一個啓動屏幕上包含一個居中圖像,而不是在滑動手勢(右側,左側)時更改圖像。嘗試使用UIScrollView顯示圖像
我對此很陌生,所以這裏是我正在尋找的東西http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/。
這是在我的控制器實現的代碼,我有:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:@"img1.jpg" forKey:@"http://www.testweb.com"];
[myDictionary setObject:@"img2.jpg" forKey:@"http://www.test2.com"];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = [myDictionary count];
for (NSString* key in myDictionary) {
UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:[myDictionary objectForKey:key]]];
CGRect rect = CGRectMake(10.0f, 90.0f, image.size.width, image.size.height);
UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setImage:image];
CGPoint superCenter = CGPointMake([self.view bounds].size.width/2.0, [self.view bounds].size.height/2.0);
[imageView setCenter:superCenter];
self.view.backgroundColor = [UIColor whiteColor];
[scroll addSubview:imageView];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
}
我第一次在這裏的問題是,我得到img2
我最初的屏幕,而不是img1
上。第二個問題是當我向右滑動時,我得到白色屏幕,沒有圖像。任何建議,我錯過了什麼,我可以嘗試/閱讀等?
編輯:
我要做到這一點的「最輕」可能的方式,不使用花哨的畫廊API等只顯示幾個中心的屏幕上真正的小圖像(即200×200像素)的那我可以來回滑動(不要太難)。學習一門新語言時一切都很艱難。
請參考以下鏈接:滾動例 http://developer.apple.com/library/ios/#samplecode/Scrolling/Introduction/Intro.html – user11 2012-08-17 09:08:41