我创建了一个滚动视图,但我想让它像页面视图一样。
- (void)setupScrollView:(UIScrollView*)scrMain {
for (int i=1; i<=11; i++) {
image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
if (i==1) {
imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 2, 289, 470)];
}
else
imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*350, 2, 289, 470)];
imgV.contentMode=UIViewContentModeScaleToFill;
[imgV setImage:image];
imgV.tag=i+1;
[imgV bringSubviewToFront:self.aleatoire];
[scrMain addSubview:imgV];
UIImageView *aleatoire=[[UIImageView alloc]initWithFrame:CGRectMake (self.aleatoire.frame.origin.x-19,self.aleatoire.frame.origin.y,self.aleatoire.frame.size.width,self.aleatoire.frame.size.height)];
aleatoire.image=self.aleatoire.image;
[imgV addSubview:aleatoire];
[imgV bringSubviewToFront:aleatoire];
UIButton *rand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rand addTarget:self
action:@selector(randomBtn:)
forControlEvents:UIControlEventTouchUpInside];
rand.frame=aleatoire.frame;
rand.backgroundColor=[UIColor clearColor];
rand.userInteractionEnabled = YES;
imgV.userInteractionEnabled=YES;
[imgV addSubview:rand];
[imgV bringSubviewToFront:rand];
}
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*11, scrMain.frame.size.height)];
}
下一张图片不在中心。我错的任何想法?
答案 0 :(得分:1)
您设置UIScrollView
分页pagingEnabled
属性启用以创建类似pageView的滚动行为。
试试这段代码:
scrMain.pagingEnabled=YES;
scrMain.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0); // set your size to scroll content
在UIScrollView
int x = 0;
for (int i=1; i<=11; i++) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height)];
img.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
[self.scrollView addSubview:img];
x = x + [[UIScreen mainScreen] bounds].size.width;
}
self.scrollView.contentSize = CGSizeMake(x, [[UIScreen mainScreen] bounds].size.height-64);
self.scrollView.pagingEnabled = YES;
self.scrollView.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0);