简体   繁体   English

使用UITapGestureRecognizer根据iPhone中的手指触摸位置添加新视图

[英]adding the new view according to the finger touch location in iPhone using UITapGestureRecognizer

To all I am using the 对于我正在使用的所有

 UITapGestureRecognizer*singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap:)] autorelease];
    singleTap.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:singleTap

for getting the touch 获得联系

And in doSingleTap: method i have this 在doSingleTap:方法中我有这个

-(void)doSingleTap:(UITapGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationOfTouch:0 inView:self.view];
    NSLog(@"location X =>%f,location  =>%f ",point.x,point.y);
    CGPoint point1 = [recognizer locationOfTouch:1 inView:self.view];
    NSLog(@"location X =>%f,location  =>%f ",point1.x,point1.y);

     NSLog(@"location X =>%f,location  =>%f ",x,y);
    UIView *test1 = [[UIView alloc]initWithFrame:CGRectMake(point.x, point1.y, x, y)];
    test1.backgroundColor= [UIColor greenColor];
    [self.view addSubview:test1];


}

getting the problem in adding the new view on the main view according to the finger location or position View is properly getting add on the view according to the position . 在根据手指位置或位置在主视图上添加新视图时出现问题。视图正在根据位置正确在视图上添加。

I want that view get add according to the two finger and automatically adjust their (x,y,w,h). 我希望该视图根据两根手指添加,并自动调整其(x,y,w,h)。 I need help if any one help me Thanks in advance I google on this but didn't get any help 如果有人帮助我,我需要帮助预先感谢我在Google上进行了搜索,但没有得到任何帮助

Compare with point.x and point1.x take lesser 1 as x and do same for y (compare with point.y and point1.y smaller 1 as y). 与point.x和point1.x进行比较,将x减去1,对y进行相同处理(将point.y和point1.y的较小值与y进行比较)。 take the difference between point.x and point1.x as width and do same for height (difference between point.y and point1.y) will sove the issue 将point.x和point1.x之间的差异作为宽度,对高度进行相同(point.y和point1.y之间的差异)将解决问题

float x = (point.x < point1.x) ? point.x : point1.x;
float y = (point.y < point1.y) ? point.y : point1.y;
float width = fabsf(point.x - point1.x);
float height = fabsf(point.y - point1.y);
UIView *test1 = [[UIView alloc]initWithFrame:CGRectMake(x, y,width,height)];

Try to calculate rect of view with the next: 尝试使用下一个计算视图矩形:

float x = MIN(point.x, point1.x);
float y = MIN(point.y, point1.y);
float width = fabsf(point.x - point1.x);
float height = fabsf(point.y - point1.y);
CGRect frame = CGRectMake(x, y, width, height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM