我想在UIScrollView上添加一组imageview。我的问题是即使增加x值,图像视图也会彼此重叠。奇怪的是,这个问题只适用于使用ios 4.2.1的设备,但可以在模拟器4.2和其他设备上运行。UIImageView不遵循origin.x位置
这里是我的代码:
- (void)addScrollView {
[self setImages];
scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
[self addSubview:scrollView];
NSUInteger nimages;
CGFloat cx = 0;
for (nimages = 1; nimages < [imagesArray count] ; nimages++) {
UIImage *image = [UIImage imageNamed:[imagesArray objectAtIndex:nimages]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.origin.x = cx;
rect.origin.y = 0;
rect.size.width = 320;
rect.size.height = 480;
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += rect.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
从这里叫做:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addScrollView];
UIImage *handImg;
UIImageView *longhand = [[UIImageView alloc] init];
minuteHand = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 480)];
handImg = [UIImage imageNamed:@"clock_long_hand.png"];
longhand.image = handImg;
/************** TRY COMMENT OUT HERE **************/
longhand.frame = CGRectMake(155 - (handImg.size.width/2), 240 - handImg.size.height, handImg.size.width, handImg.size.height);
[minuteHand addSubview:longhand];
[self addSubview:minuteHand];
/************** END COMMENT OUT HERE **************/
[longhand release];
}
return self;
}
UPDATE:
此代码是在UIView子类这就是为什么我使用[自... ]而不是[self.view ...]。
我尝试过尝试,发现当我注释掉指定的部分时,滚动视图可以正常工作。但是,当它包含时,这是图像视图重叠的时候。
我知道滚动视图的内容大小是正确的,因为它的指标继续滚动,只有一个纯黑色的背景。
我真的很困惑,为什么这对一些ios而不是其他人。希望你能帮助。
我不确定这是什么导致的问题,但在我看来,你有一个称为scrollView的视图控制器的属性(因为你在一点使用self.scrollView)与支持iVar也称为scrollView。当你初始化scrollView时,你使用支持iVar而不是属性,这意味着它不被保留。如果你尝试在每个有scrollView的地方使用self.scrollView,会发生什么? – Darren 2011-12-28 15:49:40
听起来你正在尝试使gellery,你为什么不使用[现有画廊](https://github.com/enormego/PhotoViewer) – 2011-12-28 15:53:05
@Darren我试着用self.scrollView,它似乎没有改变任何东西。我的图像仍然重叠。 – Jebadiah 2011-12-29 01:31:21