开发者

UIView not moving when changing its center

I have a UIView, I am changing its center but the UIView isn't moving.

pLocation is a UIView* created in a NIB file. I am trying to move it changing its center (or frame or anything really). Here is my current test case.

- (void)Update {

    [mWorldObj Update];

    static float xVal = 10;
    xVal += 0.1;

    NSLog(@"Before Loc X: %f Y: %f", pLocation.center.x, pLocation.center.y);

    pLocation.center = CGPointMake(xVal, 100.0);
    [pLocation setNeedsDisplay];   //Test
    [pLocation setNeedsLayout];    //Test
    pLocation.hidden = !pLocation.hidden;  //Test - Hide and unhide to force re-draw

    NSLog(@"After Loc X: %f Y: %f - Frame %f %f", pLocation.center.x, pLocation.center.y, pLocation.frame.origin.x, pLocation.frame.origin.y);
}

Before and after print out the correct numbers

] Before Loc X: 60.500000 Y: 55.000000
] After Loc X: 10.100000 Y: 100.000000 - Frame -50.400002 45.000000
] Before Loc X: 10.100000 Y: 100.000000
] After Loc X: 10.200001 Y: 100.000000 - Frame -50.299999 45.000000
] Before Loc X: 10.200001 Y: 100.0000开发者_JS百科00
] After Loc X: 10.300001 Y: 100.000000 - Frame -50.199997 45.000000

but the view never moves onscreen. How do I force a re-draw of the view?

Edit:

I have animations that play before hand and mess with other buttons. When I add moving the location to that animation like so:

-(void)  EnableButtons {
    pInfo.hidden = false;
    pMore.hidden = false;
    //SetLocButtonsClickable(true);
    [UIView beginAnimations:@"ShowButtons" context:nil];    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];   
    [UIView setAnimationDuration:0.5];

    // Make the animatable changes.
    pInfo.alpha = 1.0;
    pMore.alpha = 1.0;
    pLocation.center = CGPointMake(200, 000);   //****Added test code

    [UIView commitAnimations];
}

The Location moves just fine


Hey, dunno how your view hierarchy is set up, but I've recently had a very similar problem and it was caused because the containing UIView had resized to a size smaller than the view I was moving, so in practice It couldn't move because it was held in place by it's containing view.

So, if your pLocation is contained in another UIView, it might be worth checking its frame too to verify it allows for pLocation's movement.


Animations can only be performed off the main thread. Ensure animation code is being called from the main thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