Custom Slider In SpriteKit - objective-c

I'm trying to create a horizontal slider like the one shown below in SpriteKit (in Objective C for Mac OS).
I'm certainly doing something wrong because the "knob" of the slider never moves left, it only moves right and I'm not sure what the issue is. I'm using the mouseDragged: method to handle everything. Here's the code below:
Slider.m
#import "Slider.h"
#interface Slider()
#property CGSize dimensions;
#property SKSpriteNode *background, *foreground, *knob;
#end
#implementation Slider
-(instancetype) initWithDimensions:(CGSize)dimensions Percentage:(double)percentage {
if (self = [super init]) {
_dimensions = dimensions;
_percentage = percentage;
[self initBackgroundSprite];
[self initForegroundSprite];
[self initKnob];
self.userInteractionEnabled = YES;
}
return self;
}
-(void) initBackgroundSprite {
_background = [SKSpriteNode spriteNodeWithImageNamed:#"sliderBG"];
_background.centerRect = CGRectMake(6.0/13.0, 5.0/11.0, 1.0/13.0, 1.0/11.0);
[_background setAnchorPoint:CGPointMake(0, 0.5)];
double xScale = _dimensions.width / _background.frame.size.width;
double yScale = _dimensions.height / _background.frame.size.height;
[_background setXScale:xScale];
[_background setYScale:yScale];
[self addChild:_background];
}
-(void) initForegroundSprite {
_foreground = [SKSpriteNode spriteNodeWithImageNamed:#"sliderFG"];
_foreground.centerRect = CGRectMake(6.0/13.0, 5.0/11.0, 1.0/13.0, 1.0/11.0);
[_foreground setAnchorPoint:CGPointMake(0, 0.5)];
double xScale = _dimensions.width*_percentage / _foreground.frame.size.width;
double yScale = _dimensions.height / _foreground.frame.size.height;
[_foreground setXScale:xScale];
[_foreground setYScale:yScale];
[self addChild:_foreground];
}
-(void) initKnob {
_knob = [SKSpriteNode spriteNodeWithImageNamed:#"sliderKnob"];
_knob.centerRect = CGRectMake(6.0/13.0, 5.0/11.0, 1.0/13.0, 1.0/11.0);
[_knob setAnchorPoint:CGPointMake(0, 0.5)];
double scaleFactor = 2 / (_knob.frame.size.height / _background.frame.size.height);
NSLog(#"%f, %f", _knob.frame.size.height, _background.frame.size.height);
[_knob setScale:scaleFactor];
[_knob setZPosition:2];
[_knob setName:#"knob"];
[self addChild:_knob];
}
-(void) mouseDragged:(NSEvent *)event {
CGPoint location = [event locationInNode:self];
NSArray *nodes = [self nodesAtPoint:location];
for (SKNode *node in nodes) {
if ([node isKindOfClass:[SKSpriteNode class]]) {
SKSpriteNode *sprite = (SKSpriteNode*)node;
if ([sprite.name isEqualToString:#"knob"]) {
[self updateKnobPositionWithLocation:location];
}
}
}
}
-(void) updateKnobPositionWithLocation:(CGPoint)location {
double x = location.x;
double y = _knob.position.y; //don't want the y-pos to change
double bgX = _background.position.x; //x pos of slider
double width = _background.frame.size.width; //width of slider
if (x > bgX + width)//if knob goes beyond width of slider, restrict to width
x = bgX + width;
else if (x < bgX)
x = bgX;
[_knob setPosition:CGPointMake(x, y)];
}
Here's a video to illustrate the behaviour:
https://drive.google.com/open?id=0B8Zfr1yQCdf-OG5kZFRWNFgxd1k

I've solved all of the issues with the slider class. Thanks for the help everyone. In case anyone is interested, here's the full code:
Slider.h
#import <SpriteKit/SpriteKit.h>
#interface Slider : SKSpriteNode
#property double percentage;
-(instancetype) initWithDimensions:(CGSize)dimensions Percentage:(double)percentage;
#end
Slider.m
#import "Slider.h"
#interface Slider()
#property CGSize dimensions;
#property SKSpriteNode *background, *foreground, *knob;
#property double backgroundWidth, foregroundWidth;
#property bool isBeingUsed;
#property SKLabelNode *percentageLabel;
#end
#implementation Slider
//creates a slider with certain dimensions and at a specific starting percentage clamped [0, 1]
-(instancetype) initWithDimensions:(CGSize)dimensions Percentage:(double)percentage {
if (self = [super init]) {
_dimensions = dimensions;
_percentage = percentage;
_isBeingUsed = NO;
[self initBackgroundSprite];
[self initForegroundSprite];
[self initKnob];
[self initPercentageLabel];
self.userInteractionEnabled = YES;
}
return self;
}
//sprite initialization
-(void) initBackgroundSprite {
_background = [SKSpriteNode spriteNodeWithImageNamed:#"sliderBG"];
_background.centerRect = CGRectMake(6.0/13.0, 5.0/11.0, 1.0/13.0, 1.0/11.0);
_backgroundWidth = _background.frame.size.width;
[_background setAnchorPoint:CGPointMake(0, 0.5)];
double xScale = _dimensions.width / _backgroundWidth;
double yScale = _dimensions.height / _background.frame.size.height;
[_background setXScale:xScale];
[_background setYScale:yScale];
[self addChild:_background];
}
-(void) initForegroundSprite {
_foreground = [SKSpriteNode spriteNodeWithImageNamed:#"sliderFG"];
_foreground.centerRect = CGRectMake(6.0/13.0, 5.0/11.0, 1.0/13.0, 1.0/11.0);
[_foreground setAnchorPoint:CGPointMake(0, 0.5)];
_foregroundWidth = _foreground.frame.size.width;
double xScale = _dimensions.width*_percentage / _foregroundWidth;
double yScale = _dimensions.height / _foreground.frame.size.height;
[_foreground setXScale:xScale];
[_foreground setYScale:yScale];
[self addChild:_foreground];
}
-(void) initKnob {
_knob = [SKSpriteNode spriteNodeWithImageNamed:#"sliderKnob"];
_knob.centerRect = CGRectMake(6.0/13.0, 5.0/11.0, 1.0/13.0, 1.0/11.0);
[_knob setAnchorPoint:CGPointMake(0.5, 0.5)];
double scaleFactor = 2 / (_knob.frame.size.height / _background.frame.size.height);
[_knob setScale:scaleFactor];
[_knob setPosition:CGPointMake(_foreground.frame.size.width, -_knob.frame.size.height*0.05)];
[_knob setZPosition:2];
[_knob setName:#"knob"];
[self addChild:_knob];
}
-(void) initPercentageLabel {
_percentageLabel = [SKLabelNode labelNodeWithFontNamed:#"Hiragino Kaku Gothic Std"];
[_percentageLabel setText:[NSString stringWithFormat:#"%.0f%%", _percentage*100]];
[_percentageLabel setFontSize:15];
double x = _dimensions.width * 1.05;
double y = _knob.frame.size.height*0.05;
[_percentageLabel setPosition:CGPointMake(x, y)];
[_percentageLabel setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeLeft];
[_percentageLabel setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];
[self addChild:_percentageLabel];
}
-(void) mouseDragged:(NSEvent *)event {
CGPoint location = [event locationInNode:self];
_isBeingUsed = YES;
[self updateKnobPositionWithLocation:location];
}
-(void) mouseDown:(NSEvent *)event {
[super mouseDown:event];
CGPoint location = [event locationInNode:self];
_isBeingUsed = YES;
[self updateKnobPositionWithLocation:location];
}
-(void) mouseUp:(NSEvent *)event {
[super mouseUp:event];
_isBeingUsed = NO;
}
-(void) updateKnobPositionWithLocation:(CGPoint)location {
if (_isBeingUsed) {
double x = location.x;
double y = _knob.position.y; //don't want the y-pos to change
double bgX = _background.position.x; //x pos of slider
double width = _background.frame.size.width; //width of slider
if (x > bgX + width)//if knob goes beyond width of slider, restrict to width
x = bgX + width;
else if (x < bgX)
x = bgX;
[_knob setPosition:CGPointMake(x, y)];
[self updateForegroundPercentage];
//NSLog(#"(%f, %f)", _knob.position.x, _knob.position.y);
}
}
-(void) updateForegroundPercentage {
_percentage = _knob.position.x / _background.frame.size.width;
double xScale = _dimensions.width*_percentage / _foregroundWidth;
[_foreground setXScale:xScale];
[_percentageLabel setText:[NSString stringWithFormat:#"%.0f%%", _percentage*100]];
}
#end

Related

Specified the scroll of a UIScrollView with an UIProgressView

I would like to add a progress bar under the navigation bar which will indicate the progress of the scroll of a UIScrollView, I use (not work) : I do
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.progressView.progress = scrollView.contentOffset.y;
}
check now I created one demo:
#import "ViewController.h"
#interface ViewController ()<UIScrollViewDelegate>{
UIScrollView *scroll;
UIProgressView *indicater;
}
#end
#implementation ViewController
- (void)viewDidLoad {
indicater=[[UIProgressView alloc]initWithFrame:CGRectMake(0, 65, 320,10)];
indicater.backgroundColor=[UIColor redColor];
indicater.progress = 0.0;
indicater.hidden=false;
[self.view addSubview:indicater];
scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 80,300, 200)];
scroll.backgroundColor=[UIColor greenColor];
scroll.userInteractionEnabled=YES;
scroll.delegate=self;
scroll.contentSize = CGSizeMake(300 ,5000);
[self.view addSubview:scroll];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint offset = scroll.contentOffset;
CGRect bounds = scroll.bounds;
CGSize size = scroll.contentSize;
UIEdgeInsets inset = scroll.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
NSLog(#"%f",y);
NSLog(#"%f",h);
NSNumber *num=[NSNumber numberWithFloat:y];
NSNumber *num1=[NSNumber numberWithFloat:h];
[self UpdateProgressbar:num TotalscrollLenrth:num1];
}
-(void)UpdateProgressbar:(NSNumber*)currentscrollLenrth TotalscrollLenrth:(NSNumber*)n
{
NSString *totalLength = [NSString stringWithFormat:#"%#", n];
NSLog(#"%#", totalLength);
NSString *currentScrool = [NSString stringWithFormat:#"%#", currentscrollLenrth];
NSLog(#"%#", currentScrool);
if (currentscrollLenrth <= n) {
[indicater setProgress:([currentScrool intValue]/[totalLength floatValue])];
}
else {
// do somithig
}
}

Reproduce the catapult with elastic Angry bird SpriteKit

I want create a slingshot like Angry Bird and my question is how can I create a elastic which follow my sprite (a bird for example) when I load the launched and which hang to my slingshot ?
How can I do that with SpriteKit ?
Thank you for your help.
This kind of logic coding you really need to figure out on your own. I will give you something to work with. This is from my personal prototype:
-(void)fireRocket
{
if (timesRan <= 0) {
timesRan++;
}
else
{
[self enumerateChildNodesWithName:#"rocket" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *rocket = (SKSpriteNode *) node;
[rocket removeFromParent];
[rocket removeAllActions];
}];
}
SKSpriteNode * rocket = [SKSpriteNode spriteNodeWithImageNamed:#"rocket-310663_150"];
rocket.size = CGSizeMake(30, 32);
rocket.name = #"rocket";
rocket.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
rocket.position = CGPointMake(50, 185);
//CGFloat angle = M_PI_4;
CGFloat magnitude = 26;
CGFloat angle = [self angleOfRocketFire];
//CGFloat angle = M_PI_4;
[self addChild:rocket];
[rocket.physicsBody applyImpulse:CGVectorMake(magnitude*cos(angle),
magnitude*sin(angle))];
}
-(CGFloat)angleOfRocketFire
{
__block CGFloat angle;
__block CGPoint handlePosition;
[self enumerateChildNodesWithName:#"handle" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *handle = (SKSpriteNode *) node;
CGFloat xCoord = handle.position.x + (handle.size.width / 2);
CGFloat yCoord = handle.position.y + (handle.size.height / 2);
handlePosition = CGPointMake(xCoord, yCoord);
}];
[self enumerateChildNodesWithName:#"rocketLauncher" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *rocketLauncher = (SKSpriteNode *) node;
CGFloat xCoord = rocketLauncher.position.x + (rocketLauncher.size.width / 2);
CGFloat yCoord = rocketLauncher.position.y + (rocketLauncher.size.height / 2);
CGPoint launcherPosition = CGPointMake(xCoord, yCoord);
CGFloat slope = (handlePosition.y - launcherPosition.y) / (handlePosition.x - launcherPosition.x);
//angle = atan(slope);
angle = atan(slope);
NSLog(#"%f", slope);
}];
return angle;
}

Drawing leaves trails when resizing window

I am trying to draw a box in custom view. I have the points for the corners of the box and they are scaled. If I put a small rectangle at each vertex on the box and the resize the window it works perfectly. If I use [path stroke] to draw the lines between the vertices and then resize the window, I end up with a mess as the box is redrawn as the window gets resized, so instead of a single box in a resized window I get all the boxes that were redrawn. I have not found a way to clear the custom view of all the intermediary drawings. Any help would be appreciated. Also, I am really new at this.
#import <Cocoa/Cocoa.h>
#interface PointDisplay : NSView
{
NSBezierPath *pathForLine;
NSMutableArray *pointList;
float originalWidth;
float originalHeight;
}
#end
import "PointDisplay.h"
#implementation PointDisplay
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
int opts = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:area];
pathForLine = [[NSBezierPath alloc] init];
pointList = [[NSMutableArray alloc] init];
NSRect originalRect = [self bounds];
originalWidth = originalRect.size.width;
originalHeight = originalRect.size.height;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSRect myRect = [self bounds];
float widthNow = myRect.size.width;
float heightNow = myRect.size.height;
[[NSColor greenColor] set];
[NSBezierPath fillRect:myRect];
int rectWidth = 10, rectHeight = 10;
float lineWidth = 3.0;
if (pointList.count != 0)
{
NSPoint myPoint = [pointList[0] locationInWindow];
myPoint = [self convertPoint:myPoint fromView:nil];
myPoint.x = (myPoint.x - rectWidth / 2) * widthNow / originalWidth;
myPoint.y = (myPoint.y - rectHeight / 2) * heightNow / originalHeight;
[pathForLine moveToPoint:myPoint];
NSRect anotherRect = NSMakeRect(myPoint.x, myPoint.y, rectWidth, rectHeight);
[[NSColor redColor] set];
[pathForLine setLineWidth:lineWidth + .5 * ((int)lineWidth % 2)];
//[[NSBezierPath bezierPathWithRect:anotherRect] stroke];
for (int i = 1; i < pointList.count; i++)
{
myPoint = [pointList[i] locationInWindow];
myPoint = [self convertPoint:myPoint fromView:nil];
myPoint.x = (myPoint.x - 5) * widthNow / originalWidth;
myPoint.y = (myPoint.y - 5) * heightNow / originalHeight;
anotherRect = NSMakeRect(myPoint.x, myPoint.y, 10, 10);
[pathForLine lineToPoint:myPoint];
[pathForLine stroke];
//[[NSBezierPath bezierPathWithRect:anotherRect] fill];
}
}
}
#pragma mark Mouse events
- (void)mouseUp:(NSEvent *)theEvent
{
[pointList addObject:theEvent];
[self setNeedsDisplay:YES];
}
- (void)mouseExited:(NSEvent *)theEvent
{
[pathForLine closePath];
[pathForLine stroke];
[self setNeedsDisplay:YES];
}
#end

Only increase height or width using pinch gesture [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using UIPinchGestureRecognizer to scale uiviews in single direction
My code is below:
UIPinchGestureRecognizer *croperViewGessture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(croperViewScale:)];
croperViewGessture.delegate=self;
[croperView addGestureRecognizer:croperViewGessture];
-(void)CanvasScale:(id)sender
{
if([(UIPinchGestureRecognizer *)sender state]==UIGestureRecognizerStateBegan)
{
if ([sender numberOfTouches] == 2) {
_pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
_pntOrig[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];
} else {
_pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationInView:cropedAngle];
_pntOrig[1] = _pntOrig[0];
}
_lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x);
_lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y);
_xScale = 1.0;
_yScale = 1.0;
}
if ([(UIPinchGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged) {
if ([sender numberOfTouches] == 2) {
CGPoint pntNew[2];
pntNew[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
pntNew[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];
CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x);
CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y);
CGFloat dX = fabs(lenX - _lenOrigX);
CGFloat dY = fabs(lenY - _lenOrigY);
CGFloat tot = dX + dY;
CGFloat pX = dX / tot;
CGFloat pY = dY / tot;
CGFloat scale = [(UIPinchGestureRecognizer *)sender scale];
CGFloat dscale = scale - 1.0;
_xScale = dscale * pX + 1;
_yScale = dscale * pY + 1;
}
}
CGAffineTransform transform = cropedAngle.transform;
CGAffineTransform newTarnsform = CGAffineTransformScale(transform, _lenOrigX, _lenOrigY);
[cropedAngle setTransform:newTarnsform];
}
But problem is that when I do Zoomin OR Zoomout then view spread on all over the screen and after it disable Please view my code and tell me what is wrong .
Please help me in this issue
i am Thankfull in advance.
I wrote my own custom extension to UIPinchGestureRecognizer to provide an xScale and a yScale, in addition to the normal scale. This is a drop in replacement for UIPinchGestureRecognizer. You now have the option of looking at the normal scale or the new xScale and yScale. Use these values accordingly to scale your view based on how the user does the pinch gesture.
RMPinchGestureRecognizer.h
#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#interface RMPinchGestureRecognizer : UIPinchGestureRecognizer {
CGPoint _pntOrig[2];
CGFloat _lenOrigX;
CGFloat _lenOrigY;
CGFloat _xScale;
CGFloat _yScale;
}
#property (nonatomic, readonly) CGFloat xScale;
#property (nonatomic, readonly) CGFloat yScale;
#end
RMPinchGestureRecognizer.m
#import "RMPinchGestureRecognizer.h"
#implementation RMPinchGestureRecognizer
#synthesize xScale = _xScale;
#synthesize yScale = _yScale;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateChanged) {
if ([self numberOfTouches] == 2) {
CGPoint pntNew[2];
pntNew[0] = [self locationOfTouch:0 inView:self.view];
pntNew[1] = [self locationOfTouch:1 inView:self.view];
CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x);
CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y);
CGFloat dX = fabs(lenX - _lenOrigX);
CGFloat dY = fabs(lenY - _lenOrigY);
CGFloat tot = dX + dY;
CGFloat pX = dX / tot;
CGFloat pY = dY / tot;
CGFloat scale = [self scale];
CGFloat dscale = scale - 1.0;
_xScale = dscale * pX + 1;
_yScale = dscale * pY + 1;
}
}
}
- (void)setState:(UIGestureRecognizerState)state {
if (state == UIGestureRecognizerStateBegan) {
if ([self numberOfTouches] == 2) {
_pntOrig[0] = [self locationOfTouch:0 inView:self.view];
_pntOrig[1] = [self locationOfTouch:1 inView:self.view];
} else {
_pntOrig[0] = [self locationInView:self.view];
_pntOrig[1] = _pntOrig[0];
}
_lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x);
_lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y);
_xScale = 1.0;
_yScale = 1.0;
}
[super setState:state];
}
#end
Take a look HERE
Your line CGAffineTransformScale(transform, scaleTemp,scaleTemp); uses the same scaleTemp variable to modify both the x and y values of the transform.

iPhone trim audio recording

I have a voice memo component in my app, and I want to allow the user to trim the audio, similar to QuickTime X on Mac OS Ten point Six handles it, or like the Voice Memos app on the iPhone. Here's an example of both:
Any help is appreciated.
I am not a UI programmer by any means. This was a test I wrote to see how to write custom controls. This code may or may not work. I have not touched it in some time.
header
#interface SUIMaxSlider : UIControl {
#private
float_t minimumValue;
float_t maximumValue;
float_t value;
CGPoint trackPoint;
}
#property (nonatomic, assign) float_t minimumValue, maximumValue;
#property (nonatomic, assign) float_t value;
#end
implementation
#import "SUIMaxSlider.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>
//#import "Common.h"
#define kSliderPadding 5
#implementation SUIMaxSlider
#synthesize minimumValue, maximumValue;
#pragma mark -
#pragma mark Interface Initialization
- (id) initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
trackPoint.x = self.bounds.size.width;
self.backgroundColor = [UIColor colorWithRed:135.0/255.0 green:173.0/255.0 blue:255.0/255.0 alpha:0.0];
}
return self;
}
- (id) initWithFrame: (CGRect) aFrame {
if (self = [super initWithFrame:aFrame]) {
self.frame = aFrame;
self.bounds = aFrame;
self.center = CGPointMake(CGRectGetMidX(aFrame), CGRectGetMidY(aFrame));
trackPoint.x = aFrame.size.width;
}
return self;
}
- (id) init {
return [self initWithFrame:CGRectZero];
}
#pragma mark -
#pragma mark Properties.
#pragma mark -
- (float_t) value {
return value;
}
- (void) setValue:(float_t) v {
value = fmin(v, maximumValue);
value = fmax(value, minimumValue);
float_t delta = maximumValue - minimumValue;
float_t scalar = ((self.bounds.size.width - 2 * kSliderPadding) / delta) ;
float_t x = (value - minimumValue) * scalar;
x += 5.0;
trackPoint.x = x;
[self setNeedsDisplay];
}
#pragma mark -
#pragma mark Interface Drawing
#pragma mark -
- (void) drawRect:(CGRect) rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef lightBlue = [UIColor colorWithRed:135.0/255.0 green:173.0/255.0 blue:255.0/255.0 alpha:1.0].CGColor;
CGColorRef lightBlueAlpha = [UIColor colorWithRed:135.0/255.0 green:173.0/255.0 blue:255.0/255.0 alpha:0.7].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:130.0/255.0 green:130.0/255.0 blue:130.0/255.0 alpha:1.0].CGColor;
CGColorRef darkGrayColor = [UIColor colorWithRed:70.0/255.0 green:70.0/255.0 blue:70.0/255.0 alpha:1.0].CGColor;
CGColorRef redColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.6].CGColor;
CGRect boundsRect = self.bounds;
CGRect sliderRect = CGRectMake(0, 0, boundsRect.size.width, boundsRect.size.height / 2);
/*
CGContextSetFillColorWithColor(context, lightGrayColor);
CGContextFillRect(context, sliderRect);
halfHeight.origin.y = sliderRect.size.height;
CGContextSetFillColorWithColor(context, darkGrayColor);
CGContextFillRect(context, sliderRect);
*/
CGFloat tx = fmin(sliderRect.size.width - kSliderPadding, trackPoint.x);
tx = fmax(kSliderPadding, tx);
sliderRect.origin.y = boundsRect.origin.y;
sliderRect.size.width = tx ;
CGContextSetFillColorWithColor(context, lightBlueAlpha);
CGContextFillRect(context, sliderRect);
sliderRect.origin.y = sliderRect.size.height;
CGContextSetFillColorWithColor(context, lightBlue);
CGContextFillRect(context, sliderRect);
CGFloat mid = boundsRect.size.height / 2 ;
CGPoint a = CGPointMake(tx - kSliderPadding, mid);
CGPoint b = CGPointMake(tx, mid - kSliderPadding);
CGPoint c = CGPointMake(tx + kSliderPadding, mid);
CGPoint d = CGPointMake(tx, mid + kSliderPadding);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, a.x, a.y);
CGContextAddLineToPoint(context, b.x, b.y);
CGContextAddLineToPoint(context, c.x, c.y);
CGContextAddLineToPoint(context, d.x, d.y);
CGContextAddLineToPoint(context, a.x, a.y);
CGContextFillPath(context);
}
#pragma mark -
#pragma mark Touch Tracking
#pragma mark -
- (void) trackTouch:(UITouch *) touch {
CGPoint p = [touch locationInView:self];
//bound track point
trackPoint.x = fmax(p.x, kSliderPadding) ;
trackPoint.x = fmin(trackPoint.x, self.bounds.size.width - kSliderPadding);
[self setNeedsDisplay];
float_t x = trackPoint.x - kSliderPadding;
float_t delta = maximumValue - minimumValue;
float_t scalar = (x / (self.bounds.size.width - 2 * kSliderPadding)) ;
value = minimumValue + (delta * scalar);
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[self trackTouch:touch];
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint p = [touch locationInView:self];
trackPoint.x = fmax(p.x, kSliderPadding) ;
trackPoint.x = fmin(trackPoint.x, self.bounds.size.width - kSliderPadding);
[self setNeedsDisplay];
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[self trackTouch:touch];
return YES;
}
#end