我正在尝试在 UIScrollView 上添加一组 ImageView 。我的问题是,即使我增加 x 值, ImageView 也会相互重叠。同样奇怪的是,这个问题仅适用于 ios 4.2.1 的设备,但适用于模拟器 4.2 和其他设备 ios。
这是我的代码:
- (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;
}
更新:
此代码位于 UIView 子类中,这就是为什么我使用 [self...] 而不是 [self.view...]。
我尝试进行实验,发现当我注释掉指示的部分时, ScrollView 工作正常。但是当它被包含时,这就是 ImageView 重叠的时候。
我知道 ScrollView 的内容大小是正确的,因为它的指示器继续滚动,只有纯黑色背景。
我真的很困惑为什么这在某些 ios 上有效而在其他 ios 上无效。希望您能帮忙。
最佳答案
这可能与自动调整蒙版大小有关。实际图像的宽度,我的意思是 .png 文件大于 320,对吗?
关于iphone - UIImageView 不遵循 origin.x 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657674/