简体   繁体   中英

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

-(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). I need help if any one help me Thanks in advance I google on this but didn't get any help

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). 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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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