UILabel停止动画

时间:2014-11-13 23:59:37

标签: objective-c animation uiimageview uitextfield

我有5 UIImageViews在屏幕上制作动画。如果按下一个,并且符合要求,它将为您的分数加1,使用:

self.score.text = @(self.score.text.integerValue + 1).stringValue;

但是当UILabel中的文字更新时,所有动画都会突然停止,剩下的UIImageViews会消失。但是,动画仅在几秒钟后重新启动,就像图像被隐藏一样。用于动画图像和更改图像的代码(每个图像都相同):

- (void) squareOneMover {
    NSUInteger r = arc4random_uniform(3);
    [self.squareOne setHidden:NO];
    CGPoint originalPosition = self.squareOne.center;
    CGPoint position = self.squareOne.center;
    originalPosition.y = -55;
    position.y += 790;
    [self.squareOne setCenter:originalPosition];

    [UIView animateWithDuration:r + 3
                          delay:0.0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         [self.squareOne setCenter:position];
                     }
                     completion:^(BOOL complete) {
                         if (complete) {
                             [self squareOneColour];
                             [self squareOneMover];
                         }
                     }
    ];
}

- (void) squareOneColour {
    NSUInteger r = arc4random_uniform(5);
    [self.squareOne setImage:[self.colorArray objectAtIndex:r]];
}

有人有解决方案吗?如果通过更改UILabel中的文本应该停止动画(我不知道为什么会这样做),有人可以提供一种解决方法来保持分数成为可能。

编辑:我创建了一个按钮,可以增加score的整数值。这意味着我可以手动更改UILabel中的文本。在文本发生变化的那一刻,动画仍停止。

编辑2:按下事件的方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    NSUInteger a = [self.colorArray indexOfObject:self.squareOne.image];
    NSUInteger b = [self.iconColorArray indexOfObject:self.icon.image];
    if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
        _squareOne.hidden = YES;
    }
    if (a!=b) {
        if (self.squareOne.hidden==YES) {
            NSLog(@"NO");
            }
        }
    if (a==b) {
        if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
            self.score.text = @(self.score.text.integerValue + 1).stringValue;
        }
    }

    if ([self.squareTwo.layer.presentationLayer hitTest:touchLocation]) {
        _squareTwo.hidden = YES;
    }
}

看看马特的答案,我如何通过“改变其约束”来动画UIImageView

约束:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-25-[btn1]-25-[btn2(==btn1)]-25-[btn3(==btn1)]-25-[btn4(==btn1)]-25-[btn5(==btn1)]-25-|" options:0 metrics:nil views:views]];

[self.view addConstraint:[NSLayoutConstraint 
      constraintWithItem:self.squareOne 
               attribute:NSLayoutAttributeTop 
               relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
               attribute:NSLayoutAttributeTop 
              multiplier:0.0 
                constant:-55.0]];

3 个答案:

答案 0 :(得分:1)

问题在于这一行:

 [self.squareOne setCenter:position];

您通过设置位置来设置squareOne的位置动画。但同时你也有约束也位于squareOne。在您更改标签文本之前,这个事实仍然隐藏起来;触发布局,现在约束都声明自己,结束了正在发生的所有其他事情。

一种解决方案是通过更改其约束来设置squareOne的位置动画。现在,当触发布局时,现有约束将匹配情况,因为它们是定位事物的唯一力。

答案 1 :(得分:0)

 //what all are you checking with the if's? 

 //I'm just curious there's a lot going on here

**//if this evaluates true
if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
    _squareOne.hidden = YES;
}**
if (a!=b) {
    if (self.squareOne.hidden==YES) {
        NSLog(@"NO");
        }
    }
if (a==b) {
    **          //this will evaluate true also
    if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
        self.score.text = @(self.score.text.integerValue + 1).stringValue;
    }**
}

//this works fine even when I added everything from interface builder which almost never happens

<强>更新

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic)UILabel *label;
@property int tapCount;
@end

@implementation ViewController


-(void)viewDidLoad{

     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(userTap:)];
    [self.view addGestureRecognizer:tap];


}
-(UILabel*)label{

    if (!_label) {
        _label = [[UILabel alloc]init];
        _label.text = @"taps:%3i",0;
        [_label sizeToFit];
        _label.center = self.view.center;
        [self.view addSubview:_label];
    }

    return _label;
}

-(void)userTap:(UITapGestureRecognizer*)sender {
    NSLog(@"tap");
 self.tapCount ++;
    [UIView animateWithDuration:1
                     animations:^{
                        self.label.text = [NSString stringWithFormat:@"taps:%i",self.tapCount];
                        self.label.center = [sender locationInView:self.view];

                    }
                     completion:nil];

}



@end

**只是为了将来的旁观者清理一下代码** 直接复制和粘贴只删除ViewController.m中的所有内容 在一个新项目上并将其粘贴到它的位置

答案 2 :(得分:0)

除了手动设置约束动画外,您还可以在动画期间UIImageView生成约束(不会被同级UILabel破坏)。就像在this question中一样。