1

こんにちは、Interface Builder を使用して UIView に 2 つのボタンを追加し、これら 2 つのボタンの間に線を引きたいと思います。これはどのように可能ですか?提案してください。ありがとう

4

1 に答える 1

3

drawrect を使用しない簡単なオプションは、2 つのボタンの間に薄い 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
于 2010-12-09T13:26:55.197 に答える