2013-01-17 57 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