簡體   English   中英

UIScrollView - 在反彈動畫啟動時設置負內容偏移

[英]UIScrollView - set negative contentOffset when bounce animation starts

我使用的是UITableView時候我的拔下來實現(像Spotify應用)的比例/ unblur在我的用戶的個人資料畫面效果UITableView為負contentOffset.y 這一切都很好......

現在,當用戶下拉到一個小於或等於某個值的contentOffset.y調用它的keepOffsetY maintainOffsetY = 70.0 - 並且他“放開”視圖,我想保持這個contentOffset直到用戶“推”再次查看,而不是視圖再次自動彈回到contentOffset = (0,0)

為了實現這一點,我必須知道觸摸何時開始和結束,我可以使用以下委托方法:

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // is like touches begin
    if ([scrollView isEqual:_tableView]) {
        NSLog(@"touches began");
        _touchesEnded = NO;
    }
}
- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
// is like touches ended
NSLog(@"touches ended");
if ([scrollView isEqual:_tableView]) {
    _touchesEnded = YES;
}

}

然后進去

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

我檢查contentOffset.y是否小於或等於maintainOffsetY ,如果用戶已經“放開”該表,那么它即將反彈(或已經反彈)回到contentOffset = (0,0)

似乎不可能只讓它反彈回maintainOffsetY 有人知道解決方法或對如何解決這個問題有所了解嗎?

任何幫助深表感謝!

要為滾動視圖設置負偏移,可以使用此選項。

您需要保存滾動視圖的初始內容插入。 如果您有半透明的導航欄,它可能不是0。

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];

        contentInsetTopInitial = self.tableView.contentInset.top;
    }

然后寫這個。

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    CGPoint contentOffset = scrollView.contentOffset; // need to save contentOffset before changing contentInset
    CGFloat contentOffsetY = contentOffset.y + contentInsetTopInitial; // calculate pure offset, without inset

    if (contentOffsetY < -maintainOffsetY) {
        scrollView.contentInset = UIEdgeInsetsMake(contentInsetTopInitial + maintainOffsetY, 0, maintainOffsetY, 0);
        [scrollView setContentOffset:contentOffset animated:NO];
    }
}

希望這會有所幫助。

暫無
暫無

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

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