2017-04-21 37 views
1

我有一些文本标签的两列。当我触摸我的第二列的任何标签时,如何获得第三列标签附加到第一列。 这里是代码列1和列2标签如何定位相对于与触摸其他标签的标签的五个人副本的目标C

column1的标签

for(int i=0;i<length;i++) 
{ 


    _lbl = [[UILabel alloc]initWithFrame:rect1]; 
    // lbl.frame=CGRectMake(x, y, width+10, 30); 
    _lbl.center = CGPointMake(x, y); 
    _lbl.tag=i; 
    _lbl.textAlignment=NSTextAlignmentCenter; 
    _lbl.backgroundColor=[UIColor blueColor]; 
    _lbl.textColor=[UIColor whiteColor]; 
    _lbl.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0]; 
    //label.clipsToBounds=YES; 
    //label.layer.cornerRadius=5.0; 
    [self.gameLayer addSubview:_lbl]; 
    _lbl.userInteractionEnabled = YES; 
    [email protected]"Text1"; 
    [_lbl sizeToFit]; 

列2为标签

for(int i=0;i<length;i++) 
{ 


    _lbl2 = [[UILabel alloc]initWithFrame:rect]; 
    // _lbl2.frame=CGRectMake(x, y, width+10, 30); 
    _lbl2.center = CGPointMake(x+20, y); 
    _lbl2.tag=i+5; 
    _lbl2.textAlignment=NSTextAlignmentCenter; 
    _lbl2.backgroundColor=[UIColor greenColor]; 
    _lbl2.textColor=[UIColor whiteColor]; 
    _lbl2.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0]; 
    //_lbl2.clipsToBounds=YES; 
    //_lbl2.layer.cornerRadius=5.0; 
    [self.gameLayer addSubview:_lbl2]; 
    _lbl.userInteractionEnabled = YES; 
    [email protected]"Text2"; 
    [_lbl2 sizeToFit]; 

