![](/img/trans.png)
[英]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.