Skip to content

Commit

Permalink
fixed bugs with hue. improved touch area of sliders.
Browse files Browse the repository at this point in the history
  • Loading branch information
JARinteractive authored and JARinteractive committed Jul 22, 2013
1 parent 134aa76 commit 71885df
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 18 deletions.
8 changes: 5 additions & 3 deletions example/ESCColorPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@
LastUpgradeCheck = 0500;
ORGANIZATIONNAME = Escappe;
TargetAttributes = {
4DD5C9D01794EA3200BB1615 = {
DevelopmentTeam = CAFGS5263J;
};
4DD5C9EB1794EA3200BB1615 = {
TestTargetID = 4DD5C9D01794EA3200BB1615;
};
Expand Down Expand Up @@ -408,7 +411,7 @@
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: James Rantanen (5Z3434WNR8)";
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
Expand All @@ -429,6 +432,7 @@
"-all_load",
"-ObjC",
);
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "EBE838DE-CFC8-4C79-9DC4-FEED8E294C77";
SDKROOT = iphoneos;
};
name = Debug;
Expand Down Expand Up @@ -473,7 +477,6 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_IDENTITY = "iPhone Developer";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "ESCColorPicker/ESCColorPicker-Prefix.pch";
INFOPLIST_FILE = "ESCColorPicker/ESCColorPicker-Info.plist";
Expand All @@ -488,7 +491,6 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_IDENTITY = "iPhone Developer";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "ESCColorPicker/ESCColorPicker-Prefix.pch";
INFOPLIST_FILE = "ESCColorPicker/ESCColorPicker-Info.plist";
Expand Down
2 changes: 1 addition & 1 deletion example/ESCColorPicker/ESCColorControlThumb.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ - (id)initWithFrame:(CGRect)frame {
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 2.0;
self.layer.shadowOffset = CGSizeMake(0.0, 1.0);
self.layer.shadowOffset = CGSizeZero;

self.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1.0];
}
Expand Down
2 changes: 1 addition & 1 deletion example/ESCColorPicker/ESCColorPicker-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.escappe.${PRODUCT_NAME:rfc1034identifier}</string>
<string>com.bwater.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
5 changes: 3 additions & 2 deletions example/ESCColorPicker/ESCColorPicker-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#endif
27 changes: 22 additions & 5 deletions example/ESCColorPicker/ESCColorPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ @implementation ESCColorPickerView

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0];

[self escRegisterObserverProtocol:@protocol(ESCColorPickerViewObserver)];

self.hueWheel = [[ESCHueWheel alloc] init];
Expand All @@ -31,8 +33,8 @@ - (instancetype)initWithFrame:(CGRect)frame {
[self.brightnessSlider escAddObserver:self forSelector:@selector(sliderValueDidChange:) forwardingToSelector:@selector(brightnessDidChange:)];
[self addSubview:self.brightnessSlider];

self.colorView = [[UIView alloc] init];
[self addSubview:self.colorView];
//self.colorView = [[UIView alloc] init];
//[self addSubview:self.colorView];
}
return self;
}
Expand All @@ -41,12 +43,16 @@ - (void)layoutSubviews {
[super layoutSubviews];

CGRect contentRect = CGRectInset(self.bounds, PADDING, PADDING);
CGFloat sliderHeight = 40.0;

self.brightnessSlider.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMaxY(contentRect) - sliderHeight, CGRectGetWidth(contentRect), sliderHeight);
self.saturationSlider.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMinY(self.brightnessSlider.frame) - PADDING - sliderHeight, CGRectGetWidth(contentRect), sliderHeight);


self.colorView.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(contentRect), CGRectGetWidth(self.bounds), 80.0);

self.hueWheel.frame = CGRectMake(CGRectGetMinX(contentRect) + 10.0, CGRectGetMaxY(self.colorView.frame) + PADDING, CGRectGetWidth(contentRect) - 20.0, CGRectGetWidth(contentRect) - 20.0);
self.saturationSlider.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMaxY(self.hueWheel.frame) + PADDING, CGRectGetWidth(contentRect), 40.0);
self.brightnessSlider.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMaxY(self.saturationSlider.frame) + PADDING, CGRectGetWidth(contentRect), 40.0);
CGFloat hueWheelSide = CGRectGetWidth(contentRect) - 40.0;
self.hueWheel.frame = CGRectMake(CGRectGetMidX(contentRect) - hueWheelSide / 2.0, CGRectGetMinY(self.saturationSlider.frame) - PADDING - hueWheelSide, hueWheelSide, hueWheelSide);
}

