为什么设置视图的框架会导致浮动不精确?

时间:2014-08-16 01:50:09

标签: objective-c core-graphics cgrect

在为视图框架分配CGRect时,我遇到了一个非常奇怪的浮动不精确。我可以缩小再现步骤:

指定不同大小的框架

CGRect frame = self.label.frame;
frame.origin.x = 13.f;
NSLog(@"boundingRect = %@", NSStringFromCGRect(boundingRect));
frame.size = boundingRect.size;
NSLog(@"Before assignment, x=%f", frame.origin.x);
self.label.frame = frame;
NSLog(@"After assignment, x=%f", self.label.frame.origin.x);

输出:

boundingRect = {{0, 0}, {116.072, 13.123}}
Before assignment, x=13.000000
After assignment, x=12.999996 <= ???

分配帧而不更改源大小

CGRect frame = self.label.frame;
frame.origin.x = 13.f;
//NSLog(@"boundingRect = %@", NSStringFromCGRect(boundingRect));
//frame.size = boundingRect.size;
NSLog(@"Before assignment, x=%f", frame.origin.x);
self.label.frame = frame;
NSLog(@"After assignment, x=%f", self.label.frame.origin.x);

输出:

Before assignment 13.000000
After assignment 13.000000

知道这里发生了什么吗?

0 个答案:

没有答案