0

当我用代码同时点击或按住两个 UILabel 时,我想在两个 UILabel 之间画一条线,我不知道该怎么做,如果有人有建议,将不胜感激,将不胜感激

4

3 回答 3

2

在此处输入图像描述我就是这样做的。顺便说一句,我的答案已经过测试...

为了管理可点击的标签,我将创建我自己的标签,名为 LabelControl,它可以扩展UIControl。这个自定义控件将有一个UILabel(为简单起见只读)作为子视图。通过这样做,我们将能够添加 Target-Action 事件UIControlEventTouchDownUIControlEventTouchUpInside.

这是自定义标签:

.h 文件:

@interface LabelControl : UIControl
@property (nonatomic, retain, readonly) UILabel *theLabel;
@end

.m 文件:

@implementation LabelControl
@synthesize theLabel = _theLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _theLabel.backgroundColor = [UIColor clearColor];
        _theLabel.userInteractionEnabled = NO;
        [self addSubview:_theLabel];
    }
    return self;
}

-(void)dealloc {

    [_theLabel release];
    [super dealloc];
}

然后对于您要绘制的“线条”,我会使用 aUIView因为这样您就可以使用它的hidden属性来隐藏/显示线条。在 ViewController 中添加以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.label1 = [[[LabelControl alloc] initWithFrame:CGRectMake(60, 50, 200, 40)] autorelease];
    self.label1.theLabel.text = @"Hello";
    self.label1.userInteractionEnabled = YES;
    self.label1.backgroundColor = [UIColor blueColor];
    [self.label1 addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
    [self.label1 addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.label1];

    self.label2 = [[[LabelControl alloc] initWithFrame:CGRectMake(60, 150, 200, 40)] autorelease];
    self.label2.theLabel.text = @"World";
    self.label2.userInteractionEnabled = YES;
    self.label2.backgroundColor = [UIColor purpleColor];
    [self.label2 addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
    [self.label2 addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.label2];

    //the position of the line is hard-coded. You will have to modify this yourself
    self.theLine = [[UIView alloc] initWithFrame:CGRectMake(60, 120, 200, 3)];
    self.theLine.backgroundColor = [UIColor blueColor];
    self.theLine.hidden = YES;
    [self.view addSubview:self.theLine];
}

-(void)showOrHideLine {

    if (self.label1.selected && self.label2.selected) {
        NSLog(@"Both are selected");
        self.theLine.hidden = NO;
    } else {
        self.theLine.hidden = YES;
    }
}

-(void)touchDown:(id)sender {
    //When any of the custom label gets the touch down event set the `selected` property
    //to YES. (This property is inherited form `UIControl`
    NSLog(@"Touch down");
    LabelControl *labelControl = (LabelControl*)sender;
    labelControl.selected = YES;
    [self showOrHideLine];
}

-(void)touchUp:(id)sender {
    //When any of the custom label gets the touch up event set the `selected` property
    //to NO. (This property is inherited form `UIControl`
    NSLog(@"Touch Up");
    LabelControl *labelControl = (LabelControl*)sender;
    labelControl.selected = NO;
    [self showOrHideLine];
}

如果您有任何问题,请告诉我。希望这可以帮助!

于 2013-06-10T22:44:24.797 回答
1

我将概述它并给你一些代码(未经测试),但你需要做一些工作才能使完整的效果发挥作用。

以编程方式,您可以子类化UIButton. 在您的新子类中,放置您的触摸识别方法。称它为“customUIButton”之类的东西。

从您的主视图控制器中,创建您的两个实例UIButton并将它们添加为子视图

customUIButton *Label1 = [[customUIButton alloc] init];
[Label1 setTitle:@"Your First Label" forState:UIControlStateNormal];
[Label1 addTarget:self action:@selector(itemClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Label1];

customUIButton *Label2 = [[customUIButton alloc] init];
[Label2 setTitle:@"Your Second Label" forState:UIControlStateNormal];
[Label2 addTarget:self action:@selector(itemClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Label1];

当您按下按钮时,它会尝试调用“itemClicked”方法。制作该方法并接收(id)sender... - (void)itemClicked: (id)sender { 在其中放置一些逻辑以指示正在按下按钮。您可以通过引用发送的发件人来知道哪个按钮。

同样在您的视图控制器中,转到 DrawRect 方法并放入绘图逻辑。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, Label1.center.x, Label1.center.y);
CGContextAddLineToPoint(context, Label2.center.x, Label2.center.y);
CGContextStrokePath(context);
CGContextRestoreGState(context); 

把它放在“if 语句”中,这样​​如果两个按钮都有焦点,它将被绘制(否则跳过绘制代码)。

于 2013-06-10T21:24:26.607 回答
0

如何使用图像视图在 IB 中放置一条线。在 viewDidLoad 方法中隐藏图像视图。然后,每当用户点击其中一个 UILabel 时,取消隐藏包含您的行的图像视图。根据您使用 Objective-C 的经验来检测 UILabel 上的触摸可能会很棘手。如果是这样,您可以在每个标签上放置不可见的按钮,这应该对您有用。有关在 UILabel 上检测触摸的更多信息,请查看此链接: Handling Touch Event in UILabel and hooking it up to an IBAction

于 2013-06-10T20:50:03.880 回答