0

我有一个覆盖 UIScrollView 的子类

touchesBegan:withEvent: touchesMoved:withEvent:
touchesEnded:withEvent:

覆盖这三个似乎是一种广泛使用的技术(基于我在论坛中的观察)。然而,一旦我在 OS3 上编译了这段代码,这些方法就不再被调用了。有没有其他人看到这个问题?是否有不使用未记录方法的已知修复程序?

我第一次尝试解决方案是将所有 touchesBegan/Moved/Ended 方法向下移动到我的内容视图中并设置

延迟内容触摸 = 否;canCancelContentTouches = 否;

这部分工作,但当我放大时让我无法平移。我的第二次尝试仅在有两次触摸时才设置 canCancelContentTouches = NO(从而将捏合手势传递给内容)。这种方法很粗略,效果不是很好。

有任何想法吗?我的要求是滚动视图必须处理平移触摸,我必须处理缩放触摸。

4

1 回答 1

1

我的解决方案并不漂亮。基本上有一个包含内容视图的滚动视图。滚动视图根本没有实现 touchesBegan、Moved、Ended。内容视图维护一个指向其父级的指针(在本例中称为“parentScrollView”)。内容视图处理逻辑并使用 [parentScrollView setCanCancelContentTouches:...] 来确定是否让父视图取消触摸事件(从而执行滚动事件)。点击计数逻辑之所以存在,是因为用户很少同时将两个手指放在屏幕上,所以如果第一次触摸很快就跟着第二次触摸,则必须忽略它。

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    if(parentViewIsUIScrollView)
    {
        UIScrollView * parentScrollView = (UIScrollView*)self.superview;
        if([touches count] == 1)
        {
            if([[touches anyObject] tapCount] == 1)
            {   
                if(numberOfTouches > 0)
                {
                    [parentScrollView setCanCancelContentTouches:NO];
                    //NSLog(@"cancel NO - touchesBegan - second touch");
                    numberOfTouches = 2;
                }
                else
                {
                    [parentScrollView setCanCancelContentTouches:YES];
                    //NSLog(@"cancel YES - touchesBegan - first touch");
                    numberOfTouches = 1;
                }   
            }
            else
            {
                numberOfTouches = 1;
                [parentScrollView setCanCancelContentTouches:NO];
                //NSLog(@"cancel NO - touchesBegan - doubletap");
            }
        }
        else
        {       
            [parentScrollView setCanCancelContentTouches:NO];
            //NSLog(@"cancel NO - touchesBegan");
            numberOfTouches = 2;
        }
        //NSLog(@"numberOfTouches_touchesBegan = %i",numberOfTouches);
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    if(touchesCrossed)
        return;

    if(parentViewIsUIScrollView)
    {
        UIScrollView * parentScrollView = (UIScrollView*)self.superview;
        NSArray * thoseTouches = [[event touchesForView:self] allObjects]; 

        if([thoseTouches count] != 2)
            return;
        numberOfTouches = 2;


        /* compute and perform pinch event */

        [self setNeedsDisplay];
        [parentScrollView setContentSize:self.frame.size];
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    touchesCrossed = NO;
    if(parentViewIsUIScrollView)
    {
        numberOfTouches = MAX(numberOfTouches-[touches count],0);
        [(UIScrollView*)self.superview setCanCancelContentTouches:YES];
        //NSLog(@"cancel YES - touchesEnded");
        //NSLog(@"numberOfTouches_touchesEnded = %i",numberOfTouches);  
    }
}
于 2009-09-23T18:29:23.477 回答