iPhone OS3更改为UIScrollView子类

时间:2009-06-23 18:36:09

标签: iphone-sdk-3.0 uiscrollview

我有一个UIScrollView的子类覆盖

的touchesBegan:withEvent:方法 touchesMoved:withEvent:方法
touchesEnded:withEvent:方法

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

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

delaysContentTouches = NO; canCancelContentTouches = NO;

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

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

1 个答案:

答案 0 :(得分: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);  
    }
}