UILabel Stops Animation

I have 5 UIImageViews animated around the screen. If you click and it meets the requirements, it will add 1 to your account using this:

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

But when the text in UILabel updated, all animations stop abruptly, and the remaining UIImageViews disappear. But the animation resumes after a few seconds, as if the images were becoming hidden. Code for animating images and changing images (they are the same for each):

 - (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 when changing text in UILabel it is supposed to stop the animation (I don’t know why they will do it), someone can provide a workaround to make the evaluation possible.

Edit: I created a button that will increment the integer score . This means that I can change the text in UILabel manually. The animation still stopped when the text changed.

Edit 2: Method for clicked 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, how could I revitalize a UIImageView by “changing its constraints”?

Limitations:

 [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]]; 
+1
objective-c uitextfield animation uiimageview
Nov 13 '14 at 23:59
source share
3 answers
  //what all are you checking with the if's? //I'm just curious there 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 clearing the code for future viewers ** direct copy and paste deletes all the contents inside ViewController.m to a new project and paste it into it

0
Nov 21 '14 at 8:53
source share

The problem is this line:

  [self.squareOne setCenter:position]; 

Animate the position of squareOne by setting its position. But meanwhile, you also have limitations that also squareOne position. This fact remains hidden until you change the label text; which launches the layout, and now all the restrictions affirm themselves, putting an end to everything else that happened.

One solution is to revitalize squareOne by changing its limitations. Now that the layout starts, the existing constraints will fit the situation, because they are the only force that positions things.

+1
Nov 17 '14 at 1:55
source share

Instead of animating constraints manually, you can also have UIImageView generate constraints during the animation (which is not interrupted by UILabel sibling). Like in this question .

0
Jun 09 '15 at 2:06 on
source share



All Articles