tutsorg
tutsorg

Reputation: 21

Apply Touches to Custom Control

I made a Custom Circle Control, Were i am Sending values by Slider from my RootView Controller. Can You help me out, how to Apply touches to Circle Control were i can change the values, like making custom slider around my Custom Circle.

#import "RKCustomCircle.h"

@implementation RKCustomCircle
@synthesize sliderPerccentageValue;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        xPos = 320/2;
        yPos = 250;
        radius = 80;
        rotationAngle = 0;
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawCircleChart: context];

}


- (void) drawCircleChart:(CGContextRef) context
{
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 320, 480));
    float a = rotationAngle;
    [self drawCirclewithStartingAngle:a withContext:context];    
}


- (void) drawCirclewithStartingAngle:(float)startAngle withContext:(CGContextRef) context
{
    float endAngle = startAngle + (sliderPerccentageValue/ 100) * (M_PI*2);
    float adjY = yPos;
    float rad = radius;
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, xPos, adjY);
    CGContextAddArc(context, xPos, adjY, rad, startAngle, endAngle, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
}

RootView is,

@implementation RKViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    circleView = [[RKCustomCircle alloc] initWithFrame:CGRectMake(0, 100, 320, 360)];

    [circleView addTarget:self action:@selector(newValue:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:circleView];


}



- (IBAction)sliderValueChangedInPercentage:(UISlider *)sender {

    circleView.sliderPerccentageValue =sender.value;

    [circleView setNeedsDisplay];


}

Upvotes: 0

Views: 59

Answers (3)

Khanh Nguyen
Khanh Nguyen

Reputation: 11132

You need to override the pointInside:withEvent: method of the circle view. Refer to this for more information.

Upvotes: 0

iphonic
iphonic

Reputation: 12717

All your things are correct, but only the following part is incorrect.

- (IBAction)sliderValueChangedInPercentage:(UISlider *)sender {

    circleView.sliderPerccentageValue =sender.value;

    [circleView setNeedsDisplay];


}

The above code doesn't supply any value because you have attached to your control that implements UIControl, not UISlider. You don't have to either, your UIControl will work..

Instead you can have touch events inside your control itself no need to add any action from outside, and you have to detect circular touch and keep track of values, see this app here it will help you to get values when touched circular.

Hope it will help you. All the best.

Upvotes: 0

iPatel
iPatel

Reputation: 47099

Use UITapGestureRecognizer.

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [circleView addGestureRecognizer:singleTap];

Add this method

- (void)oneTap:(UIGestureRecognizer *)gesture 
{
    NSLog(@"Touch occur");

}

Upvotes: 1

Related Questions