Search code examples
objective-ccocoa-touchiosuiviewuitouch

Avoid UIView touch delay


I am using a subclass of UIView in my app. When the user touches on view I need to get the coordinates of the point; for this I am using:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    currentPoint=[[touches anyObject]locationInView:self];

}

It has some delay. Can any one give me the solution to get the points immediately? Thank you all.

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        self.frame=frame;
        rect=[[NSMutableArray alloc] initWithCapacity:1];
        currentPointsList=[[NSMutableArray alloc]initWithCapacity:1];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];

    firstTouchedPoint = [touch locationInView:self];
    NSLog(@"the firstTouched point is %");
    for (int i=0; i<[rect count]; i++) {
    if(CGRectContainsPoint([[rect objectAtIndex:i]CGRectValue], firstTouchedPoint)){

        CGRect firstLineRect = CGRectMake(firstTouchedPoint.x,  
                                   [[rect objectAtIndex:i]CGRectValue].origin.y, 
                                   [[rect objectAtIndex:i]CGRectValue].size.width-firstTouchedPoint.x+[[rect objectAtIndex:i]CGRectValue].origin.x, 
                                   [[rect objectAtIndex:i]CGRectValue].size.height);
        UIImageView *firstLine=[[UIImageView alloc]initWithFrame:firstLineRect];
        firstLine.backgroundColor=[[UIColor blueColor]colorWithAlphaComponent:0.3f];
        [self addSubview:firstLine];
        break;
    }

}

Solution

  • If you are using a UIScrollView there is a delay touches property (delaysContentTouches). You may want to make sure that is NO.