简体   繁体   English

静态UITableView的UITableViewCell的子视图中的触摸事件

[英]Touch events in a subview of a UITableViewCell of a static UITableView

I have a custom switch 我有一个自定义开关

UICustomSwtich : UISlider UICustomSwtichUISlider

downloaded from here 这里下载

How it works 这个怎么运作

UICustomSwitch overwrite this touch events to work as it have to work: UICustomSwitch覆盖此触摸事件以使其正常工作:

- (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 . UICustomSwitch可以完美地在任何UIView ,甚至可以在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: 我有一个从StoryBoard设计的静态UITableView ,具有以下层次结构:

UITableView > UITableViewCell > UIView > UICustomSwitch 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? 为什么触摸事件有时到达我的UICustomSwitch而有时到达我? And why the events of ended and trackingWithTouch but not the began? 以及为什么结束事件和trackWithTouch事件而不是开始事件?

Thanks in advance. 提前致谢。

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 ? 但是有一个问题没有得到解决。为什么UITableViewUITableViewCell cancell UITouch事件UICustomSwitch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM