I obtained a solution implementing all (less one commented) touch and track events:
#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];
}
}
When the control is out of a table works with normal methods. When is inside a table, sometimes exit by cancelled methods and sometimes by ended methods. In any case, I obtained the correct behavior.
But there is a question unresolved.. Why UITableView
or UITableViewCell
cancell UITouch
events of UICustomSwitch
?