Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my UIView animate to the wrong position?

I have a UILabel, hardWayLabel, inside of a UIView, hardWayView. I need this view (and consequently the label) to animate to a new position on the far left side of the screen. However, while the label goes to the correct position, the view can be seen continuing to move far off of the left side of the screen. I use the following code for the animation (which I use many other times in my app successfully):

        CGRect frame = hardWayView.frame;
        frame.origin.x = painPoint.x;
        frame.origin.y = painPoint.y;
        hardWayView.frame = frame;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.25];

I have used NSLog to make sure that painPoint.x and painPoint.y are what they should be 9which is confirmed by the fact that the UILabel ends up in the correct position. I guess the real question is why would the UIView continue to move past the screen boundaries (and only do so in this instance when the UIView is being animated from the screen's center to the left)?

I do have Autolayout enabled, though I do not think that this is the culprit. This problem occurs when I run the app on the iPhone Simulator with iOS 7, and I am unable to get the simulator to run with iOS 6 so am not able to isolate the issue to iOS 7 (though could it be? I began development in an old version of Xcode and the app does not use storyboards).

Here is a screen recording of the issue. As you can see, moving the two views from the left to the right works normally. Though when moving the views back to the left, one of the views (the black background) appears to go off screen through the top left corner while the label (in orange text) goes to the correct location). Explanatory video

Update: Here's some more code. It is near identical to other parts of my application that work flawlessly, which is a bit of a brain blaster.

NSString *stringOfRands = tDict[@"originalRand"];
if ([stringOfRands rangeOfString:@"+"].location != NSNotFound)
{
     NSArray *arrayOfRands = [stringOfRands componentsSeparatedByString:@" + "];

     for (int z=1; z<[arrayOfRands count]; z++)
     {
          NSString *updateValue = arrayOfRands[z];
          NSInteger newHiddenPosition = [self findBlank:leftSpaces];

          NSDictionary *updateStuff = @{@"updateValue": updateValue, @"workingMasterDict":tDict, @"newPos":@(newHiddenPosition), @"newCGPto":@"left", @"@parent":colorV};

          [self revealHidden:updateStuff];

      } 
}

I believe the problem is truly happening during the revealHidden method, where the animation is called.

[leftSpaces replaceObjectAtIndex:[hardWayPos intValue] withObject:[NSString stringWithFormat:@"full"]];
CGPoint painPoint = [[leftPoints objectAtIndex:[hardWayPos intValue]] CGPointValue];    
[UIView animateWithDuration:0.25 animations:^{
        CGRect frame = hardWayView.frame;
        frame.origin.x = painPoint.x;
        frame.origin.y = painPoint.y;
        hardWayView.frame = frame;
    } completion:^(BOOL finished) {
        //
    }];

hardWayView.hidden=0;
hardWayView.backgroundColor = [UIColor blueColor];
hardWayLabel.textColor = [UIColor orangeColor];

I have tried making frame.origin.x and .y actual numbers (versus variables) and the same pattern ensues: the UILabel animates to the correct location and the UIView animates off from the top left corner of the screen

like image 829
zch Avatar asked Oct 20 '13 04:10

zch


1 Answers

Whooaa! After muuuch debugging I finally found the issue!! :) Let me tell you... you have some interesting code there. Anyway, you'll be surprised to know how silly the issue was; you ready?

Here is the problem:

Around line 540 you have an extra "@" sign, that's why when you try to retrieve the value it can't find it:

//ERROR HERE
NSArray *arrayOfRands = [stringOfRands componentsSeparatedByString:@" + "];

for (int z=1; z<[arrayOfRands count]; z++)
{
    NSString *updateValue = arrayOfRands[z];
    NSInteger newHiddenPosition = [self findBlank:leftSpaces];

    // This is the line with the error!!
    // You have @"@parent" but it should be @"parent"      
    NSDictionary *updateStuff = @{@"updateValue": updateValue, @"workingMasterDict":tDict, @"newPos":@(newHiddenPosition), @"newCGPto":@"left", @"@parent":colorV};

    [self revealHidden:updateStuff];

}

It was very interesting to debug your code!

Cheers :)

like image 73
LuisCien Avatar answered Sep 19 '22 12:09

LuisCien