- (void)saturationDidChange:(CGFloat)saturation {
Expand All @@ -72,6 +78,17 @@ - (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)b

self.colorView.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0];

[self.hueWheel setHue:hue saturation:saturation brightness:brightness];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (CGRectContainsPoint(CGRectInset(self.saturationSlider.frame, -20.0, -5.0), point)) {
hitView = self.saturationSlider;
} else if (CGRectContainsPoint(CGRectInset(self.brightnessSlider.frame, -20.0, -5.0), point)) {
hitView = self.brightnessSlider;
}
return hitView;
}

@end
2 changes: 1 addition & 1 deletion example/ESCColorPicker/ESCGradientSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (void)panGesture:(UIPanGestureRecognizer *)panGestureRecognizer {

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGFloat touchLocationOnThumb = [touch locationInView:self.thumb].x;
return touchLocationOnThumb > -20.0 && touchLocationOnThumb < CGRectGetMaxX(self.thumb.bounds) + 20.0;
return touchLocationOnThumb > -40.0 && touchLocationOnThumb < CGRectGetMaxX(self.thumb.bounds) + 40.0;
}

- (void)layoutSubviews {
Expand Down
2 changes: 2 additions & 0 deletions example/ESCColorPicker/ESCHueWheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

@interface ESCHueWheel : UIView<ESCObservable>

- (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness;

@end
120 changes: 115 additions & 5 deletions example/ESCColorPicker/ESCHueWheel.m
Original file line number Diff line number Diff line change
@@ -1,35 +1,145 @@
#import "ESCHueWheel.h"
#import "ESCColorControlThumb.h"

@interface ESCHueWheel()<ESCObservableInternal>
@interface ESCHueWheel()<ESCObservableInternal, UIGestureRecognizerDelegate>

@property (nonatomic) UIView *wheel;
@property (nonatomic) UIView *wheelCenter;
@property (nonatomic) UIView *wheelCenterBorder;
@property (nonatomic) NSArray *colorSwatchViews;
@property (nonatomic) UIView *thumb;
@property (nonatomic) CGFloat hue;
@property (nonatomic, readonly) CGFloat angle;

@property (nonatomic) CGFloat relativeTouchAngle;

@end

@implementation ESCHueWheel

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self escRegisterObserverProtocol:@protocol(ESCHueWheelObserver)];

self.wheel = [[UIView alloc] init];
self.wheel.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
self.wheel.backgroundColor = [UIColor colorWithWhite:0.25 alpha:1.0];
self.wheel.clipsToBounds = YES;
[self addSubview:self.wheel];

NSInteger numberOfSwatches = 12;
NSMutableArray *colorSwatchViews = [NSMutableArray arrayWithCapacity:numberOfSwatches];
for (NSInteger i = 0; i < numberOfSwatches; i++) {
UIView *colorSwatchView = [[UIView alloc] init];
colorSwatchView.backgroundColor = [UIColor orangeColor];
//colorSwatchView.layer.edgeAntialiasingMask = kCALayerBottomEdge | kCALayerLeftEdge | kCALayerRightEdge | kCALayerTopEdge;
colorSwatchView.layer.shouldRasterize = YES;
colorSwatchView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
[self.wheel addSubview:colorSwatchView];
[colorSwatchViews addObject:colorSwatchView];
}
self.colorSwatchViews = colorSwatchViews;


self.wheelCenterBorder = [[UIView alloc] init];
self.wheelCenterBorder.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
self.wheelCenterBorder.layer.shadowColor = [UIColor blackColor].CGColor;
self.wheelCenterBorder.layer.shadowOpacity = 0.5;
self.wheelCenterBorder.layer.shadowRadius = 2.0;
self.wheelCenterBorder.layer.shadowOffset = CGSizeZero;
[self addSubview:self.wheelCenterBorder];

self.thumb = [[UIView alloc] init];
self.thumb.layer.borderColor = self.wheelCenterBorder.backgroundColor.CGColor;
self.thumb.layer.borderWidth = 3.0;
self.thumb.layer.shouldRasterize = YES;
self.thumb.layer.rasterizationScale = [[UIScreen mainScreen] scale];
[self.wheelCenterBorder addSubview:self.thumb];

self.wheelCenter = [[UIView alloc] init];
self.wheelCenter.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
[self addSubview:self.wheelCenter];
[self.wheelCenterBorder addSubview:self.wheelCenter];

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
panGestureRecognizer.delegate = self;
[self addGestureRecognizer:panGestureRecognizer];
}
return self;
}

