【问题标题】:iPhone OS3 changes to UIScrollView subclassesiPhone OS3 更改为 UIScrollView 子类
【发布时间】:2009-06-23 18:36:09
【问题描述】:

我有一个覆盖 UIScrollView 的子类

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

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

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

delaysContentTouches = 否; canCancelContentTouches = NO;

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

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

【问题讨论】:

    标签: iphone-sdk-3.0 uiscrollview


    【解决方案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);  
        }
    }
    

    【讨论】:

    • 这太棒了!处理这种情况的一种非常优雅的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    相关资源
    最近更新 更多