
想要使用触摸事件填充所有不同颜色的空白区域
现在我可以填充从拾取器中拾取颜色的圆圈但是如何用不同的颜色填充镶边部分......
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UIColor *cl=[UIColor clearColor];
UITouch *tuch=[touches anyObject];
if ([clr isEqualToString:@"Red"]) {
cl=[UIColor redColor];
}
else if ([clr isEqualToString:@"Blue"]) {
cl=[UIColor blueColor] ;
}
else if ([clr isEqualToString:@"Green"]) {
cl=[UIColor greenColor];
}
CGPoint p = [tuch locationInView:self];
float xsq1=p.x -50;
xsq1=xsq1*xsq1;
float ysq1=p.y-110;
ysq1=ysq1*ysq1;
float h1 = ABS(sqrt(xsq1 + ysq1));
float xsq2=p.x -100;
xsq2=xsq2*xsq2;
float ysq2=p.y-110;
ysq2=ysq2*ysq2;
float h2 = ABS(sqrt(xsq2 + ysq2));
float xsq3=p.x -50;
xsq3=xsq3*xsq3;
float ysq3=p.y-190;
ysq3=ysq3*ysq3;
float h3 = ABS(sqrt(xsq3 + ysq3));
if (h1<=40) {
NSLog(@"touches inside of first circle");
CGContextSetFillColorWithColor(context, cl.CGColor);
CGRect cir1 = CGRectMake(10,266,80,80);
CGContextFillEllipseInRect(context, cir1);
[self setNeedsDisplayInRect:cir1];
}
else if (h2<=40) {
NSLog(@"touches inside of second circle");
CGContextSetFillColorWithColor(context, cl.CGColor);
CGRect cir2 = CGRectMake(60,266,80,80);
CGContextFillEllipseInRect(context, cir2);
[self setNeedsDisplayInRect:cir2];
}
}
Run Code Online (Sandbox Code Playgroud)
这里有两个任务,第一个是检测哪个区域被触摸,第二个是填充该区域。两者都要求您使用三角学计算上图的圆交点,并知道它们的位置。
触摸区域检测的一个简单解决方案是检查触摸是否包含在任何圆中,这可以通过计算从圆中心到触摸点的距离(如果小于其内部半径)来轻松计算圆圈。如果它位于多个圆内,您就知道它属于该交叉区域。如果它不在任何圆内,但 x 分量位于左圆和右圆的中心之间,则它必须位于所有圆之间的区域中。否则,触摸点必须位于所有圆圈之外。
为了填充上图的各个部分,您可以创建包含需要填充的区域的路径并用CGContextFillPath. 像这样的东西:
// draw a path to contain the fill region
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, startx, starty);
CGContextAddArcToPoint(ctx, ...);
// lots of other CGContextAddArcToPoint or AddLineToPoint method calls here to define the clip region
// close the clip path
CGContextClosePath(ctx);
// now you can fill the region
CGContextFillPath(ctx);
Run Code Online (Sandbox Code Playgroud)
您可以对任意多条路径重复此操作。您可以根据圆交点和半径计算要使用的路径弧。
| 归档时间: |
|
| 查看次数: |
613 次 |
| 最近记录: |