我创建了一个 UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *image2 = [UIImage imageNamed:@"pinGreen_v1.png"];
UIImage *image1 = [UIImage imageNamed:@"PinDown1.png"];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
for(int i =1; i<20; i++){
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self
action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
}
return self;
}
使用上面的代码,我正在 View 中绘制图像。我正在从我的 View Controller 加载这个 View 。我需要将此 View 放入
UIScrollview
.我应该在哪里创建 ScrollView ?在上述 View 或创建上述 View 的 View Controller 中。我的 View Controller 内的代码是:
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[drawLines release];
我应该能够滚动浏览 View 内的图像。
最佳答案
我会在 View Controller 中创建 ScrollView 并将 View 添加到其中。
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[scroll addSubview:drawLines];
scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];
关于iphone - 向 UIScrollview 添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9348406/