【问题标题】:Touch events in a subview of a UITableViewCell of a static UITableView静态 UITableView 的 UITableViewCell 子视图中的触摸事件
【发布时间】:2013-01-17 13:47:22
【问题描述】:

我有一个自定义开关

UICustomSwtich:UISlider

here下载

工作原理

UICustomSwitch 覆盖此触摸事件以使其正常工作:

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event 
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

良好的测试

UICustomSwitch 在任何UIView 中都能完美运行,甚至是UIScrollView。三个 touches 事件在需要调用的时候会正确调用。

坏作品

这是我的问题。我有一个从 StoryBoard 设计的静态UITableView,具有这种层次结构:

UITableView>UITableViewCell>UIView>UICustomSwitch

在这种情况下:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

这个函数被正确调用

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

当我移动拇指小于条的长度时,拇指保持在我离开它的同一点(问题)。

当我左右移动拇指(上升到小节的末端)时,这两种方法被调用(非常奇怪的行为,不是吗?)。

我寻找答案,发现不同解决方案的类似问题,但对我的问题有任何好处。

为什么触摸事件有时会到达我的UICustomSwitch,有时却没有?以及为什么结束和 trackingWithTouch 事件而不是开始事件?

提前致谢。

【问题讨论】:

    标签: ios cocoa-touch uitableview uitouch uicontrol


    【解决方案1】:

    我获得了一个解决方案,实现了所有(较少评论的)触摸和跟踪事件:

    #pragma - touches
    
    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        [super touchesBegan:touches withEvent:event];
    
        LOG(@"touches began");
    
        m_touchedSelf = NO;
        on = !on;
    }
    
    //- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    //{
    //    [super touchesMoved:touches withEvent:event];
    //    
    //    LOG(@"touches moved");
    //    
    //}
    
    - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
    {
        [super touchesEnded:touches withEvent:event];
    
        LOG(@"touches ended");
    
        if (!m_touchedSelf)
        {
            [self setOn:on animated:YES];
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
    }
    
    
    - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesCancelled:touches withEvent:event];
    
        LOG(@"touches cancelled");
    
        CGPoint location = [[touches anyObject] locationInView:self];
        if (location.x > self.frame.size.width/2) {
            [self setOn:YES animated:YES];
        }else{
            [self setOn:NO animated:YES];
        }
    
    }
    
    #pragma - trackings
    
    - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    {
    
        [super endTrackingWithTouch:touch withEvent:event];
    
        LOG(@"endTrackingWithTouch");
    
        m_touchedSelf = YES;
    
        if (self.value > 0.5) {
            [self setOn:YES animated:YES];
        }else{
            [self setOn:NO animated:YES];
        }
    }
    
    - (void) cancelTrackingWithEvent:(UIEvent *)event
    {
    
        LOG(@"cancelTrackingWithEvent");
    
        m_touchedSelf = YES;
    
        if (self.value > 0.5) {
            [self setOn:YES animated:YES];
        }else{
            [self setOn:NO animated:YES];
        }
    }
    

    当控件不在表中时,可以使用普通方法。当在表内时,有时通过取消的方法退出,有时通过结束的方法退出。无论如何,我得到了正确的行为。

    但是有一个问题没有解决.. 为什么UITableViewUITableViewCell 取消UITouch UICustomSwitch 的事件?

    【讨论】:

      猜你喜欢
      • 2012-06-29
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多