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

Bug fixes #67

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
46 changes: 46 additions & 0 deletions NMRangeSlider/NMRangeSlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,54 @@

#import <UIKit/UIKit.h>

@protocol NMRangeSliderDelegate <NSObject>

@optional
/**
* When lower handle image view is selected.
*
* @param view The slider view.
* @param imageView The lower image view.
*/
- (void)rangeSliderView:(UIView *)view didSelectLowerHandleImageView:(UIImageView *)imageView;

/**
* When upper handle image view is selected.
*
* @param view The slider view.
* @param imageView The upper image view.
*/
- (void)rangeSliderView:(UIView *)view didSelectUpperHandleImageView:(UIImageView *)imageView;

/**
* When slider stopped moving.
*
* @param view The slider view.
*/
- (void)rangeSliderViewDidEndSelect:(UIView *)view;

/**
* When lower handle image view did move.
*
* @param view The slider view.
* @param imageView The lower image view.
*/
- (void)rangeSliderView:(UIView *)view didMoveLowerHandleImageView:(UIImageView *)imageView;

/**
* When upper handle image view did move.
*
* @param view The slider view.
* @param imageView The upper image view.
*/
- (void)rangeSliderView:(UIView *)view didMoveUpperHandleImageView:(UIImageView *)imageView;

@end

@interface NMRangeSlider : UIControl

@property (weak, nonatomic) id <NMRangeSliderDelegate> delegate;

// default 0.0
@property(assign, nonatomic) float minimumValue;

Expand Down
45 changes: 39 additions & 6 deletions NMRangeSlider/NMRangeSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,15 @@ - (CGRect)trackRect

float xLowerValue = ((self.bounds.size.width - lowerHandleWidth) * (_lowerValue - _minimumValue) / (_maximumValue - _minimumValue))+(lowerHandleWidth/2.0f);
float xUpperValue = ((self.bounds.size.width - upperHandleWidth) * (_upperValue - _minimumValue) / (_maximumValue - _minimumValue))+(upperHandleWidth/2.0f);


if (isnan(xLowerValue)) {
xLowerValue = 0.0f;
}

if (isnan(xUpperValue)) {
xUpperValue = 0.0f;
}

retValue.origin = CGPointMake(xLowerValue, (self.bounds.size.height/2.0f) - (retValue.size.height/2.0f));
retValue.size.width = xUpperValue-xLowerValue;

Expand Down Expand Up @@ -516,8 +524,13 @@ - (CGRect)thumbRectForValue:(float)value image:(UIImage*) thumbImage
}

float xValue = ((self.bounds.size.width-thumbRect.size.width)*((value - _minimumValue) / (_maximumValue - _minimumValue)));

if (isnan(xValue)) {
xValue = 0.0f;
}

thumbRect.origin = CGPointMake(xValue, (self.bounds.size.height/2.0f) - (thumbRect.size.height/2.0f));

return CGRectIntegral(thumbRect);

}
Expand Down Expand Up @@ -612,14 +625,22 @@ -(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
_lowerHandle.highlighted = YES;
_lowerTouchOffset = touchPoint.x - _lowerHandle.center.x;

if ([self.delegate respondsToSelector:@selector(rangeSliderView:didSelectLowerHandleImageView:)]) {
[self.delegate rangeSliderView:self didSelectLowerHandleImageView:_lowerHandle];
}
}

if(CGRectContainsPoint(UIEdgeInsetsInsetRect(_upperHandle.frame, self.upperTouchEdgeInsets), touchPoint))
{
_upperHandle.highlighted = YES;
_upperTouchOffset = touchPoint.x - _upperHandle.center.x;

if ([self.delegate respondsToSelector:@selector(rangeSliderView:didSelectUpperHandleImageView:)]) {
[self.delegate rangeSliderView:self didSelectUpperHandleImageView:_upperHandle];
}
}

_stepValueInternal= _stepValueContinuously ? _stepValue : 0.0f;

return YES;
Expand Down Expand Up @@ -653,8 +674,12 @@ -(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
_lowerHandle.highlighted=NO;
}

if ([self.delegate respondsToSelector:@selector(rangeSliderView:didMoveLowerHandleImageView:)]) {
[self.delegate rangeSliderView:self didMoveLowerHandleImageView:_lowerHandle];
}
}

if(_upperHandle.highlighted )
{
float newValue = [self upperValueForCenterX:(touchPoint.x - _upperTouchOffset)];
Expand All @@ -671,8 +696,12 @@ -(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
_upperHandle.highlighted=NO;
}

if ([self.delegate respondsToSelector:@selector(rangeSliderView:didMoveUpperHandleImageView:)]) {
[self.delegate rangeSliderView:self didMoveUpperHandleImageView:_lowerHandle];
}
}


//send the control event
if(_continuous)
Expand Down Expand Up @@ -702,6 +731,10 @@ -(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
}

[self sendActionsForControlEvents:UIControlEventValueChanged];

if ([self.delegate respondsToSelector:@selector(rangeSliderViewDidEndSelect:)]) {
[self.delegate rangeSliderViewDidEndSelect:self];
}
}

@end