[英]Touch events of Scrollview in subview of UITableviewCell in iOS
[英]Touch events in a subview of a UITableViewCell of a static UITableView
我有一个自定义开关
UICustomSwtich
: UISlider
从这里下载
这个怎么运作
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
。 当必须调用三个触摸事件时,将正确调用它。
不良作品
这是我的问题。 我有一个从StoryBoard设计的静态UITableView
,具有以下层次结构:
UITableView
> UITableViewCell
> UIView
> UICustomSwitch
在这种情况下:
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
适当地调用此功能
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
当我将拇指移动到小于条形的长度以下时,拇指保持在离开它的位置(问题)。
当我左右移动拇指(抬起条的末端)时,会调用两个方法(非常奇怪的行为,不是吗?)。
我寻找答案,发现具有不同解决方案的类似问题,但对我的问题有任何好处。
为什么触摸事件有时到达我的UICustomSwitch
而有时到达我? 以及为什么结束事件和trackWithTouch事件而不是开始事件?
提前致谢。
我获得了一个解决方案,该解决方案实现了所有(较少评论)触摸和跟踪事件:
#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];
}
}
当控件不在表中时,可以使用常规方法。 当在表中时,有时通过取消的方法退出,有时通过结束的方法退出。 无论如何,我都获得了正确的行为。
但是有一个问题没有得到解决。为什么UITableView
或UITableViewCell
cancell UITouch
事件UICustomSwitch
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.