9

所以我有一个 UIView 的子类,它假设可以检测触摸。仅当触摸在当前视图内开始时,视图才会检测触摸。当触摸在视图之外开始并且它们在我的自定义视图中移动时,touchesMoved 不会被调用。有什么解决方案可以检测当前视图中尚未开始的移动触摸?

@implementation MycustomView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   // This only gets called if touches have started in the current View
} 

@end
4

4 回答 4

20

以下解决方案有效。我有多个 MyCustomView 实例;随着触摸的移动,我想检测正在触摸的视图

我最终将触摸检测从 MyCustomView 移至其 superView,因此以下代码不再位于 MyCustomView 类中:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    for (UIView *view in self.contentView.subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {

        }
    }
}
于 2012-04-01T23:20:05.467 回答
1

这应该解决它:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    for (UIView* subView in self.subviews) 
    {
        if([subView pointInside:[self convertPoint:touch toView:subView] withEvent:event])
        {
            //do your code here
        }
    }
}
于 2012-04-01T23:02:08.650 回答
0

一种方法(尽管可能还有其他方法)是禁用子视图的用户交互并使其父视图跟踪移动(使用该hitTest方法来确定触摸当前结束的视图)。

于 2012-04-01T23:20:29.110 回答
0

尝试这个....

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for(UITouch *touch in touches)
    {
        CGPoint touchPointFirstBtn = [touch locationInView:self.ChordView];
        if(CGRectContainsPoint(_btnC.frame, touchPointFirstBtn))
        {
            if (!_btnC.isHighlighted)
            {
                if(!Boolean)
                {
                    title = @"C";
                    [_tlbView reloadData];
                    NSLog(@"%@",@"touches C");

                }
                [_btnC setHighlighted:YES];
                Boolean = YES;

            }
        }
        else
        {
            [_btnC setHighlighted:NO];
            Boolean = NO;
        }
}
于 2017-03-31T07:09:32.760 回答