如何在目标c中触摸相对于其他标签的五个标签副本

时间:2017-04-21 12:00:00

标签: ios objective-c uilabel touchesbegan

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

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;
    _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];

触摸开始方法我调用方法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列标签,因为在此图片中只有两列是enter image description here

我想让第3列与column1 Text1标签结合。

2 个答案:

答案 0 :(得分:0)

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

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

  4. if(gestureRecognizer.view.tag - 2000&gt; 0)那么这是第二列,您将获得相应的第一列标签,然后您也会创建一个新的第三列标签。
  5. 有任何问题吗?评价。

答案 1 :(得分: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;
        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