如何在循环目标c

时间:2017-04-26 11:28:17

标签: objective-c uilabel

我想在视图中使用用户点击制作3个标签动画,但只有最后一个标签动画,为什么列中的所有标签都没有动画,如何使所有3个标签在视图中开始使用用户手势进行动画制作。任何帮助...

`

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

    // Create UILabel for Column
    _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=[MainArrayFirst objectAtIndex:i];
    [_lblA sizeToFit];
    _lblA.hidden = YES;


    // add a Tap Gesture Recognizer to the label
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
    [_lblA addGestureRecognizer:tap];
     }
 - (void) gotTapped:(id)sender {
  for (UILabel *v in _columnA) {
    v.hidden = !v.hidden;

}

  [self fadeIn];
     }



  -(void)fadeIn{

CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadein.delegate = self;
fadein.fromValue = [NSNumber numberWithInt:0];
fadein.toValue = [NSNumber numberWithInt:1];
fadein.duration = 0.3;

[self.lblA.layer addAnimation:fadein forKey:@"fade"];

}

`

1 个答案:

答案 0 :(得分:0)

首先在.h文件中声明一个数组

NSMutableArray *arrLabel;

然后在数组中添加所有标签

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

        // Create UILabel for Column
        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];
        //lblA.userInteractionEnabled = YES;
        _lblA.text=[MainArrayFirst objectAtIndex:i];
        [_lblA sizeToFit];
        _lblA.hidden = YES;

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

然后最终改变动画功能

-(void)fadeIn{

    for(int i=0;i<arrLabel.count;i++)
    {
        UILabel *tempLabel = (UILabel *)[arrLabel objectAtIndex:i];
        CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadein.delegate = self;
        fadein.fromValue = [NSNumber numberWithInt:0];
        fadein.toValue = [NSNumber numberWithInt:1];
        fadein.duration = 0.3;

        [tempLabel.layer addAnimation:fadein forKey:@"fade"];
    }

}