gpt4 book ai didi

ios - 一起点击或按下时在两个 UIlabel 之间画一条线

转载 作者:行者123 更新时间:2023-11-28 20:18:04 24 4
gpt4 key购买 nike

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

最佳答案

enter image description here我就是这样做的。顺便说一句,我的答案经过测试......

为了管理可点击标签,我将创建自己的名为 LabelControl 的标签,它扩展了 UIControl。这个自定义控件将有一个 UILabel(为简单起见只读)作为 subview 。通过这样做,我们将能够添加 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];
}

然后对于你想画的“线”,我会使用 UIView 因为这样你就可以使用它的 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];
}

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

关于ios - 一起点击或按下时在两个 UIlabel 之间画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com