iOS開發---UIscrollView嵌套tableview的懸停效果實現方式


一直以來,蘋果的懸停效果,沒有太多的邏輯,所以直接使用UITableview的組頭懸停即可

但是最近的懸停效果比較繁瑣,如果繼續采用這方式,加上刷新的邏輯之后,或者有二級懸停之后,就不再好使了!

所以只能自己寫這種效果了

遇到的坑,一開始以為只要判斷懸停的位置,然后對兩個控件進行 滾動屬性的切換即可,但是發現有問題,到了臨界點,有一下卡頓,滾動停止,父視圖或者子視圖,並不能完美的跟上滾動!所以這種方案就夭折了!

新方案:依然采用監測臨界點的方法,但是這次通過設置兩個視圖的contenoffset屬性,其實兩個都在滾動,只是有一個一直在同一位置,視覺上就是一個在滾,一個等待了,到了臨界點,滾動狀態切換為相反,這樣就實現了!

同時還有一個關鍵點就是要設置一下滾動視圖的屬性!下邊直接上代碼!

#import <UIKit/UIKit.h>

@interface CourseTableView : UITableView<UIGestureRecognizerDelegate>

@end

 

#import "CourseTableView.h"

@implementation CourseTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

@end

 //該方法返回YES時,意味着所有相同類型的手勢都會得到處理。

 

下面是簡單的臨界點判斷邏輯

//父tableview
@property(nonatomic,assign)BOOL canScroll;
//子tableview
@property (nonatomic, assign) BOOL sonCanScroll;

 

#pragma mark UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat bottom = [_tableView rectForSection:0].size.height+MainScreenWidth*140/375.0;
   
    
    if (scrollView == _tableView) {//父視圖
        CGFloat scrollViewY = _tableView.contentOffset.y;
        if (scrollViewY>=bottom) {//當subcell還沒有滾動到
            _tableView.contentOffset = CGPointMake(0, bottom);
            if (self.canScroll) {
                self.canScroll = NO;
                self.sonCanScroll = YES;
                NSLog(@"父視圖懸停---111");
            }
        }else{
            if (!self.canScroll) {//子cell沒到頂
                if (_courseTableview.contentOffset.y == 0) {
                    self.canScroll = YES;
                    self.sonCanScroll = NO;
                    NSLog(@"父視圖動---2222");
                }else{
                    _tableView.contentOffset = CGPointMake(0, bottom);
                    NSLog(@"父視圖懸停---2222");
                }
               
            }
        }
        
        if (_tableView.contentOffset.y == bottom &&_courseTableview.contentOffset.y == 0) {
            self.canScroll = YES;
        }
        
    }
    //    NSLog(@"%lf",scrollView.contentOffset.y);
    if (scrollView == _courseTableview) {//子視圖
        CGFloat scrollViewY = _courseTableview.contentOffset.y;
        if (!self.sonCanScroll&&self.canScroll) {
            _courseTableview.contentOffset = CGPointZero;
            NSLog(@"子視圖懸停---111");
        }
        if (scrollViewY <= 0) {
            self.sonCanScroll = NO;
            _courseTableview.contentOffset = CGPointZero;
            self.canScroll = YES;
            NSLog(@"子視圖懸停---2222");
        }
        
        if (_tableView.contentOffset.y == bottom &&_courseTableview.contentOffset.y == 0) {
            self.canScroll = YES;
        }
        
    }
}

 byzqk


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM