如何在 objective c 中通过触摸定位标签的五个副本相对于其他标签

How to position five copies of a label relative to other label with touch in objective c

我有两列带有一些文本的标签。当我触摸第二列的任何标签时,如何获得附加到第 1 列的第 3 列标签。 这是 column1 和 Column2 标签的代码

第 1 列标签

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;
    _lbl.text=@"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;
    _lbl2.text=@"Text2";
    [_lbl2 sizeToFit];

在touches 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];

我没有在所需位置获得第 3 列标签,因为在这张图片中只有两列是

我想将第 3 列与第 1 列 Text1 标签连接起来。

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

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

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

有什么问题吗?评论。

@Moon - 我仍然不完全确定您要做什么,但希望这对您有所帮助。

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

如果这不能满足您的要求(或者如果您不知道如何使它满足您的要求),您将需要 更多明确描述您的目标。


//  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;
        lblA.text=@"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;
        lblB.text=@"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;
        lblC.text=@"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