This commit is contained in:
lianxiang
2018-09-12 20:30:22 +08:00
parent a2de9505ee
commit 7d0027f58d
21 changed files with 805 additions and 110 deletions
@@ -0,0 +1,15 @@
//
// NYPopover.h
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NYPopover : UIView
@property (nonatomic, strong) UILabel *textLabel;
@end
@@ -0,0 +1,134 @@
//
// NYPopover.m
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import "NYPopover.h"
@implementation NYPopover
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.font = [UIFont boldSystemFontOfSize:13];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.adjustsFontSizeToFitWidth = YES;
self.opaque = NO;
[self addSubview:self.textLabel];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
CGFloat y = (frame.size.height - 26) / 3;
if (frame.size.height < 38)
y = 0;
self.textLabel.frame = CGRectMake(0, y, frame.size.width, 26);
}
- (void)drawRect:(CGRect)rect
{
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* gradientColor = [UIColor colorWithRed: 0.267 green: 0.303 blue: 0.335 alpha: 1];
UIColor* gradientColor2 = [UIColor colorWithRed: 0.04 green: 0.04 blue: 0.04 alpha: 1];
UIColor* shadowColor2 = [UIColor colorWithRed: 0.524 green: 0.553 blue: 0.581 alpha: 0.3];
//// Gradient Declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)gradientColor.CGColor,
(id)gradientColor2.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// Shadow Declarations
UIColor* innerShadow = shadowColor2;
CGSize innerShadowOffset = CGSizeMake(0, 1.5);
CGFloat innerShadowBlurRadius = 0.5;
//// Frames
CGRect frame = self.bounds;
//// Subframes
CGRect frame2 = CGRectMake(CGRectGetMinX(frame) + floor((CGRectGetWidth(frame) - 11) * 0.51724 + 0.5), CGRectGetMinY(frame) + CGRectGetHeight(frame) - 9, 11, 9);
//// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMinY(frame) + 4.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMaxY(frame) - 11.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMaxX(frame) - 4.5, CGRectGetMaxY(frame) - 7.5) controlPoint1: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMaxY(frame) - 9.29) controlPoint2: CGPointMake(CGRectGetMaxX(frame) - 2.29, CGRectGetMaxY(frame) - 7.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame2) + 10.64, CGRectGetMinY(frame2) + 1.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame2) + 5.5, CGRectGetMinY(frame2) + 8)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame2) + 0.36, CGRectGetMinY(frame2) + 1.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 4.5, CGRectGetMaxY(frame) - 7.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMaxY(frame) - 11.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 2.29, CGRectGetMaxY(frame) - 7.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMaxY(frame) - 9.29)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMinY(frame) + 4.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 4.5, CGRectGetMinY(frame) + 0.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMinY(frame) + 2.29) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 2.29, CGRectGetMinY(frame) + 0.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMaxX(frame) - 4.5, CGRectGetMinY(frame) + 0.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMinY(frame) + 4.5) controlPoint1: CGPointMake(CGRectGetMaxX(frame) - 2.29, CGRectGetMinY(frame) + 0.5) controlPoint2: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMinY(frame) + 2.29)];
[bezierPath closePath];
CGContextSaveGState(context);
[bezierPath addClip];
CGRect bezierBounds = bezierPath.bounds;
CGContextDrawLinearGradient(context, gradient,
CGPointMake(CGRectGetMidX(bezierBounds), CGRectGetMinY(bezierBounds)),
CGPointMake(CGRectGetMidX(bezierBounds), CGRectGetMaxY(bezierBounds)),
0);
CGContextRestoreGState(context);
////// Bezier Inner Shadow
CGRect bezierBorderRect = CGRectInset([bezierPath bounds], -innerShadowBlurRadius, -innerShadowBlurRadius);
bezierBorderRect = CGRectOffset(bezierBorderRect, -innerShadowOffset.width, -innerShadowOffset.height);
bezierBorderRect = CGRectInset(CGRectUnion(bezierBorderRect, [bezierPath bounds]), -1, -1);
UIBezierPath* bezierNegativePath = [UIBezierPath bezierPathWithRect: bezierBorderRect];
[bezierNegativePath appendPath: bezierPath];
bezierNegativePath.usesEvenOddFillRule = YES;
CGContextSaveGState(context);
{
CGFloat xOffset = innerShadowOffset.width + round(bezierBorderRect.size.width);
CGFloat yOffset = innerShadowOffset.height;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
innerShadowBlurRadius,
innerShadow.CGColor);
[bezierPath addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(bezierBorderRect.size.width), 0);
[bezierNegativePath applyTransform: transform];
[[UIColor grayColor] setFill];
[bezierNegativePath fill];
}
CGContextRestoreGState(context);
[[UIColor blackColor] setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
@end
@@ -0,0 +1,21 @@
//
// NYSliderPopover.h
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "NYPopover.h"
@interface NYSliderPopover : UISlider
@property (nonatomic, strong) NYPopover *popover;
- (void)showPopover;
- (void)showPopoverAnimated:(BOOL)animated;
- (void)hidePopover;
- (void)hidePopoverAnimated:(BOOL)animated;
@end
@@ -0,0 +1,187 @@
//
// 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"
@implementation NYSliderPopover
- (instancetype)init
{
self = [super init];
if (self) {
}
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.value = 0.5;
self.minimumTrackTintColor = GIGARGB(216,216, 216, 1);
self.maximumTrackTintColor = GIGARGB(216,216, 216, 1);
}
-(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;
}
@end