1

大家好,我在 UIView 上使用 10 个弧(片或片)的 CGContext 绘制饼图。

但是当我触摸这个饼图的第一条弧线和最后一条弧线时,我没有从它们那里得到触摸点......这个饼图的所有弧线都支持触摸事件......

为什么会发生这种情况我被困住了...请帮助我...摆脱这个...

这是我的代码

-(void) drawRect:(CGRect)rect
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(ctx, 2.0);

float mult = 360/total;
// Draw each pie slice   
float curValue = 0.0f;  

for(int i=0; i< 10; i++)
    {   
    int startAngle = (int)(curValue * mult);  
    int arcAngle = (int)  ([[sorted objectAtIndex:i] floatValue]* mult);   

    // Ensure that rounding errors do not leave a gap between the first and last slice   
    if ( count == [sorted count] ) { arcAngle = 360 - startAngle; }   

    CGContextBeginPath(ctx);  
    CGContextMoveToPoint(ctx, x, y );
    CGContextAddArc(ctx, x, y, r, startAngle * M_PI/180.0, (startAngle * M_PI/180.0) + (arcAngle * M_PI/180), 0);

    CGContextClosePath(ctx);  

    CGContextSetStrokeColorWithColor(ctx, [[UIColor lightGrayColor] CGColor]); 
    CGContextDrawPath(ctx, kCGPathFillStroke );  curValue += [[sorted objectAtIndex:i] floatValue];

    }  
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
double startRotationValue = [[[self.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] doubleValue];
rotation.fromValue = [NSNumber numberWithDouble:startRotationValue];
rotation.toValue = [NSNumber numberWithDouble:startRotationValue+5];
rotation.duration = 2.0;
[self.layer addAnimation:rotation forKey:@"rotating"];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}

提前致谢.......

4

0 回答 0