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

A few changes #3

Open
wants to merge 5 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
28 changes: 16 additions & 12 deletions SCPageScrubberBar/SCCalloutView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

#import "SCCalloutView.h"
#import "NSString+BZExtensions.h" // Hsoi 2013-08-19 - not ideal to use our own, but here we are.

#define kSCCalloutViewHeight 43.0f
#define kSCCalloutViewMinWidth 20.0f
#define kSCCalloutViewMinWidthWidthAnthor 41.0f
Expand All @@ -33,6 +35,16 @@ - (CGFloat)_minWidth;

@implementation SCCalloutView


@synthesize anchorDirection = _anchorDirection;
@synthesize titleLabel = _titleLabel;
@synthesize subtitleLabel = _subtitleLabel;
@synthesize titleLabelInsets = _titleLabelInsets;
@synthesize subtitleLabelInsets = _subtitleLabelInsets;
@synthesize maxWidth = _maxWidth;
@synthesize minWidth = _minWidth;


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
Expand Down Expand Up @@ -63,8 +75,8 @@ - (id)initWithFrame:(CGRect)frame

- (CGSize)sizeThatFits:(CGSize)size
{
CGSize titleLabelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
CGSize subTitleLabelSize = [self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font];
CGSize titleLabelSize = [self.titleLabel.text barz_sizeWithFont:self.titleLabel.font];
CGSize subTitleLabelSize = [self.subtitleLabel.text barz_sizeWithFont:self.subtitleLabel.font];
// Get the max width of the two labels
CGFloat maxLabelsWidth = MAX(titleLabelSize.width, subTitleLabelSize.width);

Expand Down Expand Up @@ -174,11 +186,7 @@ - (UILabel *)titleLabel
_titleLabel.shadowColor = [UIColor blackColor];
_titleLabel.shadowOffset = CGSizeMake(0, -1);
_titleLabel.contentMode = UIViewContentModeCenter;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {
_titleLabel.textAlignment = NSTextAlignmentCenter;
} else {
_titleLabel.textAlignment = UITextAlignmentCenter;
}
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
Expand All @@ -193,11 +201,7 @@ - (UILabel *)subtitleLabel
_subtitleLabel.shadowColor = [UIColor blackColor];
_subtitleLabel.shadowOffset = CGSizeMake(0, -1);
_subtitleLabel.contentMode = UIViewContentModeCenter;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {
_subtitleLabel.textAlignment = NSTextAlignmentCenter;
} else {
_subtitleLabel.textAlignment = UITextAlignmentCenter;
}
_subtitleLabel.textAlignment = NSTextAlignmentCenter;
}
return _subtitleLabel;
}
Expand Down
3 changes: 3 additions & 0 deletions SCPageScrubberBar/SCPageScrubberBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
@property (nonatomic, weak) id <SCPageScrubberBarDelegate> delegate;
@property (nonatomic, assign) BOOL alwaysShowTitleView;
@property (nonatomic, assign) BOOL isPopoverMode;

- (void)setValue:(float)value showCallout:(BOOL)show;

@end

@protocol SCPageScrubberBarDelegate <NSObject>
Expand Down
88 changes: 48 additions & 40 deletions SCPageScrubberBar/SCPageScrubberBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,33 @@ - (CGRect)_thumbRect;
- (void)_fadeCalloutViewInAndOut:(BOOL)aFadeIn;
- (void)_updateCalloutViewPosition;
- (void)_updateCalloutViewText;
- (UIImage*)_generateDotsImage;
@end

@implementation SCPageScrubberBar

@synthesize dotsImage = _dotsImage;
@synthesize clearImage = _clearImage;
@synthesize backgroundLayer = _backgroundLayer;
@synthesize calloutView = _calloutView;
@synthesize delegate = _delegate;
@synthesize alwaysShowTitleView = _alwaysShowTitleView;
@synthesize isPopoverMode = _isPopoverMode;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self.layer addSublayer:self.backgroundLayer];
UIImage* thumbImage = [UIImage imageNamed:@"SCPageScrubberBar.bundle/thumb.png"];

