簡體   English   中英

用於水平和垂直滾動的UIScrollView策略

[英]UIScrollView strategies for horizontal and vertical scrolling

情況:我的視圖頂部有各種各樣的工具欄(不是UIToolbar,而是自定義UIView)。 該工具欄有許多按鈕。 在它的下面,我有一個帶有表單的滾動視圖。 當點擊工具欄上的按鈕時,相應的表格應根據點擊哪個按鈕從右或左顯示在屏幕上。 我不希望工具欄繼續前進。 但是,我確實希望用戶在滾動視圖上向上或向下滾動時,工具欄也可以向上滾動。 我現在完成此操作的方法是在scrollViewDidScroll委托方法中(請注意,如果僅通過按工具欄上的按鈕進行,則我僅允許水平滾動):

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.contentOffset.x != 0 && _shouldScrollHorizontally == NO)
    {
        CGPoint offset = scrollView.contentOffset;
        offset.x = 0;
        scrollView.contentOffset = offset;
    }

    CGRect buttonFrame = [_buttons frame];

    buttonFrame.origin.y = -scrollView.contentOffset.y + 10;

    [_buttons setFrame:buttonFrame];
}

但是,我擔心這效率低下並會導致一些延遲。 我想知道是否有更好的方法來實現我要實現的目標。

歸根結底,如果滾動視圖水平滾動,我希望工具欄保持靜止,但是如果滾動視圖垂直滾動,則希望工具欄與滾動視圖一起移動。

感謝您的任何建議!

由於僅在輕按按鈕時才允許水平“滾動”,因此請適當地將UIScrollView contentSize width設置為屏幕的寬度及其高度。 要在按鈕點擊上水平“滾動”,只需為視圖水平移動設置動畫。

視圖層次結構類似於外部outerView ,它是將UIScrollView contentSize設置為當前的寬度和高度。 滾動視圖是outerView的子視圖,工具欄是滾動視圖的子視圖。

要設置水平運動的動畫,通常最簡單的方法是更改outerView.centertoolbarView.center ,例如:

 CGPoint newCenterPoint; CGPoint newToolbarCenterPoint; CGPoint centerPoint1 = // set center as appropriate CGPoint centerPoint2 = // set center as appropriate CGPoint centerPoint3 = // set center as appropriate CGPoint toolbarCenter1 = // set center as appropriate CGPoint toolbarCenter2 = // set center as appropriate CGPoint toolbarCenter3 = // set center as appropriate if (buttonTapped == button1) { newCenterPoint = centerPoint1; newToolbarCenterPoint = toolbarCenter1; } else if (buttonTapped == button2){ newCenterPoint = centerPoint2; newToolbarCenterPoint = toolbarCenter2; } else if (buttonTapped == button3){ newCenterPoint = centerPoint3; newToolbarCenterPoint = toolbarCenter3; } [UIView animateWithDuration:0.25 animations:^{ outerView.center = newCenterPoint; toolbarView.center = newToolbarCenterPoint; }]; 

暫無
暫無

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

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