- (void)panGesture:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint touchLocation = [panGestureRecognizer locationInView:self.wheel];
touchLocation.x -= CGRectGetMidX(self.wheel.bounds);
touchLocation.y -= CGRectGetMidY(self.wheel.bounds);
CGFloat touchAngle = atan(touchLocation.y / touchLocation.x);
touchAngle += touchLocation.x < 0.0 ? M_PI : 0.0;
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
self.relativeTouchAngle = self.angle - touchAngle;
} else if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
CGFloat hue = (touchAngle + self.relativeTouchAngle + M_PI_2) / (M_PI * 2);
hue = fmod(hue + 1.0, 1.0); //correct if hue is negative
self.hue = hue;
[self.escNotifier hueDidChange:self.hue];
}
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return CGRectContainsPoint(CGRectInset(self.thumb.bounds, -20.0, -20.0), [touch locationInView:self.thumb]);
}

- (void)setHue:(CGFloat)hue {
_hue = hue;
[self setNeedsLayout];
}

- (CGFloat)angle {
return self.hue * 2 * M_PI - M_PI_2;
}

- (void)layoutSubviews {
[super layoutSubviews];

self.wheel.frame = CGRectInset(self.bounds, 5.0, 5.0);
self.wheel.layer.cornerRadius = CGRectGetWidth(self.wheel.frame) / 2.0;
CGFloat radius = CGRectGetWidth(self.wheel.frame) / 2.0;
self.wheel.layer.cornerRadius = radius;

self.wheelCenter.frame = CGRectInset(self.bounds, 35.0, 35.0);
self.wheelCenterBorder.frame = CGRectInset(self.bounds, 35.0, 35.0);
self.wheelCenterBorder.layer.cornerRadius = CGRectGetWidth(self.wheelCenterBorder.frame) / 2.0;
self.wheelCenter.frame = CGRectInset(self.wheelCenterBorder.bounds, 3.0, 3.0);
self.wheelCenter.layer.cornerRadius = CGRectGetWidth(self.wheelCenter.frame) / 2.0;

NSInteger i = 0;
CGFloat colorSwatchRadius = radius - ((radius - CGRectGetWidth(self.wheelCenter.frame) / 2.0)) / 2.0;
for (UIView *colorSwatchView in self.colorSwatchViews) {
CGFloat angle = (CGFloat)i / [self.colorSwatchViews count] * 2 * M_PI - M_PI_2;
CGFloat x = cos(angle) * colorSwatchRadius + CGRectGetMidX(self.wheel.bounds);
CGFloat y = sin(angle) * colorSwatchRadius + CGRectGetMidY(self.wheel.bounds);
colorSwatchView.center = CGPointMake(x, y);
colorSwatchView.bounds = CGRectMake(0.0, 0.0, 36.0, 10.0);
colorSwatchView.transform = CGAffineTransformMakeRotation(angle);
i++;
}

CGFloat thumbRadius = CGRectGetWidth(self.wheelCenterBorder.frame) / 2.0 + 10.0;
CGFloat x = cos(self.angle) * thumbRadius + CGRectGetMidX(self.wheelCenterBorder.bounds);
CGFloat y = sin(self.angle) * thumbRadius + CGRectGetMidY(self.wheelCenterBorder.bounds);
self.thumb.center = CGPointMake(x, y);
self.thumb.bounds = CGRectMake(0.0, 0.0, 16.0, 64.0);
self.thumb.transform = CGAffineTransformMakeRotation(self.angle + M_PI_2);
self.thumb.layer.cornerRadius = 16.0;
}

- (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0];
self.wheelCenter.backgroundColor = color;
self.thumb.backgroundColor = color;

NSInteger i = 0;
for (UIView *colorSwatchView in self.colorSwatchViews) {
colorSwatchView.backgroundColor = [UIColor colorWithHue:(CGFloat)i / [self.colorSwatchViews count] saturation:saturation brightness:brightness alpha:1.0];
i++;
}

self.hue = hue;
}

@end

0 comments on commit 71885df

Please sign in to comment.