2013-01-17 53 views
0

我有一个自定义开关触摸事件

UICustomSwtichUISlider

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。必须调用三个触摸事件才能正确调用。

坏作品

这里是我的问题。我有一个静态UITableView从故事板设计的层次结构:

UITableView>UITableViewCell>UIView>UICustomSwitch

在这种情况下:

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

此功能properlly称为

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

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

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

当我将拇指向左和向右移动(上升条的末端)时,会调用两种方法(非常奇怪的行为,不是吗?)。

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

为什么触摸事件有时会达到我的UICustomSwitch,有时不会?以及为什么结束和跟踪的事件与触摸但不是开始?

在此先感谢。

回答

0

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

#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 cancell UITouch事件UICustomSwitch