首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将触摸应用于自定义控件

将触摸应用于自定义控件
EN

Stack Overflow用户
提问于 2013-08-12 16:14:32
回答 3查看 54关注 0票数 0

我做了一个自定义的圆形控件,因为我从我的RootView控制器通过滑块发送值。你能帮我吗?如果我可以改变值,如何将触摸应用到Circle控件,就像在我的custom Circle周围制作自定义滑块一样。

代码语言:javascript
运行
复制
#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是,

代码语言:javascript
运行
复制
@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];


}
EN

Stack Overflow用户

发布于 2013-08-12 16:24:35

使用UITapGestureRecognizer

代码语言:javascript
运行
复制
 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [circleView addGestureRecognizer:singleTap];

添加此方法

代码语言:javascript
运行
复制
- (void)oneTap:(UIGestureRecognizer *)gesture 
{
    NSLog(@"Touch occur");

}
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18182253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档