コードで両方を同時にタップまたはホールドすると、2つのUILabelsの間に線を引きたいのですが、これを行う方法がわかりません。誰かにアドバイスがあれば大歓迎です。
3 に答える
2
これは私がそれを行う方法です。私の答えはところでテストされています...
タップ可能なラベルを管理するには、LabelControl と呼ばれる独自のラベルを作成しますUIControl
。このカスタム コントロールにはUILabel
、サブビューとして (簡単にするために読み取り専用) があります。これにより、Target-Action イベントUIControlEventTouchDown
とUIControlEventTouchUpInside
.
カスタムラベルは次のとおりです。
.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
できるため、 a を使用します。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 に答える