Question

I have a custom switch

UICustomSwtich: UISlider

downloaded from here

How it works

UICustomSwitch overwrite this touch events to work as it have to work:

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

Good tests

UICustomSwitch works perfectly in any UIView, even a UIScrollView. The three touches events are called properly when it have to be called.

Bad works

Here's my problem. I have a static UITableView designed from StoryBoard with this hierarchy:

UITableView> UITableViewCell> UIView > UICustomSwitch

In this scenario:

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

This function is called properlly

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

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

When I move the thumb less than the length of the bar, the thumb remains at the same point I left it (the problem).

When I move the thumb to left and right (rises the end of the bar) the two methods are called (very strange behavior, isn't it?).

I looked for the answer finding similar problems with different solutions, but any good for my problem.

Why touch events sometimes reaches my UICustomSwitch and sometimes not? And why the events of ended and trackingWithTouch but not the began?

Thanks in advance.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top