This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSOpacitySlider.m
executable file
·98 lines (78 loc) · 2.72 KB
/
RSOpacitySlider.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// RSOpacitySlider.m
// RSColorPicker
//
// Created by Jared Allen on 5/16/13.
// Copyright (c) 2013 Red Cactus LLC. All rights reserved.
//
#import "RSOpacitySlider.h"
/**
* Returns image that looks like a checkered background.
*/
UIImage* RSOpacityBackgroundImage(CGFloat length, UIColor *color) {
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, length*0.5, length*0.5)];
UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake(length*0.5, length*0.5, length*0.5, length*0.5)];
UIBezierPath* rectangle3Path = [UIBezierPath bezierPathWithRect: CGRectMake(0, length*0.5, length*0.5, length*0.5)];
UIBezierPath* rectangle4Path = [UIBezierPath bezierPathWithRect: CGRectMake(length*0.5, 0, length*0.5, length*0.5)];
UIGraphicsBeginImageContext(CGSizeMake(length, length));
[color setFill];
[rectanglePath fill];
[rectangle2Path fill];
[[UIColor whiteColor] setFill];
[rectangle3Path fill];
[rectangle4Path fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@implementation RSOpacitySlider
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initRoutine];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initRoutine];
}
return self;
}
-(void)initRoutine {
self.minimumValue = 0.0;
self.maximumValue = 1.0;
self.continuous = YES;
self.enabled = YES;
self.userInteractionEnabled = YES;
UIImage *backgroundImage = RSOpacityBackgroundImage(16.f, [UIColor colorWithWhite:0.5 alpha:1.0]);
self.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
[self addTarget:self action:@selector(myValueChanged:) forControlEvents:UIControlEventValueChanged];
}
- (CGRect)trackRectForBounds:(CGRect)bounds
{
//to hide the track view
return CGRectMake(0, ceilf(bounds.size.height / 2), bounds.size.width, 0);
}
-(void)myValueChanged:(id)notif {
_colorPicker.opacity = self.value;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef space = CGColorSpaceCreateDeviceGray();
NSArray* colors = [[NSArray alloc] initWithObjects:
(id)[UIColor colorWithWhite:0 alpha:0].CGColor,
(id)[UIColor colorWithWhite:1 alpha:1].CGColor,nil];
CGGradientRef myGradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, myGradient, CGPointZero, CGPointMake(rect.size.width, 0), 0);
CGGradientRelease(myGradient);
CGColorSpaceRelease(space);
}
-(void)setColorPicker:(RSColorPickerView*)cp {
_colorPicker = cp;
if (!_colorPicker) { return; }
self.value = [_colorPicker brightness];
}
@end