259 lines
6.2 KiB
Objective-C
Executable File
259 lines
6.2 KiB
Objective-C
Executable File
//
|
||
// NYSliderPopover.m
|
||
// NYReader
|
||
//
|
||
// Created by Cassius Pacheco on 21/12/12.
|
||
// Copyright (c) 2012 Nyvra Software. All rights reserved.
|
||
//
|
||
|
||
#import "NYSliderPopover.h"
|
||
#import "NYPopover.h"
|
||
#define thumbBound_x 10
|
||
#define thumbBound_y 20
|
||
|
||
@interface NYSliderPopover()
|
||
{
|
||
CGRect _lastBounds;
|
||
}
|
||
@end
|
||
|
||
@implementation NYSliderPopover
|
||
|
||
- (instancetype)init
|
||
{
|
||
self = [super init];
|
||
if (self) {
|
||
[self thumbimageView];
|
||
[self createUI];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (instancetype)initWithFrame:(CGRect)frame
|
||
{
|
||
self = [super initWithFrame:frame];
|
||
if (self) {
|
||
[self thumbimageView];
|
||
[self createUI];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
#pragma mark -
|
||
#pragma mark UISlider methods
|
||
|
||
- (NYPopover *)popover
|
||
{
|
||
if (_popover == nil){
|
||
//Default size, can be changed after
|
||
[self addTarget:self action:@selector(updatePopoverFrame) forControlEvents:UIControlEventValueChanged];
|
||
_popover = [[NYPopover alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - 32, 40, 32)];
|
||
[self updatePopoverFrame];
|
||
_popover.alpha = 0;
|
||
[self.superview addSubview:_popover];
|
||
// [self thumbimageView];
|
||
// [self createUI];
|
||
}
|
||
|
||
return _popover;
|
||
}
|
||
|
||
- (void)setValue:(float)value
|
||
{
|
||
[super setValue:value];
|
||
//[self updatePopoverFrame];
|
||
|
||
}
|
||
|
||
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
|
||
{
|
||
[self updatePopoverFrame];
|
||
[self showPopoverAnimated:YES];
|
||
[super touchesBegan:touches withEvent:event];
|
||
}
|
||
|
||
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
|
||
{
|
||
[self hidePopoverAnimated:YES];
|
||
[super touchesEnded:touches withEvent:event];
|
||
}
|
||
|
||
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
|
||
{
|
||
[self hidePopoverAnimated:YES];
|
||
[super touchesCancelled:touches withEvent:event];
|
||
}
|
||
|
||
#pragma mark -
|
||
#pragma mark - Popover methods
|
||
|
||
- (void)updatePopoverFrame
|
||
{
|
||
//Inspired in Collin Ruffenach's ELCSlider https://github.com/elc/ELCSlider/blob/master/ELCSlider/ELCSlider.m#L53
|
||
|
||
CGFloat minimum = self.minimumValue;
|
||
CGFloat maximum = self.maximumValue;
|
||
CGFloat value = self.value;
|
||
|
||
if (minimum < 0.0) {
|
||
|
||
value = self.value - minimum;
|
||
maximum = maximum - minimum;
|
||
minimum = 0.0;
|
||
}
|
||
|
||
CGFloat x = self.frame.origin.x;
|
||
CGFloat maxMin = (maximum + minimum) / 2.0;
|
||
|
||
x += (((value - minimum) / (maximum - minimum)) * self.frame.size.width) - (self.popover.frame.size.width / 2.0);
|
||
|
||
if (value > maxMin) {
|
||
|
||
value = (value - maxMin) + (minimum * 1.0);
|
||
value = value / maxMin;
|
||
value = value * 11.0;
|
||
|
||
x = x - value;
|
||
|
||
} else {
|
||
|
||
value = (maxMin - value) + (minimum * 1.0);
|
||
value = value / maxMin;
|
||
value = value * 11.0;
|
||
|
||
x = x + value;
|
||
}
|
||
|
||
CGRect popoverRect = self.popover.frame;
|
||
popoverRect.origin.x = x;
|
||
popoverRect.origin.y = self.frame.origin.y - popoverRect.size.height - 1;
|
||
|
||
self.popover.frame = popoverRect;
|
||
}
|
||
|
||
- (void)showPopover
|
||
{
|
||
[self showPopoverAnimated:NO];
|
||
}
|
||
|
||
- (void)showPopoverAnimated:(BOOL)animated
|
||
{
|
||
if (animated) {
|
||
[UIView animateWithDuration:0.25 animations:^{
|
||
self.popover.alpha = 1.0;
|
||
}];
|
||
} else {
|
||
self.popover.alpha = 1.0;
|
||
}
|
||
}
|
||
|
||
- (void)hidePopover
|
||
{
|
||
[self hidePopoverAnimated:NO];
|
||
}
|
||
|
||
- (void)hidePopoverAnimated:(BOOL)animated
|
||
{
|
||
if (animated) {
|
||
[UIView animateWithDuration:0.25 animations:^{
|
||
self.popover.alpha = 0;
|
||
}];
|
||
} else {
|
||
self.popover.alpha = 0;
|
||
}
|
||
}
|
||
|
||
-(void)createUI{
|
||
|
||
self.minimumValue = 0.0;
|
||
self.maximumValue = 1.0;
|
||
// self.minimumTrackTintColor = GIGARGB(216,216, 216, 1);
|
||
// self.maximumTrackTintColor = GIGARGB(216,216, 216, 1);
|
||
self.minimumTrackTintColor = GIGAUIColorFromRGBA(0xEA4D4D);
|
||
self.maximumTrackTintColor = GIGAUIColorFromRGBA(0xEA4D4D);
|
||
|
||
}
|
||
|
||
-(CGRect)trackRectForBounds:(CGRect)bounds
|
||
{
|
||
bounds.origin.x = bounds.origin.x;
|
||
bounds.origin.y = bounds.origin.y;
|
||
bounds.size.height = bounds.size.height;
|
||
bounds.size.width = bounds.size.width;
|
||
return bounds;
|
||
}
|
||
|
||
|
||
|
||
-(void)thumbimageView{
|
||
|
||
[self setThumbImage:[self OriginImage:[UIImage imageNamed:@"btn_slider_thumb"] scaleToSize:CGSizeMake(40, 40)] forState:UIControlStateNormal];
|
||
[self setThumbImage:[self OriginImage:[UIImage imageNamed:@"btn_slider_thumb"] scaleToSize:CGSizeMake(40, 40)] forState:UIControlStateHighlighted];
|
||
}
|
||
|
||
-(UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size
|
||
|
||
{
|
||
UIGraphicsBeginImageContext(size);//size为CGSize类型,即你所需要的图片尺寸
|
||
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
|
||
|
||
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
|
||
|
||
UIGraphicsEndImageContext();
|
||
|
||
return scaledImage;
|
||
|
||
}
|
||
|
||
|
||
// 扩大thumb 触控区域
|
||
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
|
||
|
||
{
|
||
rect.origin.x = rect.origin.x;
|
||
rect.size.width = rect.size.width ;
|
||
CGRect result = [super thumbRectForBounds:bounds trackRect:rect value:value];
|
||
|
||
_lastBounds = result;
|
||
return result;
|
||
|
||
}
|
||
|
||
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
|
||
|
||
UIView *result = [super hitTest:point withEvent:event];
|
||
|
||
if (point.x < 0 || point.x > self.bounds.size.width){
|
||
|
||
return result;
|
||
|
||
}
|
||
|
||
if ((point.y >= -thumbBound_y) && (point.y < _lastBounds.size.height + thumbBound_y)) {
|
||
float value = 0.0;
|
||
value = point.x - self.bounds.origin.x;
|
||
value = value/self.bounds.size.width;
|
||
|
||
value = value < 0? 0 : value;
|
||
value = value > 1? 1: value;
|
||
|
||
value = value * (self.maximumValue - self.minimumValue) + self.minimumValue;
|
||
[self setValue:value animated:YES];
|
||
}
|
||
return result;
|
||
|
||
}
|
||
|
||
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
|
||
BOOL result = [super pointInside:point withEvent:event];
|
||
if (!result && point.y > -10) {
|
||
if ((point.x >= _lastBounds.origin.x - thumbBound_x) && (point.x <= (_lastBounds.origin.x + _lastBounds.size.width + thumbBound_x)) && (point.y < (_lastBounds.size.height + thumbBound_y))) {
|
||
result = YES;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
@end
|