Last active
August 29, 2015 14:06
-
-
Save jawj/9037468b91ec98e47ba8 to your computer and use it in GitHub Desktop.
A UISlider subclass for iOS7 that respects alignmentRectInsets on custom thumb images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface AlignmentRectRespectingSlider : UISlider | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @implementation AlignmentRectRespectingSlider | |
| - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value { | |
| UIImage* image = self.currentThumbImage; | |
| UIEdgeInsets insets = image.alignmentRectInsets; | |
| CGFloat fullWidth = image.size.width; | |
| CGFloat fullHeight = image.size.height; | |
| CGFloat coreWidth = fullWidth - insets.left - insets.right; | |
| CGFloat coreHeight = fullHeight - insets.top - insets.bottom; | |
| CGFloat usableTrackWidth = rect.size.width - coreWidth; | |
| CGFloat offset = usableTrackWidth * value; | |
| CGRect thumbRect = CGRectMake(rect.origin.x - insets.left + offset, | |
| rect.origin.y + 0.5f * rect.size.height - 0.5f * coreHeight - insets.top, | |
| fullWidth, fullHeight); | |
| return thumbRect; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment