我正在制作一个应用程序,我必须在其中控制像图形一样的面部微笑。例如,如果我向下滑动滑块(到一个小于中间的值),它应该给弧一个悲伤的脸(比如:-()效果,如果我向上滑动滑块,弧应该给出微笑的效果(比如:- ) ). 最初的嘴唇就像 :-| . 我需要使用滑块控制作为弧线的嘴唇吗?
微笑滑块视图控制器.h
#import <UIKit/UIKit.h>
@interface smileSliderViewController : UIViewController
{
IBOutlet UISlider *slider;
}
-(IBAction)valueChange:(id)sender;
@end
微笑滑块视图控制器.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);
}
}
微笑.h
#import <UIKit/UIKit.h>
#import "smile.m"
@interface smile : UIView
{
IBOutlet UISlider *slider;
}
@end
微笑.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);
}
}