避免 UIView 触摸延迟

发布于 2024-10-31 01:12:12 字数 1570 浏览 5 评论 0原文

我在我的应用程序中使用 UIView 的子类。当用户触摸视图时,我需要获取该点的坐标;为此我正在使用:

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

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

}

它有一些延迟。谁能给我一个立即获得积分的解决方案吗?谢谢大家。

- (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;
    }

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2024-11-07 01:12:12

如果您使用的是 UIScrollView,则有一个延迟触摸属性 (delaysContentTouches)。您可能想确保答案是否定的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文