Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding bias in UIView. #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UIKit/Classes/UIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ typedef NSUInteger UIViewAnimationOptions;
BOOL _autoresizesSubviews;
BOOL _userInteractionEnabled;
CALayer *_layer;
CGRect _frame;
NSInteger _tag;
UIViewContentMode _contentMode;
UIColor *_backgroundColor;
Expand Down
47 changes: 26 additions & 21 deletions UIKit/Classes/UIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ - (id)initWithFrame:(CGRect)theFrame
_subviews = [[NSMutableSet alloc] init];
_gestureRecognizers = [[NSMutableSet alloc] init];

_frame = CGRectZero;

_layer = [[[isa layerClass] alloc] init];
_layer.delegate = self;
_layer.layoutManager = [UIViewLayoutManager layoutManager];
Expand Down Expand Up @@ -618,41 +620,41 @@ - (void)_superviewSizeDidChangeFrom:(CGSize)oldSize to:(CGSize)newSize
*/

if (hasAutoresizingFor(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin)) {
frame.origin.y = floorf(frame.origin.y + (frame.origin.y / oldSize.height * delta.height));
frame.size.height = floorf(frame.size.height + (frame.size.height / oldSize.height * delta.height));
frame.origin.y += frame.origin.y / oldSize.height * delta.height;
frame.size.height += frame.size.height / oldSize.height * delta.height;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight)) {
const CGFloat t = frame.origin.y + frame.size.height;
frame.origin.y = floorf(frame.origin.y + (frame.origin.y / t * delta.height));
frame.size.height = floorf(frame.size.height + (frame.size.height / t * delta.height));
frame.origin.y += frame.origin.y / t * delta.height;
frame.size.height += frame.size.height / t * delta.height;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight)) {
frame.size.height = floorf(frame.size.height + (frame.size.height / (oldSize.height - frame.origin.y) * delta.height));
frame.size.height += frame.size.height / (oldSize.height - frame.origin.y) * delta.height;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)) {
frame.origin.y = floorf(frame.origin.y + (delta.height / 2.f));
frame.origin.y += delta.height / 2.f;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleHeight)) {
frame.size.height = floorf(frame.size.height + delta.height);
frame.size.height += delta.height;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleTopMargin)) {
frame.origin.y = floorf(frame.origin.y + delta.height);
frame.origin.y += delta.height;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleBottomMargin)) {
frame.origin.y = floorf(frame.origin.y);
// nothing required
}

if (hasAutoresizingFor(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin)) {
frame.origin.x = floorf(frame.origin.x + (frame.origin.x / oldSize.width * delta.width));
frame.size.width = floorf(frame.size.width + (frame.size.width / oldSize.width * delta.width));
frame.origin.x += frame.origin.x / oldSize.width * delta.width;
frame.size.width += frame.size.width / oldSize.width * delta.width;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth)) {
const CGFloat t = frame.origin.x + frame.size.width;
frame.origin.x = floorf(frame.origin.x + (frame.origin.x / t * delta.width));
frame.size.width = floorf(frame.size.width + (frame.size.width / t * delta.width));
frame.origin.x += frame.origin.x / t * delta.width;
frame.size.width += frame.size.width / t * delta.width;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth)) {
frame.size.width = floorf(frame.size.width + (frame.size.width / (oldSize.width - frame.origin.x) * delta.width));
frame.size.width += frame.size.width / (oldSize.width - frame.origin.x) * delta.width;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin)) {
frame.origin.x = floorf(frame.origin.x + (delta.width / 2.f));
frame.origin.x += delta.width / 2.f;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleWidth)) {
frame.size.width = floorf(frame.size.width + delta.width);
frame.size.width += delta.width;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleLeftMargin)) {
frame.origin.x = floorf(frame.origin.x + delta.width);
frame.origin.x += delta.width;
} else if (hasAutoresizingFor(UIViewAutoresizingFlexibleRightMargin)) {
frame.origin.x = floorf(frame.origin.x);
// nothing required
}

self.frame = frame;
Expand Down Expand Up @@ -684,14 +686,17 @@ + (NSSet *)keyPathsForValuesAffectingFrame

- (CGRect)frame
{
return _layer.frame;
return _frame;
}

- (void)setFrame:(CGRect)newFrame
{
if (!CGRectEqualToRect(newFrame,_layer.frame)) {
_frame = newFrame;

if (!CGRectEqualToRect(CGRectIntegral(newFrame),CGRectIntegral(_layer.frame))) {
CGRect oldBounds = _layer.bounds;
_layer.frame = newFrame;
CGRect roundedFrame = CGRectIntegral(_frame);
_layer.frame = roundedFrame;
[self _boundsDidChangeFrom:oldBounds to:_layer.bounds];
[[NSNotificationCenter defaultCenter] postNotificationName:UIViewFrameDidChangeNotification object:self];
}
Expand Down