Why is my UIView animating the wrong position?

I have UILabel, hardWayLabel , inside a UIView, hardWayView . I need this view (and therefore the shortcut) to animate a new position on the left side of the screen. However, while the mark is in the correct position, the view can be seen while continuing to move far from the left side of the screen. I use the following code for animation (which I use many times in my application 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 used NSLog to make sure that painPoint.x and painPoint.y are what they should be 9, which is confirmed by the fact that UILabel is in the correct position. I guess the real question is why will the UIView continue to move past the edges of the screen (and only in this case, when the UIView is animating to the left from the center of the screen)?

I have Autolayout turned on, although I don’t think it’s a criminal. This problem occurs when I run the application on the iPhone Simulator with iOS 7, and I can’t get the simulator to work with iOS 6, so I can’t isolate the problem before iOS 7 (although maybe?) I started development in the old version of Xcode and the application do not use storyboards).

Here is the screen recording of the problem. As you can see, moving two images from left to right works fine. Although when moving the views to the left, one of the views (black background) seems to exit the screen in the upper left corner, while the label (in orange text) goes to the right place). Explanatory video

Update: here is some more code. It is almost identical to the 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 that the problem does occur 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 tried to make frame.origin.x and .y real numbers (compared to variables) and the same pattern: UILabel is animated to the right place, and UIView is animated in the upper left corner of the screen

+6
source share
3 answers

Whooaa! After debugging muuuch, I finally found a problem !! :) Let me tell you ... you have interesting code there. In any case, you will be surprised to learn how stupid the question is; you are ready?

Here is the problem:

Around line 540 you have an extra β€œ@” sign, so when you try to get a value, it cannot 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!

Greetings :)

+10
source

In appearance, you set hardWayView.frame = frame; before the animation starts, so that the UIView changes its location and then the animation. Switching places between

 hardWayView.frame = frame; [UIView beginAnimations:nil context:nil]; 

must fix it.

Regardless, I would strongly advise moving on to animation blocks. The above code should look like this:

 [UIView animateWithDuration:0.4 animations:^{ CGRect frame = hardWayView.frame; frame.origin.x = painPoint.x; frame.origin.y = painPoint.y; hardWayView.frame = frame; } completion:^(BOOL finished) { // }]; 
+6
source

Wouldn't it be better to have code inside a completion block? (Or before the animateWithDuration block).

 [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]; NSDictionary *hardWay = @{@"view": hardWayView, @"label": hardWayLabel, @"intValue":[NSString stringWithFormat: @"%@", hardWayLabel.text], @"hiddenBool":@0, @"position":@([hardWayPos intValue]), @"originalRand": [NSString stringWithFormat: @"%@", hardWayLabel.text]}; [masterDict replaceObjectAtIndex:[masterDict indexOfObject:miniMaser] withObject:hardWay]; }]; 

Also, if it were me, I would use a separate name for each rectangle to help debug. for example, do not always call your rectangular "frame". Name it centerFrame, leftFrame, rightFrame.

When you exit the hardWayView frame at the end of the animation, it reports what you expect?

When you exit the hardWayView frame at the beginning of the animation, it tells you what you expect?

My guess is a hardWayView frame, this is not what you think.

Does it work as you expect if you set the frame directly without animating it?

+1
source

Source: https://habr.com/ru/post/956339/


All Articles