Skip to content

Instantly share code, notes, and snippets.

@nielsbot
Last active December 15, 2015 17:09
Show Gist options
  • Save nielsbot/5294660 to your computer and use it in GitHub Desktop.
Save nielsbot/5294660 to your computer and use it in GitHub Desktop.
Make sure your layer is pixel-aligned when setting it's position. (Avoids things looking blurry)
// when setting the position of a layer, use this:
// layer.position = [ layer pixelAlignedPostionForPoint:<originalPoint> ] ;
@implementation CALayer (SetPositionPixelAligned)
-(CGPoint)pixelAlignedPositionForPoint:(CGPoint)p
{
CGSize size = self.bounds.size ;
CGPoint anchorPoint = self.anchorPoint ;
CGPoint result = (CGPoint){
roundf( p.x ) + anchorPoint.x * fmodf( size.width, 2.0f )
, roundf( p.y ) + anchorPoint.y * fmodf( size.height,2.0f )
} ;
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment