簡體   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