向 UIScrollview 添加视图

发布于 2025-01-07 04:31:04 字数 1292 浏览 3 评论 0原文

我创建了一个 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;
}

使用上面的代码在我的视图中绘制图像。我正在从我的视图控制器加载此视图。我需要将此视图放入 UIScrollview 中。我应该在哪里创建滚动视图?在上面的视图或创建上面视图的视图控制器中。

我的视图控制器内的代码是:

 DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
 drawLines.backgroundColor = [UIColor blackColor];
 [self.view addSubview:drawLines];
 [drawLines release];

我应该能够滚动视图内的图像。

I have created a 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;
}

With the above code I am drawing images in my view. I am loading this view from my view controller. I need to put this view inside a UIScrollview. Where should I create the scroll view? In the above view or the view controller where the above view is created.

The code inside my view controller is :

 DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
 drawLines.backgroundColor = [UIColor blackColor];
 [self.view addSubview:drawLines];
 [drawLines release];

I should be able to scroll through the images inside my view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2025-01-14 04:31:04

我将在视图控制器中创建滚动视图并将视图添加到其中。

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];

I would create the the scrollview in the viewcontroller and add the view to it.

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文