メインビューにスクロールビューを追加(フレームを適切に設定)
[mainview addSubview:scr];
次のようなメソッドを記述します。
- (void)setupScrollView:(UIScrollView*)scrMain {
}
次に、viewDidLoadスクロールビューをメソッドに渡します。
[self setupScrollView:scr];
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=5; i++) {
// create image
// UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
// create imageView
// NSLog(@"===============>%d",i);
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*5, scrMain.frame.size.height)];
// enable timer after each 2 seconds for scrolling.
// [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:nil userInfo:nil repeats:YES];
}
手動で両方向に移動できます。