// Hsoi 2013-09-19 - as a quick way of having a more iOS7 look and feel, only use the
// bundled thumb image under pre-7 OS's. Otherwise, just use the 7 slider thumb.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
UIImage* thumbImage = [UIImage imageNamed:@"SCPageScrubberBar.bundle/thumb.png"];
[self setThumbImage:thumbImage forState:UIControlStateNormal];
}
[self setMaximumTrackImage:self.clearImage forState:UIControlStateNormal];
[self setMinimumTrackImage:self.clearImage forState:UIControlStateNormal];
[self setThumbImage:thumbImage forState:UIControlStateNormal];

[self addSubview:self.calloutView];
}
Expand All @@ -49,10 +61,30 @@ - (void)layoutSubviews
nFrame.size.width = self.bounds.size.width;
self.backgroundLayer.frame = nFrame;

_dotsImage = nil;
self.backgroundLayer.contents = (id)self.dotsImage.CGImage;

[self _updateCalloutViewText];
[self _updateCalloutViewPosition];
}

- (void)setValue:(float)value showCallout:(BOOL)show {
self.value = value;

if (show) {
self.calloutView.alpha = 0.0;
[UIView animateWithDuration:0.5
animations:^{
self.calloutView.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5
animations:^{
self.calloutView.alpha = 0.0;
} completion:NULL];
}];
}
}

#pragma mark - Self Private Methods

- (CGRect)_thumbRect
Expand All @@ -66,14 +98,15 @@ - (CGRect)_thumbRect

- (void)_fadeCalloutViewInAndOut:(BOOL)aFadeIn
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (aFadeIn) {
self.calloutView.alpha = 1.0;
} else {
self.calloutView.alpha = 0.0;
}
[UIView commitAnimations];
[UIView animateWithDuration:0.5
animations:^{
if (aFadeIn) {
self.calloutView.alpha = 1.0;
}
else {
self.calloutView.alpha = 0.0;
}
} completion:NULL];
}

- (void)_updateCalloutViewPosition
Expand Down Expand Up @@ -107,31 +140,6 @@ - (void)_updateCalloutViewText
[self.calloutView sizeToFit];
}

- (UIImage*)_generateDotsImage
{
// Get the image of one dot
UIImage *image = [UIImage imageNamed:@"SCPageScrubberBar.bundle/dot.png"];
CGFloat scale = [UIScreen mainScreen].scale;
CGSize size = CGSizeMake(self.bounds.size.width, image.size.height);
// The total width of one dot including spacing
CGFloat oneDotWidth = image.size.width + kSCDotImageSpacing;

UIGraphicsBeginImageContextWithOptions(size, NO, scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();

// Draw dotImage repeatly
NSInteger len = (int)(size.width / oneDotWidth);
for (NSInteger i = 0; i < len; i++) {
CGPoint drawPoint = CGPointMake(i * oneDotWidth + kSCDotImageSpacing / 2, 0);
CGContextDrawImage(ctx, (CGRect){drawPoint, image.size}, image.CGImage);
}

// Generate the image
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}


#pragma mark - UIControl touch event tracking

Expand Down Expand Up @@ -209,8 +217,8 @@ - (CALayer *)backgroundLayer
- (SCCalloutView *)calloutView
{
if (_calloutView == nil) {
_calloutView = [[SCCalloutView alloc] initWithFrame:CGRectMake(0, 0, 100, 0)];
_calloutView.alpha = 0;
_calloutView = [[SCCalloutView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 0.0)];
_calloutView.alpha = 0.0;
_calloutView.anchorDirection = self.isPopoverMode ? SCCalloutViewAnchorBottom : SCCalloutViewAnchorNone;
}
return _calloutView;
Expand All @@ -221,9 +229,9 @@ - (void)setAlwaysShowTitleView:(BOOL)alwaysShowTitleView
if (_alwaysShowTitleView != alwaysShowTitleView) {
_alwaysShowTitleView = alwaysShowTitleView;
if (_alwaysShowTitleView) {
self.calloutView.alpha = 1;
self.calloutView.alpha = 1.0;
} else {
self.calloutView.alpha = 0;
self.calloutView.alpha = 0.0;
}
}
}
Expand Down