How to set parameters of CGContextAddArcToPoint method using a slider
Posted
by
Manish Sahni
on Stack Overflow
See other posts from Stack Overflow
or by Manish Sahni
Published on 2012-06-16T05:41:49Z
Indexed on
2012/06/16
9:16 UTC
Read the original article
Hit count: 367
am making an app in which i have to control a smile of a face like graphics. For example if i slide the slider down(to a value less than middle) it should give the arc a sad face(like :-( ) effect and if i slide the slider up the arc should give the effect of smile(like :-) ).initially lips are like :-| . I need to control the lips which is an arc using a slider?
smileSliderViewController.h
#import <UIKit/UIKit.h>
@interface smileSliderViewController : UIViewController
{
IBOutlet UISlider *slider;
}
-(IBAction)valueChange:(id)sender;
@end
smileSliderViewController.m
#import "smileSliderViewController.h"
//#import "smile.h"
@implementation smileSliderViewController
-(IBAction)valueChange:(id)sender
{
int value = (int)(slider.value);
if(value>0 && value<25)
{
//value--;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0,0.0,1.0,1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 120,180);
CGContextAddArcToPoint(context, 190 , 170, 270, 200, 0 );
CGContextStrokePath(context);
}
}
smile.h
#import <UIKit/UIKit.h>
#import "smile.m"
@interface smile : UIView
{
IBOutlet UISlider *slider;
}
@end
smile.m
#import "smile.h"
#import "smileSliderViewController.h"
@implementation smile
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
//CGContextSetStrokeColor(context, [UIColor redColor].CGColor);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0,0.0,1.0,1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGRect rectangle = CGRectMake(60, 20, 200,200);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
/*CGRect rectangle1 = CGRectMake(130,170,50,10);
CGContextAddRect(context,rectangle1);
CGContextStrokePath(context);*/
/*CGContextMoveToPoint(context,100,200);
CGContextAddArcToPoint(context,120,150,400,150,70 );
CGContextStrokePath(context);*/
/*CGContextMoveToPoint(context, 100,100);
CGContextAddArcToPoint(context, -100,200, -400,200, 80);
CGContextStrokePath(context);*/
CGContextSetStrokeColorWithColor(context,color);
CGRect rectangle2 = CGRectMake(80, 90, 50, 8);
CGContextAddEllipseInRect(context, rectangle2);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context,color);
CGRect rectangle3 = CGRectMake(180, 90, 50, 8);
CGContextAddEllipseInRect(context, rectangle3);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context,color);
CGContextMoveToPoint(context, 155,120);
CGContextAddLineToPoint(context, 155,160);
CGContextStrokePath(context);
/*CGContextSetStrokeColorWithColor(context, color);
CGRect rectangle4 = CGRectMake(130, 180, 50,8);
CGContextAddEllipseInRect(context, rectangle4);
CGContextStrokePath(context);*/
/*CGContextSetStrokeColorWithColor(context,color);
CGContextMoveToPoint(context, 120,180);
CGContextAddLineToPoint(context, 190,180);
CGContextStrokePath(context);*/
int value = (int)(slider.value);
if(value == 25)
{
CGContextMoveToPoint(context, 120,180);
CGContextAddArcToPoint(context, 190 , 180, 250 , 180, 0 );
CGContextStrokePath(context);
}
}
© Stack Overflow or respective owner