在触摸Begin方法我调用一个方法makeThirdColumn

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

     { 
     CGPoint pt = [[touches anyObject] locationInView:self.superview]; 
     _xOffset = pt.x - self.center.x; 
     _yOffset = pt.y - self.center.y; 
     [self makeThirdColumn]; 

I,没有在要求的位置获得第三列标签在此图像中只有两列enter image description here

我想与COLUMN1文本1标签的第三列concate。

+0

基于您的代码......首先,创建5个标签没有文字,所以只是简单的绿色长方形,都在彼此的顶部,这样的结果就是One绿色框。其次,您也可以对5个红色标签进行相同处理,并重新放在另一个上面,从而产生一个红色框。第三,你通过设置框,然后改变坐标系的原点,然后改变整个帧,再对所有彼此的顶部创建5个标签,每个时间。另外...你不会创建任何对你添加的标签的引用,所以在“第三行”中没有label1或label2。 – DonMag

+0

我会建议---第一步:编写正确创建前两个“行”标签的代码。然后编辑显示*代码的问题,再加上它的外观图像,并显示您希望显示第三行标签的位置。 – DonMag

+0

哦,我看到它让我更新我的代码 – Flying

回答

0

@Moon - 我仍然不完全知道你正在尝试做的,但希望这将有助于你对你的方式。

为了让它更清楚些,我使用了列名为A,B和C的列,而不是1,2和3.这就是结果 - 当您点击时,红色“列C”标签将显示或隐藏灰色的“B列”标签。

enter image description here

如果这不会做你想做的(或者,如果你不能找出如何让这个做你想做的),你将需要在描述更加清晰的目标。


// LabelColumnsViewController.h 

#import <UIKit/UIKit.h> 

@interface LabelColumnsViewController : UIViewController 

@end 

// LabelColumnsViewController.m 

#import "LabelColumnsViewController.h" 

@interface LabelColumnsViewController() 

@property (strong, nonatomic) UIView *gameLayer; 

@property (strong, nonatomic) NSMutableArray *columnA; 
@property (strong, nonatomic) NSMutableArray *columnB; 
@property (strong, nonatomic) NSMutableArray *columnC; 

@end 

@implementation LabelColumnsViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // add a UIView to hold the Labels 
    _gameLayer = [[UIView alloc] initWithFrame:self.view.frame]; 
    _gameLayer.backgroundColor = [UIColor orangeColor]; 
    [self.view addSubview:_gameLayer]; 

    // initialize arrays to hold references to the labels 
    _columnA = [NSMutableArray array]; 
    _columnB = [NSMutableArray array]; 
    _columnC = [NSMutableArray array]; 

    // set rows to 5 
    int length = 5; 

    // top of top row 
    CGFloat y1 = 100.0; 

    // left of Column A 
    CGFloat x1 = 20.0; 

    // left of Column B 
    CGFloat x2 = 220.0; 

    // Vertical spacing for rows of labels 
    CGFloat yInc = 40.0; 

    // initialize a CGRect 
    CGRect rect1 = CGRectMake(x1, y1, 100, 30); 

    for (int i = 1; i < length; i++) 
    { 

     // Create UILabel for Column A 
     UILabel *lblA = [[UILabel alloc] initWithFrame:rect1]; 
     lblA.tag = 100+i; 
     lblA.textAlignment = NSTextAlignmentCenter; 
     lblA.backgroundColor = [UIColor blueColor]; 
     lblA.textColor = [UIColor whiteColor]; 
     lblA.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0]; 
     [self.gameLayer addSubview:lblA]; 
     lblA.userInteractionEnabled = YES; 
     [email protected]"Text1"; 
     [lblA sizeToFit]; 


     // "move" the rectangle, so the next label starts at column B 
     rect1.origin.x = x2; 

     // Create UILabel for Column B 
     UILabel *lblB = [[UILabel alloc] initWithFrame:rect1]; 
     lblB.tag = 200+i; 
     lblB.textAlignment = NSTextAlignmentCenter; 
     lblB.backgroundColor = [UIColor grayColor]; 
     lblB.textColor = [UIColor whiteColor]; 
     lblB.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0]; 
     [self.gameLayer addSubview:lblB]; 
     lblB.userInteractionEnabled = YES; 
     [email protected]"Text2"; 
     [lblB sizeToFit]; 

     // add a Tap Gesture Recognizer to the label 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)]; 
     [lblB addGestureRecognizer:tap]; 


     // "move" the rectangle, so the next label starts at column C 
     // this is the right-edge of the Column A label 
     rect1.origin.x = lblA.frame.origin.x + lblA.frame.size.width; 

     // Create UILabel for Column C 
     UILabel *lblC = [[UILabel alloc] initWithFrame:rect1]; 
     lblC.tag = 300+i; 
     lblC.textAlignment = NSTextAlignmentCenter; 
     lblC.backgroundColor = [UIColor redColor]; 
     lblC.textColor = [UIColor whiteColor]; 
     lblC.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0]; 
     [self.gameLayer addSubview:lblC]; 
     lblC.userInteractionEnabled = YES; 
     [email protected]"Text3"; 
     [lblC sizeToFit]; 

     // Column C is initially Hidden 
     lblC.hidden = YES; 


     // add the labels to their Arrays so we can access them later 
     [_columnA addObject:lblA]; 
     [_columnB addObject:lblB]; 
     [_columnC addObject:lblC]; 


     // reset left of rect to Column A, and increment y (for next row) 
     rect1.origin.x = x1; 
     rect1.origin.y += yInc; 

    } 

} 

- (void) gotTapped:(id)sender { 
    // a label in Column B was tapped, so show Column C labels if they were hidden, 
    //  or hide them if they were visible 
    for (UILabel *v in _columnC) { 
     v.hidden = !v.hidden; 
    } 
} 

@end 
+0

你是对的,但它解决了我的一半问题。我的问题是如何我使cloumnC标签的大小与ColumnB标签相同。假设我的columnB标签有一些rendom字符串和它的大小根据文本,那么我如何在列C中的任何标签上点击时在ColumnC中生成相同大小的标签 – Flying

0
  1. 创建一个gestureRecogniser并将其添加到gameLayer视图。
  2. 实现目标的方法 func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool

  3. 您可以通过设置特殊的标记如确定第二列的标签。 (对于第一列i + 1000,第二列i + 2000等)

  4. if(gestureRecognizer.view.tag - 2000> 0)那么这是第二列,您将获得相应的第一列标签,然后创建一个新的第三列标签。

任何问题吗?评论。

相关问题