在 UIView 中绘制以及 addSubview

发布于 2024-10-06 16:06:02 字数 76 浏览 0 评论 0原文

你好 我想使用 Interface Builder 在 UIView 上添加两个按钮,并在这两个按钮之间画一条线。这怎么可能?请建议。谢谢

Hi
I want add two buttons on a UIView using Interface Builder, and draw a line between these two buttons. How is this possible? Please suggest. Thanks

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

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

发布评论

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

评论(1

傲性难收 2024-10-13 16:06:02

不使用drawrect的一个简单选择是在两个按钮之间放置一个薄的UIView。将视图的背景颜色设置为您想要的线条颜色。

编辑:
您仍然可以使用精简 UIView 并根据需要切换其可见性。
如果您仍然想绘制线条,您可能需要创建一个自定义 UIView。

@interface CustomView : UIView {
    UIButton *button1;
    UIButton *button2;
}
@end

@implementation CustomView

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn1.frame = CGRectMake(20, 5, 80, 30);
        [btn1 setTitle:@"Button1" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(buttonPressed:) 
                forControlEvents:UIControlEventTouchUpInside];
        button1 = btn1;
        [self addSubview:btn1];

        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn2.frame = CGRectMake(20, 60, 80, 30);
        [btn2 setTitle:@"Button2" forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(buttonPressed:) 
                forControlEvents:UIControlEventTouchUpInside];
        button2 = btn2;
        [self addSubview:btn2];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (button1.selected)
        CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    else
        CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 10.0, 45.0);
    CGContextAddLineToPoint(context, 150.0, 45.0);
    if (button1.selected)
    {
        CGContextAddLineToPoint(context, 180.0, 35.0);      
    }
    CGContextDrawPath(context, kCGPathStroke);
}

-(void)buttonPressed:(UIButton *)button
{
    button1.selected = !button1.selected;
    button2.selected = !button2.selected;
    [self setNeedsDisplay];
}

@end

A simple option without using drawrect is to just put a thin UIView between the two buttons. Set the view's background color to the line color you want.

Edit:
You could still use thin UIViews and toggle their visibility as needed.
If you still want to draw the lines, you might want to create a custom UIView.

@interface CustomView : UIView {
    UIButton *button1;
    UIButton *button2;
}
@end

@implementation CustomView

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn1.frame = CGRectMake(20, 5, 80, 30);
        [btn1 setTitle:@"Button1" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(buttonPressed:) 
                forControlEvents:UIControlEventTouchUpInside];
        button1 = btn1;
        [self addSubview:btn1];

        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn2.frame = CGRectMake(20, 60, 80, 30);
        [btn2 setTitle:@"Button2" forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(buttonPressed:) 
                forControlEvents:UIControlEventTouchUpInside];
        button2 = btn2;
        [self addSubview:btn2];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (button1.selected)
        CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    else
        CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 10.0, 45.0);
    CGContextAddLineToPoint(context, 150.0, 45.0);
    if (button1.selected)
    {
        CGContextAddLineToPoint(context, 180.0, 35.0);      
    }
    CGContextDrawPath(context, kCGPathStroke);
}

-(void)buttonPressed:(UIButton *)button
{
    button1.selected = !button1.selected;
    button2.selected = !button2.selected;
    [self setNeedsDisplay];
}

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