【问题标题】:Avoid UIView touch delay避免 UIView 触摸延迟
【发布时间】:2011-04-08 09:13:56
【问题描述】:

我在我的应用程序中使用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;
    }

}

【问题讨论】:

  • 你能发布子类的触摸方法吗?
  • 我在那个班级写了 touchesBegan touchesMoved 和 Touchesended。
  • 在没有足够数据的情况下很难判断延迟的来源。

标签: objective-c cocoa-touch ios uiview uitouch


【解决方案1】:

如果您使用的是 UIScrollView,则有一个延迟触摸属性 (delaysContentTouches)。您可能需要确保这不是。

【讨论】:

  • 我正在将我的视图添加到滚动视图中是否有任何问题。在将我的视图作为子视图添加到滚动视图之前我做了 self.scrollview.delaysContentTouches=NO...但它也是不工作
  • +1 再次感谢您的回答确实用于我的工作。