【发布时间】:2011-04-06 20:23:00
【问题描述】:
我有一个 UIView,我正在改变它的中心,但 UIView 没有移动。
pLocation 是在 NIB 文件中创建的 UIView*。我正在尝试移动它来改变它的中心(或框架或任何东西)。这是我当前的测试用例。
- (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 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.000000
] After Loc X: 10.300001 Y: 100.000000 - Frame -50.199997 45.000000
但视图永远不会在屏幕上移动。如何强制重新绘制视图?
编辑:
我有一些动画会在手前播放并与其他按钮混淆。当我像这样将位置移动到该动画时:
-(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];
}
位置移动得很好
【问题讨论】:
-
尝试将其移动超过 0.1,尝试改为 2.0。 AFAIK 值是像素,没有 0.1 像素...
-
0.1的增量有点小,你试过更大的值吗,例如5 ?此外,设置中心已经重绘了视图(没有调用
drawRect:),因此以下三行是不必要的(并且 setNeedsLayout 仅对其子视图有影响)。 -
如果你只是隐藏它(pLocation.hidden = YES),它会隐藏吗?视图是否正确连接到插座?
-
@Zaky German:实际上这些值不是像素,但在完全规范的视图树中,它确实匹配一个像素(在非视网膜设备上;在视网膜设备上,一个点实际上跨越四个像素) .在视图或其父视图之一上设置非标识
transform会破坏这种点对像素关系。
标签: iphone objective-c uiview