簡體   English   中英

通過UIScrollView將某些觸摸傳遞給基礎視圖

[英]Pass certain touches through UIScrollView to underlying views

我有一個有兩個子視圖的視圖:

  1. 子視圖A,UIView(包含其他視圖,包含UIButtons,帶有手勢識別器的視圖......)
  2. 子視圖B,UIScrollView(包含一些視圖,但具有透明區域)。

滾動視圖位於子視圖A的頂部,並具有完整的設備寬度/高度。 我希望用戶能夠通過透明區域與滾動視圖下方的所有按鈕和手勢重新配置器進行交互,同時仍然能夠滾動(因此傳遞最小的測試結果)。

看起來像一個足夠簡單的任務,但我無法讓它工作。 滾動視圖始終阻止所有觸摸。

知道如何實現這一目標嗎? 謝謝!

您應該繼承UIScrollView並覆蓋以下方法:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

如果此方法返回NO,則對於觸摸事件,scrollview將是“透明的”。

如果您希望滾動視圖對於觸摸事件是“透明的”,僅當觸摸位於滾動視圖的透明區域時,您的實現應如下所示:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
     return ![self isPointInsideATransparentRegion:point]; //you need to implement isPointInsideATransparentRegion to check whether the point touched is in a transparent region or not
}

我現在已經解決了這個問題,方法是將滾動視圖下方的視圖添加到scrollview作為第一個子視圖,並在scrollViewDidScroll中移動它的位置:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self updateBottomViewPosition];
}

- (void)updateBottomViewPosition {
    CGPoint contentOffset = _mainScrollView.contentOffset;

    CGFloat y = MAX(contentOffset.y, 0);

    CGRect frame = _bottomPage.frame;
    if (y != frame.origin.y) {
        frame.origin.y = y;
        _leadPage.frame = frame;
    }
}

這有效,但它可能不盡如人意。

根據René的答案,如果背景視圖固定(視圖頂部為fe)並且底部滾動有彈跳,則此方法有效。

#pragma mark - Scroll delegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self updateBottomViewPosition];
}

- (void)updateBottomViewPosition {
    CGPoint contentOffset = self.scrollView.contentOffset;

    CGFloat y = MAX(contentOffset.y, 0);


    CGRect frame = self.fixedView.frame;
    if (y > 0) {
        frame.origin.y = y;
    } else {
        frame.origin.y = 0;
    }
    self.fixedView.frame = frame;
}

您可以使用它來創建一個漂亮的paralax效果,如果您不希望它被修復,您應該將y值更改為您想要的過渡運動。

暫無
暫無

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

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