iPhone trim audio recording - cocoa-touch

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

Related

Custom Slider In SpriteKit

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

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

get CGRect location in superview objective-c

I want to print out the x coordinate of a CGRect. The x,y coordinates of the rect is set to where the user touches, like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
startPoint = [touch locationInView:self];
}
- (void)drawRect:(CGRect)rect
{
ctx = UIGraphicsGetCurrentContext();
jrect = CGRectMake(startPoint.x, startPoint.y, 100, 100);
CGContextAddRect(ctx, jrect);
CGContextFillPath(ctx);
}
I could just print out the startPoint but if I would print out the CGRect's coordinate I tried to do this:
int jrectX = lroundf(CGRectGetMinX(jrect));
xlabel.text = [NSString stringWithFormat:#"x: %i", jrectX];
But the number it returns doesn't make any sense at all, sometimes they are bigger to the left than to the right. Is there anything wrong with the code?
A CGRect is a struct with four CGFloat properties: x, y, width, height
To print the x value from a CGRect:
[NSString stringWithFormat:#"%f", rect.x]
To print the entire rect, there's a convenience function:
NSStringFromCGRect(rect)
You're having problems above, because you're storing the x value into an int, and then using a float rounding function on it. So it should be:
CGFloat jrectX = CGRectGetMinX(jrect);
. . . unless you're doing rotation transformation, you can just use:
CGFloat jrectX = jrect.origin.x;
DR.h
#import <UIKit/UIKit.h>
#interface DR : UIView
{
CGContextRef ctx;
CGRect jrect;
CGPoint startPoint;
UILabel *xlabel;
}
#end
DR.m file.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
xlabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self addSubview:xlabel];
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
ctx = UIGraphicsGetCurrentContext();
jrect = CGRectMake(startPoint.x, startPoint.y, 100, 100);
CGContextAddRect(ctx, jrect);
CGContextFillPath(ctx);
// Drawing code
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
startPoint = [touch locationInView:self];
int jrectX = lroundf(CGRectGetMinX(jrect));
NSLog(#"jrectX --------------");
xlabel.text = [NSString stringWithFormat:#"x: %i", jrectX];
[self setNeedsDisplay];
}
#end
In other viewController use it...
- (void)viewDidLoad
{
DR *drr=[[DR alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[drr setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:drr];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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.

Making a circular NSSlide look and behave like in GarageBand

I am new to drawing with Cocoa, and I am making some software which will have sliders similar to these found in GarageBand:
GB Sliders http://img33.imageshack.us/img33/2668/schermafbeelding2010061r.png
These look beautiful and can be controld by moving the mouse up and down.
Can you help me with customizing NSSliders by subclassing them, so I can make them look and behave exactly as in GarageBand? Thanks.
I have one image for the knob which should be rotated as they do not need to be in 3D .
The simplest way is to create a NSView subclass that handles both the mouse management and the drawing.
There is a sample code that can help you to start named "TLayer". It is part of the Examples of the XCode 3.1.4. It contains a circular custom view that controls the offset and the radius of the shadow drawn for layers. It is easy to understand and easy to extend.
Note: as it does not seems to be available on the Apple website, so I have pasted the sources below.
ShadowOffsetView.h
#import <AppKit/AppKit.h>
extern NSString *ShadowOffsetChanged;
#interface ShadowOffsetView : NSView
{
CGSize _offset;
float _scale;
}
- (float)scale;
- (void)setScale:(float)scale;
- (CGSize)offset;
- (void)setOffset:(CGSize)offset;
#end
ShadowOffsetView.m
#import "ShadowOffsetView.h"
NSString *ShadowOffsetChanged = #"ShadowOffsetChanged";
#interface ShadowOffsetView (Internal)
- (NSCell *)cell;
#end
#implementation ShadowOffsetView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self == nil)
return nil;
_offset = CGSizeZero;
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (float)scale
{
return _scale;
}
- (void)setScale:(float)scale
{
_scale = scale;
}
- (CGSize)offset
{
return CGSizeMake(_offset.width * _scale, _offset.height * _scale);
}
- (void)setOffset:(CGSize)offset
{
offset = CGSizeMake(offset.width / _scale, offset.height / _scale);
if (!CGSizeEqualToSize(_offset, offset)) {
_offset = offset;
[self setNeedsDisplay:YES];
}
}
- (BOOL)isOpaque
{
return NO;
}
- (void)setOffsetFromPoint:(NSPoint)point
{
float radius;
CGSize offset;
NSRect bounds;
bounds = [self bounds];
offset.width = (point.x - NSMidX(bounds)) / (NSWidth(bounds) / 2);
offset.height = (point.y - NSMidY(bounds)) / (NSHeight(bounds) / 2);
radius = sqrt(offset.width * offset.width + offset.height * offset.height);
if (radius > 1) {
offset.width /= radius;
offset.height /= radius;
}
if (!CGSizeEqualToSize(_offset, offset)) {
_offset = offset;
[self setNeedsDisplay:YES];
[(NSNotificationCenter *)[NSNotificationCenter defaultCenter]
postNotificationName:ShadowOffsetChanged object:self];
}
}
- (void)mouseDown:(NSEvent *)event
{
NSPoint point;
point = [self convertPoint:[event locationInWindow] fromView:nil];
[self setOffsetFromPoint:point];
}
- (void)mouseDragged:(NSEvent *)event
{
NSPoint point;
point = [self convertPoint:[event locationInWindow] fromView:nil];
[self setOffsetFromPoint:point];
}
- (void)drawRect:(NSRect)rect
{
NSRect bounds;
CGContextRef context;
float x, y, w, h, r;
bounds = [self bounds];
x = NSMinX(bounds);
y = NSMinY(bounds);
w = NSWidth(bounds);
h = NSHeight(bounds);
r = MIN(w / 2, h / 2);
context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextTranslateCTM(context, x + w/2, y + h/2);
CGContextAddArc(context, 0, 0, r, 0, 2*M_PI, true);
CGContextClip(context);
CGContextSetGrayFillColor(context, 0.910, 1);
CGContextFillRect(context, CGRectMake(-w/2, -h/2, w, h));
CGContextAddArc(context, 0, 0, r, 0, 2*M_PI, true);
CGContextSetGrayStrokeColor(context, 0.616, 1);
CGContextStrokePath(context);
CGContextAddArc(context, 0, -2, r, 0, 2*M_PI, true);
CGContextSetGrayStrokeColor(context, 0.784, 1);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, r * _offset.width, r * _offset.height);
CGContextSetLineWidth(context, 2);
CGContextSetGrayStrokeColor(context, 0.33, 1);
CGContextStrokePath(context);
}
#end
Well, for the actual drawing you'd either have to have images for each rotation angle of the knob (easier to implement) and then just draw the proper one.
(While for a real realistic 3d look—even if possible—programmatic drawing wouldn't be worth its time, I guess.)
Or draw the knob by code. This article should give you an idea I think:
http://katidev.com/blog/2008/03/07/how-to-create-a-custom-control-with-nsview/
(For both, the mouse event handling and basic NSBezerPath drawing of circular and rotating knob-like elements)