停止的UILabel动画 [英] UILabel stops animations

查看:219
本文介绍了停止的UILabel动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个的UIImageViews 获取动画按住屏幕。如果一个人是pressed,并符合要求,它将增加1到你的分数,使用这个:

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

但是,当在的UILabel 更新文本,所有的动画突然停止,剩下的的UIImageViews 消失。但是,动画只出现图像正在成为隐藏在几秒钟后重新启动。在code动画图像和变化的图像(它们是相同的每一个人):

   - (无效){squareOneMover
    NSUInteger R = arc4random_uniform(3);
    [self.squareOne setHidden:NO];
    CGPoint originalPosition = self.squareOne.center;
    CGPoint位置= self.squareOne.center;
    originalPosition.y = -55;
    position.y + = 790;
    [self.squareOne setCenter:originalPosition];    [UIView的animateWithDuration:R + 3
                          延迟:0.0
                        选项​​:UIViewAnimationOptionAllowUserInteraction
                     动画:^ {
                         [self.squareOne setCenter:位置]。
                     }
                     完成:^(BOOL完成){
                         如果(完成){
                             [个体经营squareOneColour]
                             [个体经营squareOneMover]
                         }
                     }
    ];
} - (无效){squareOneColour
    NSUInteger R = arc4random_uniform(5);
    [self.squareOne setImage:[self.colorArray objectAtIndex:R]];
}

任何人都有一个解决方案吗?而如果通过改变文本的的UILabel 应该停止动画(我不知道他们为什么会这么做),有人可以提供一种变通方法,以保持成绩的可能。

编辑:我创建了一个按钮,会增加评分的整数值。这意味着我可以更改的UILabel 手动文本。该动画依然停止的那一刻文本改变了。

编辑2:为pressed活动方式:

   - (空)的touchesBegan:(*的NSSet)触及withEvent:方法(*的UIEvent)事件
{
    UITouch *触摸= [触摸anyObject]
    CGPoint touchLocation = [触摸locationInView:self.view];    NSUInteger A = [self.colorArray indexOfObject:self.squareOne.image];
    NSUInteger B = [self.iconColorArray indexOfObject:self.icon.image];
    如果([self.squareOne.layer presentationLayer则hitTest:touchLocation]){
        _squareOne.hidden = YES;
    }
    如果(一个!= b)的{
        如果(self.squareOne.hidden == YES){
            的NSLog(@否);
            }
        }
    如果(A == B){
        如果([self.squareOne.layer presentationLayer则hitTest:touchLocation]){
            self.score.text = @(self.score.text.integerValue + 1).stringValue;
        }
    }    如果([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-|\"选项​​:0的指标:无观点:观点]];[self.view addConstraint:[NSLayoutConstraint
      constraintWithItem:self.squareOne
               属性:NSLayoutAttributeTop
               relatedBy:NSLayoutRelationEqual
                  toItem:self.view
               属性:NSLayoutAttributeTop
              事半功倍:0.0
                常数:-55.​​0]];


解决方案

  //什么都你的,如果的检查? //我只是好奇,有很多会在这里** //如果这个判断为真
如果([self.squareOne.layer presentationLayer则hitTest:touchLocation]){
    _squareOne.hidden = YES;
} **
如果(一个!= b)的{
    如果(self.squareOne.hidden == YES){
        的NSLog(@否);
        }
    }
如果(A == B){
    ** //这将评估也如此
    如果([self.squareOne.layer presentationLayer则hitTest:touchLocation]){
        self.score.text = @(self.score.text.integerValue + 1).stringValue;
    } **
}//甚至当我从界面生成器几乎从未发生过的一切加入这个工作正常

更新

 #进口<的UIKit / UIKit.h>@interface的ViewController:UIViewController中
@property(强,非原子)的UILabel *标签;
@property INT tapCount;
@结束@implementation的ViewController
- (无效)viewDidLoad中{     UITapGestureRecognizer *自来水= [[UITapGestureRecognizer的alloc]初始化];
    [自来水addTarget:自我行动:@选择(userTap :)]。
    [self.view addGestureRecognizer:TAP];
}
- (*的UILabel)标签{    如果(!_label){
        _label = [[的UILabel的alloc]初始化];
        _label.text = @龙头:%3I,0;
        [_label sizeToFit]
        _label.center = self.view.center;
        [self.view addSubview:_label];
    }    返回_label;
} - (无效)userTap:(UITapGestureRecognizer *){发件人
    的NSLog(@点击);
 self.tapCount ++;
    [UIView的animateWithDuration:1
                     动画:^ {
                        self.label.text = [的NSString stringWithFormat:@龙头:%I,self.tapCount]
                        self.label.center = [发送locationInView:self.view];                    }
                     完成:无];}@结束

**刚刚清理code位为今后的围观**
直接复制和粘贴里面只删除ViewController.m一切
在一个新的项目,这在它的地方粘贴

I have 5 UIImageViews getting animated down the screen. If one is pressed, and it meets the requirements, it will add 1 to your score, using this:

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

But when the text in the UILabel updates, all the animations stop abruptly and the remaining UIImageViews disappear. But, the animation only restarts after a few seconds, as if the images are becoming hidden. The code to animate the images and change image(They are the same for each one):

- (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]];
}

Anyone have a solution? And if by changing the text in a UILabel is supposed to stop animations (I don't know why they would do so) can someone provide a workaround to make keeping score possible.

Edit: I created a button that would increase the integer value of score. This means I can change the text in the UILabel manually. The animations still stopped the moment the text changed.

Edit 2: Method for the pressed event:

- (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;
    }
}

Looking at Matt's answer, how would I animate the UIImageView by "changing its constraints"?

Constraints:

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

解决方案

 //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

Update

#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

**just cleaning up the code a bit for future onlookers ** direct copy and paste only delete everything inside ViewController.m on a new project and paste this in it's place

这篇关于停止的UILabel动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