UILabel stops animations

前端 未结 3 684
猫巷女王i
猫巷女王i 2020-12-22 00:39

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:



        
相关标签:
3条回答
  • 2020-12-22 01:12

    Instead of animating the constraints by hand, you can also have UIImageView generate constraints during animation (which does not get broken by a sibling UILabel). Like in this question.

    0 讨论(0)
  • 2020-12-22 01:31

    The problem is this line:

     [self.squareOne setCenter:position];
    

    You are animating the position of squareOne by setting its position. But meanwhile you also have constraints that also position squareOne. That fact remains concealed until you change the text of the label; that triggers layout, and now the constraints all assert themselves, putting an end to everything else that was going on.

    One solution is to animate the position of squareOne by changing its constraints. Now when layout is triggered, the existing constraints will match the situation because they are the only force that is positioning things.

    0 讨论(0)
  • 2020-12-22 01:36
     //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

    0 讨论(0)
提交回复
热议问题