【问题标题】:UILabel stops animationsUILabel 停止动画
【发布时间】:2014-11-13 23:59:37
【问题描述】:

我有 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]];

【问题讨论】:

  • 我认为问题在于按下事件。你能告诉我们这样做的方法吗?

标签: objective-c animation uiimageview uitextfield


【解决方案1】:

问题出在这一行:

 [self.squareOne setCenter:position];

您正在通过设置squareOne 的位置来设置其位置 的动画。但同时您也有 约束 也定位 squareOne。在您更改标签文本之前,该事实一直是隐藏的;触发布局,现在所有约束都断言自己,结束正在发生的所有其他事情。

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

【讨论】:

    【解决方案2】:
     //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 中的所有内容 在一个新项目上并将其粘贴到它的位置

    【讨论】:

    • 不要担心 if 语句。我喜欢马特的回答,所以我想知道如何通过改变约束来制作动画。虽然不知道你的答案是什么
    • 好吧,您说您不想隐藏标签,并且您说分数在标签上,但是标签获得更新的唯一方法是隐藏方块(包含标签? )
    • 如果你真的不想让自己感到沮丧,你应该从你知道的工作开始,然后继续前进。你知道你的触摸事件被接收了;如果是我,我会注释掉 if 语句并开始更新没有它们的文本,然后继续前进。上面的代码是我刚刚编写的一个小程序的直接复制和粘贴,它完美地工作,没有任何东西消失或约束到以前约束的对象。
    【解决方案3】:

    除了手动为约束设置动画外,您还可以让UIImageView 在动画期间生成约束(不会被同级 UILabel 破坏)。比如this question

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多