2017-04-21 34 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)那麼這是第二列,您將獲得相應的第一列標籤,然後創建一個新的第三列標籤。

任何問題嗎?評論。

相關問題