【问题标题】:Apply Touches to Custom Control将触摸应用到自定义控件
【发布时间】:2013-08-12 08:14:32
【问题描述】:

我制作了一个自定义圆形控件,我是否正在通过我的 RootView 控制器中的滑块发送值。你能帮帮我吗,如果我可以更改值,如何将触摸应用到圆形控件,例如在我的自定义圆形周围制作自定义滑块。

#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 是,

@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];


}

【问题讨论】:

  • RKCustomCircle的Class类型是什么,是UISlider

标签: ios cocoa-touch custom-controls uitouch


【解决方案1】:

使用UITapGestureRecognizer

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

添加这个方法

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

}

【讨论】:

  • 我只想为圆形应用触摸,而不是整个视图。
【解决方案2】:

你所有的东西都是正确的,但只有以下部分不正确。

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

    circleView.sliderPerccentageValue =sender.value;

    [circleView setNeedsDisplay];


}

上面的代码没有提供任何值,因为您附加了实现 UIControl 的控件,而不是 UISlider。你也不必,你的 UIControl 会工作..

相反,您可以在控件内部设置触摸事件,而无需从外部添加任何操作,并且您必须检测圆形触摸并跟踪值,请参阅此应用 here 它将帮助您在触摸时获取值循环。

希望对您有所帮助。一切顺利。

【讨论】:

    【解决方案3】:

    您需要覆盖圆形视图的pointInside:withEvent: 方法。更多信息请参考this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-01-23
      • 2018-02-19
      相关资源
      最近更新 更多