簡體   English   中英

將UITableView和其他UIView嵌入受限制的UIScrollView中

[英]Embed UITableView and other UIViews inside a restricted UIScrollView

我有一個涉及多個手勢的復雜情況。 基本上,我希望有一個容器UIScrollView,如果觸摸在特定區域內,則只能從左向右滾動。 如果它們不在該區域之內,則UIScrollView會將這些觸摸傳遞到在UIScrollView內並排存在的子UIViews(將其視為面板導航)。

我有包含UIViews的UIScrollView正常工作。 我繼承了UIScrollView的子類,並通過TouchesBegan / TouchesMoved / TouchesEnded / TouchesCancelled添加了平移限制。 一切正常,除了UIView是UITableView時。 似乎在那時,我的父UIScrollView從未收到這些事件,因此也永遠無法正確限制平移。

有人對如何實現此目標有任何想法嗎?

謝謝!

執行此操作的方法是將正在吃觸摸事件的子視圖子類化,並且不允許UIScrollView獲取它們。 然后,重寫pointInside:方法(具有您要仍然使用的UI的適當例外)。 例如:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// Confine the offending control to a certain area
CGRect frame = CGRectMake(0, 0,
                          self.frame.size.width,
                          self.frame.size.height - 100.00);

// Except for subview buttons (or some other UI element)
if([self depthFirstButtonTest:self pointInside:point withEvent:event])
{
    return YES;
}

return (CGRectContainsPoint(frame, point));
}

- (BOOL)depthFirstButtonTest:(UIView*)view pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView * subview in view.subviews)
{
    if([self depthFirstButtonTest:subview pointInside:point withEvent:event])
    {
        return YES;
    }
}

// Is it a button? If so, perform normal testing on it
if ([view isKindOfClass:[UIButton class]]) {
    CGPoint pointInButton = [view convertPoint:point fromView:self];
    if ([view pointInside:pointInButton withEvent:event]) {
        return YES;
    }
}

return NO;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM