如何在ios中轻敲内圆时检测面积
我想在圆圈内敲击时找到该区域。事实上,我做过一次计算,但不准确 检查我编写的代码片段,以便在我的循环视图中找到选项卡的位置如何在ios中轻敲内圆时检测面积,ios,atan2,Ios,Atan2,我想在圆圈内敲击时找到该区域。事实上,我做过一次计算,但不准确 检查我编写的代码片段,以便在我的循环视图中找到选项卡的位置 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event" this method float dx = touchLocation.x -160; float dy = touchLocation.y - 240; double angle = atan2(touc
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event" this method
float dx = touchLocation.x -160;
float dy = touchLocation.y - 240;
double angle = atan2(touchLocation.x,touchLocation.y);
/* Device is iPad */
if (count==4) {
if(angle>-1.639&&angle<=0.775)
{
area=1;
NSLog(@"touched here 1 ********************************** ");
}
else if(angle>0.775&&angle<=1.579)
{
area=2;
NSLog(@"touched here 2********************************** ");
}
else if(angle>1.579&&angle<=2.466)
{
area=3;NSLog(@"touched here 3********************************** ");
}
else
{
area=4;NSLog(@"touched here 4 ********************************** ");
}
}
else if (count==5) {
if(angle>-1.520&&angle<=0.553)
{
area=1;
NSLog(@"touched here 1 ********************************** ");
}
else if(angle>0.553&&angle<=1.262)
{
area=2;
NSLog(@"touched here 2********************************** ");
}
else if(angle>1.262&&angle<=1.884)
{
area=3;NSLog(@"touched here 3********************************** ");
}
else if(angle>1.884&&angle<=2.644)
{
area=4;NSLog(@"touched here 4 ********************************** ");
}
else
{
area=5;NSLog(@"touched here 5 ********************************** ");
}
}
else if (count==6) {
if(angle>-1.5707&&angle<=0.4692)
{
area=1;
NSLog(@"touched here 1 ********************************** ");
}
else if(angle>0.4692&&angle<=1.0219)
{
area=2;
NSLog(@"touched here 2********************************** ");
}
else if(angle>1.0219&&angle<=1.5707)
{
area=3;NSLog(@"touched here 3********************************** ");
}
else if(angle>1.5707&&angle<=2.1147)
{
area=4;NSLog(@"touched here 4 ********************************** ");
}
else if(angle>2.1147&&angle<=2.7245)
{
area=5;NSLog(@"touched here 5 ********************************** ");
}
else
{
area=6;NSLog(@"touched here 6 ********************************** ");
}
}
else if (count==7) {
if(angle>-1.5707&&angle<=0.3992)
{
area=1;
NSLog(@"touched here 1 ********************************** ");
}
else if(angle>0.3992&&angle<=0.8602)
{
area=2;
NSLog(@"touched here 2********************************** ");
}
else if(angle>0.8602&&angle<=1.346)
{
area=3;NSLog(@"touched here 3********************************** ");
}
else if(angle>1.346&&angle<=1.812)
{
area=4;NSLog(@"touched here 4 ********************************** ");
}
else if(angle>1.812&&angle<=2.304)
{
area=5;NSLog(@"touched here 5 ********************************** ");
}
else if(angle>2.304&&angle<=2.828)
{
area=6;NSLog(@"touched here 6 ********************************** ");
}
else
{
area=7;NSLog(@"touched here 7 ********************************** ");
}
}
else if (count==8){
if(angle>-1.40&&angle<=0.45)
{
area=1;
NSLog(@"touched here 1 ********************************** ");
}
else if(angle>.45&&angle<=.73)
{
area=2;
NSLog(@"touched here 2********************************** ");
}
else if(angle>.73&&angle<=1.15)
{
area=3;NSLog(@"touched here 3********************************** ");
}
else if(angle>1.15&&angle<=1.55){
area=4;NSLog(@"touched here 4 ********************************** ");
}
else if(angle>-1.55&&angle<=1.95){
area=5;NSLog(@"touched here 5 ********************************** ");
}
else if(angle>-1.95&&angle<=2.43){
area=6;NSLog(@"touched here 6 ********************************** ");
}
else if(angle>2.43&&angle<=2.98){
area=7;NSLog(@"touched here 7********************************** ");
}
// else if(angle>2.98&&angle<=-1.40){
else
{
area=8;NSLog(@"touched here 8 ********************************** ");
}
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event“此方法
浮动dx=触摸位置x-160;
float dy=touchLocation.y-240;
双角度=atan2(touchLocation.x,touchLocation.y);
/*设备是iPad*/
如果(计数=4){
如果1.579和0.0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.553和0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3和0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0eUIView
是cycleView。那么这可能是您的解决方案:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:cycleView];
if(touchLocation.y < cycleView.frame.size.height/2 && touchLocation.x < cycleView.frame.size.width/2){
//touch is in upper left corner
} else if(touchLocation.y < cycleView.frame.size.height/2 && touchLocation.x >= cycleView.frame.size.width/2){
//touch is in upper right corner
} else if(touchLocation.y >= cycleView.frame.size.height/2 && touchLocation.x < cycleView.frame.size.width/2){
//touch is in lower left corner
} else if(touchLocation.y >= cycleView.frame.size.height/2 && touchLocation.x >= cycleView.frame.size.width/2){
//touch is in upper left corner
}
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[[event AllTouchs]anyObject];
CGPoint touchLocation=[触摸位置查看:循环查看];
if(touchLocation.y=cycleView.frame.size.width/2){
//触摸屏位于右上角
}else if(touchLocation.y>=cycleView.frame.size.height/2&&touchLocation.x=cycleView.frame.size.height/2&&touchLocation.x>=cycleView.frame.size.width/2){
//触摸屏位于左上角
}
}
如果每个饼图都是一个单独的UIView
,那么就更容易了。这样,您可以分别调用每个饼图。假设您的周期UIView
是cycleView。那么这可能是您的解决方案:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:cycleView];
if(touchLocation.y < cycleView.frame.size.height/2 && touchLocation.x < cycleView.frame.size.width/2){
//touch is in upper left corner
} else if(touchLocation.y < cycleView.frame.size.height/2 && touchLocation.x >= cycleView.frame.size.width/2){
//touch is in upper right corner
} else if(touchLocation.y >= cycleView.frame.size.height/2 && touchLocation.x < cycleView.frame.size.width/2){
//touch is in lower left corner
} else if(touchLocation.y >= cycleView.frame.size.height/2 && touchLocation.x >= cycleView.frame.size.width/2){
//touch is in upper left corner
}
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[[event AllTouchs]anyObject];
CGPoint touchLocation=[触摸位置查看:循环查看];
if(touchLocation.y=cycleView.frame.size.width/2){
//触摸屏位于右上角
}else if(touchLocation.y>=cycleView.frame.size.height/2&&touchLocation.x=cycleView.frame.size.height/2&&touchLocation.x>=cycleView.frame.size.width/2){
//触摸屏位于左上角
}
}
如果每个饼片都是一个单独的UIView
,那么就更容易了。你可以这样称呼每个饼片。以下是一些想法,Kevin的答案很好,但没有概括出更多的饼片
假设在一个圆上有n个相等的切片。每个切片2*PI/n度。要找到相应的切片,你必须从三角学得到一些帮助,特别是我们将使用弧切线
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:cycleView];
double radius = self.bounds.size.width;
double sliceAngle = M_PI*2 / n;
double deltaAngle = atan2(radius - touchLocation.y,touchLocation.x - radius);
//Here you might get a negative value for delta angle,
//just keep adding 2*M_PI to the result until you get a deltaAngle between 0 and 2*M_PI
//also notice that we are normalizing the aran values for the center of our frame, which is
//the origin of the circle (keep in mind that origin is the upper left corner in UIKit)
NSInteger sliceNumber = ((NSInteger)floor(deltaAngle/sliceAngle))
}
另外,您还可以做一件额外的事情,因为您有一个圆,但UIView的应该是矩形,您可以覆盖pointInside方法以仅捕获圆内的接触。它如下所示:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//Assuming you have a circle, not an ellipse. And also that your frame is a square.
//Get the length of the touch point from the origin and make sure it's smaller than
//your circles radius
CGFloat length = sqrtf(point.x * point.x + point.y * point.y);
return (length < self.bounds.size.width);
//could have used height as well since you have a square
}
-(BOOL)点内部:(CGPoint)点与事件:(UIEvent*)事件{
//假设你有一个圆,而不是椭圆,并且你的框架是正方形。
//从原点获取接触点的长度,并确保其小于
//你的圆半径
CGFloat长度=sqrtf(点x*点x+点y*点y);
返回值(长度
这些片段没有经过测试,如果它们没有按预期工作,请告诉我。以下是一些想法,Kevin的回答很好,但没有概括更多片段
假设在一个圆上有n个相等的切片。每个切片2*PI/n度。要找到相应的切片,你必须从三角学得到一些帮助,特别是我们将使用弧切线
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:cycleView];
double radius = self.bounds.size.width;
double sliceAngle = M_PI*2 / n;
double deltaAngle = atan2(radius - touchLocation.y,touchLocation.x - radius);
//Here you might get a negative value for delta angle,
//just keep adding 2*M_PI to the result until you get a deltaAngle between 0 and 2*M_PI
//also notice that we are normalizing the aran values for the center of our frame, which is
//the origin of the circle (keep in mind that origin is the upper left corner in UIKit)
NSInteger sliceNumber = ((NSInteger)floor(deltaAngle/sliceAngle))
}
另外,您还可以做一件额外的事情,因为您有一个圆,但UIView的应该是矩形,您可以覆盖pointInside方法以仅捕获圆内的接触。它如下所示:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//Assuming you have a circle, not an ellipse. And also that your frame is a square.
//Get the length of the touch point from the origin and make sure it's smaller than
//your circles radius
CGFloat length = sqrtf(point.x * point.x + point.y * point.y);
return (length < self.bounds.size.width);
//could have used height as well since you have a square
}
-(BOOL)点内部:(CGPoint)点与事件:(UIEvent*)事件{
//假设你有一个圆,而不是椭圆,并且你的框架是正方形。
//从原点获取接触点的长度,并确保其小于
//你的圆半径
CGFloat长度=sqrtf(点x*点x+点y*点y);
返回值(长度
这些代码片段没有经过测试,如果它们没有按预期工作,请告诉我。更新:添加了完整的工作代码。
下面是一些伪代码,可以帮助您确定点击的位置。这是指最上面的图形。(我假设您的圆的中心与视图的中心重合):
(1) 找出线段从圆心到接触点的方向:
dx = touchPoint.x - circleCenter.x;
dy = touchPoint.y - circleCenter.y;
t = atan2(dy, dx); // some offsetting/direction adjustment might be required
(2) 找出接触点所在的八分之一
octant = floor(4 * t/M_PI); // will return a number between 0 and 7.
如果扇区大小不均匀(但知道每个扇区的角度大小),可以使用If-else序列
(3) 右侧的八分之一都有一个“内扇形”和一个环形。如果要测试触摸发生在两个部分中的哪一个,可以首先计算触摸点与圆心的距离:
dist = sqrtf(dx * dx + dy * dy);
显然,你需要知道每个八分之一的内径,然后进行测试
if ( dist < innerRadius[i]) ... // again, you might need to adjust the angle calculations to ensure that the right indices correspond to the right sector. See (working) code below...
更新:添加完整的工作代码
这是一些假鳕鱼