繁体   English   中英

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

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

对于我正在使用的所有

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

获得联系

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


}

在根据手指位置或位置在主视图上添加新视图时出现问题。视图正在根据位置正确在视图上添加。

我希望该视图根据两根手指添加,并自动调整其(x,y,w,h)。 如果有人帮助我,我需要帮助预先感谢我在Google上进行了搜索,但没有得到任何帮助

与point.x和point1.x进行比较,将x减去1,对y进行相同处理(将point.y和point1.y的较小值与y进行比较)。 将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)];

尝试使用下一个计算视图矩形:

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