欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ios scrollview嵌套tableview同向滑動(dòng)的示例

 更新時(shí)間:2017年11月08日 08:32:17   作者:清隱道人  
本篇文章主要介紹了ios scrollview嵌套tableview同向滑動(dòng)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

我討論的問(wèn)題是嵌套同向滑動(dòng),能避免盡量避免。最好用一個(gè)tableview實(shí)現(xiàn)。一個(gè)tableview不夠用了再嵌套,適用復(fù)雜場(chǎng)景。

首先我說(shuō)下不適用的,免得大家浪費(fèi)時(shí)間。

1.不適用上下拉刷新加載更多的頁(yè)面。

2.不適用點(diǎn)擊cell獲取點(diǎn)擊事件的頁(yè)面,可以加入button點(diǎn)擊獲取事件。

官方文檔說(shuō)盡量不要進(jìn)行兩個(gè)豎直或兩個(gè)水平方向滑動(dòng)的視圖嵌套。因?yàn)檫@個(gè)時(shí)候機(jī)器不知道用戶(hù)要讓哪個(gè)滑動(dòng),但在我們這個(gè)神奇的國(guó)度,項(xiàng)目中經(jīng)常出現(xiàn)這樣的需求,產(chǎn)品經(jīng)理總愛(ài)這樣做,andriod那邊是比較容易實(shí)現(xiàn)的,ios這邊十分復(fù)雜,我研究了一天,寫(xiě)了個(gè)demo,可以勉強(qiáng)實(shí)現(xiàn),我的項(xiàng)目中就有上下拉,因此我就硬嵌套了,用戶(hù)滑動(dòng)的時(shí)候不能準(zhǔn)確地按自己的意愿滑動(dòng)scrollview、tableview。就這樣了,這個(gè)沒(méi)有解決方案的。

我做到的效果是手點(diǎn)在哪個(gè)視圖上,哪個(gè)視圖就滾動(dòng),當(dāng)小的scroll滾到到自己的臨界值就滾動(dòng)大的scroll,當(dāng)大的也到臨界值就不滾動(dòng)。順便實(shí)現(xiàn)了一個(gè)偽懸浮的secView如果沒(méi)有那個(gè)懸浮的就把那個(gè)懸浮高度f(wàn)loatViewHeight置0。剩下的根據(jù)頁(yè)面調(diào)整frame即可通用。

這是效果圖

下面我說(shuō)一下在沒(méi)有以上兩點(diǎn)不適用的頁(yè)面的實(shí)現(xiàn)的思路:

Scrollview在控制器的view上,是一個(gè)大的視圖,tablewview在ScrollView上。根據(jù)拖拽手勢(shì)配合,先判斷首先觸摸點(diǎn)在哪個(gè)view上,再對(duì)哪個(gè)view滑動(dòng)操作。解決手勢(shì)和button點(diǎn)擊沖突。下面看代碼,注釋十分清晰。github有demo,歡迎閱讀: https://github.com/qingyindaoren/ScrollInsetTable.git

核心代碼如下:

#import "ViewController.h"
#import "YYGestureRecognizer.h"
#import "ScrollTableViewCell.h"
#import "MBProgressHUD+Add.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
//虛假的懸浮效果
static CGFloat floatViewHeight = 30.0;

static CGFloat navHeitht = 64;
// 這個(gè)系數(shù)根據(jù)自己喜好設(shè)置大小,=屏幕視圖滑動(dòng)距離/手指滑動(dòng)距離
#define moveScale 2


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic,weak)UIScrollView *scroll;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic,weak)UITableView *insetTableView;
@property (nonatomic,assign)CGFloat tableY;
@property (nonatomic,assign)CGFloat tableStartY;
@property (nonatomic,assign)CGFloat scrollY;
@property (nonatomic,assign)CGFloat scrollStartY;

//tableview 的y值 在scrollview中的位置
@property (nonatomic,assign)CGFloat tableFrameY;
@end

@implementation ViewController

- (void)viewDidLoad {

  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  self.view.backgroundColor = [UIColor whiteColor];
  self.title = @"ScrollScroll";
// 有導(dǎo)航最上部視圖是scrollview 內(nèi)部空間位置會(huì)下移,設(shè)置這個(gè)屬性后不下移。
  if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
    self.automaticallyAdjustsScrollViewInsets = NO;
  }

  UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,navHeitht, ScreenWidth, ScreenHeight-navHeitht)];
  scroll.backgroundColor = [UIColor colorWithRed:0.4 green:0.3 blue:0.2 alpha:1.0];;
  

  [self.view addSubview:scroll];
  self.scroll = scroll;
  
  
  //根據(jù)需求設(shè)置tableview的y值 暫寫(xiě)scroll高的2分之一
   self.tableFrameY = self.scroll.frame.size.height/2;
  
  UIImageView *headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, self.tableFrameY-floatViewHeight)];
  headImage.image = [UIImage imageNamed:@"scrollHead"];
  headImage.contentMode = UIViewContentModeScaleAspectFill;
  [self.scroll addSubview:headImage];
  
  NSArray *titles = @[@"ICO詳情",@"央行放大招",@"比特幣會(huì)漲",@"神秘中本村"];
  self.titles = titles;
   UISegmentedControl *segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(5, scroll.bounds.size.height/2-30, self.scroll.bounds.size.width - 10, 30)];
   [segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
  for (NSString *title in _titles) {
    [segment insertSegmentWithTitle:title atIndex:segment.numberOfSegments animated:false];
  }
  segment.selectedSegmentIndex = 0;
  [self.scroll addSubview:segment];
  
  UITableView *insetTable = [[UITableView alloc]initWithFrame:CGRectMake(0,self.tableFrameY, self.view.bounds.size.width, ScreenHeight-navHeitht-floatViewHeight)];
  insetTable.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
  
  insetTable.dataSource = self;
  insetTable.delegate = self;
  
 
  [self.scroll addSubview:insetTable];
  self.insetTableView = insetTable;
  
//github搜索 yykit 或yytext 里面有 yygestureRecognizer這個(gè)類(lèi),這個(gè)類(lèi)需要做一些修改,  // 在yygesture中所有觸摸事件方法里 加上super的方法,原文件里沒(méi)有,否則響應(yīng)鏈條中斷,scroll或tablew的按鈕點(diǎn)擊事件不執(zhí)行。
  //這個(gè)類(lèi)原文件繼承于UIGestureRecognizer, 改為繼承于UIPanGestureRecognizer 否則點(diǎn)擊事件不執(zhí)行。
  //運(yùn)行效果詳見(jiàn)我的demo

  YYGestureRecognizer *yyges = [YYGestureRecognizer new];
  yyges.action = ^(YYGestureRecognizer *gesture, YYGestureRecognizerState state){
    if (state != YYGestureRecognizerStateMoved) return ;
    
    if (CGRectContainsPoint(self.insetTableView.frame, gesture.startPoint)) {
     
     //滑動(dòng)tableview
      [self tableScrollWithGesture:gesture];
      
      
 
    }else{
      
      //滑動(dòng)scrollview
      [self scrollScrollWithGesture:gesture];
      
    }
 
  };
  //必須給scroll 加上手勢(shì) 不要給view加,不然滑動(dòng)tablew的時(shí)候會(huì)錯(cuò)誤判斷去滑動(dòng)scroll。
  [self.scroll addGestureRecognizer:yyges];
  
  //實(shí)現(xiàn)手勢(shì)代理,解決交互沖突
  yyges.delegate = self;
   scroll.contentSize = CGSizeMake(self.view.bounds.size.width, self.tableFrameY+self.insetTableView.frame.size.height);

}
//解決手勢(shì)按鈕沖突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
  //如果是 segment或scroll上的其他按鈕,取消手勢(shì)
  if([NSStringFromClass(touch.view.superclass) isEqualToString:@"UIControl"]){
    return NO;
  }


  //
    return YES;
    }
//
- (void)segmentValueChanged:(UISegmentedControl *)segment {
//scroll 到底部
  CGFloat offset = self.scroll.contentSize.height - self.insetTableView.bounds.size.height-floatViewHeight;
  if (offset > 0)
  {
    self.scrollY = offset;
    [self.scroll setContentOffset:CGPointMake(0, offset) animated:YES];
  }
  //TableView到頂部
  self.tableY = 0;
  [self.insetTableView setContentOffset:CGPointMake(0, self.tableY) animated:YES];
}
- (void)tableScrollWithGesture:(YYGestureRecognizer *)gesture{
  CGFloat scrolly;
  
  if (self.tableStartY != gesture.startPoint.y) {
    scrolly = -(gesture.currentPoint.y-gesture.startPoint.y) ;
  }else{
    scrolly = -(gesture.currentPoint.y-gesture.lastPoint.y) ;
  }
  self.tableStartY = gesture.startPoint.y;
  
  self.tableY += scrolly*moveScale;
  
  //為了顯示底部超出屏幕的tableview那部分 滑動(dòng)scrollview 此時(shí)tablewview已經(jīng)滑動(dòng)到了底部
  if (self.tableY> self.insetTableView.contentSize.height-self.insetTableView.bounds.size.height){
    self.scrollY += self.tableY-(self.insetTableView.contentSize.height-self.insetTableView.bounds.size.height);
    
    //tablewview滑動(dòng)到底部就不要滑了
    self.tableY = self.insetTableView.contentSize.height-self.insetTableView.bounds.size.height;
    
  //scrollview 滑動(dòng)到了底部就不要滑動(dòng)了
    if (self.scrollY> self.scroll.contentSize.height-self.insetTableView.bounds.size.height-floatViewHeight){
      self.scrollY = self.scroll.contentSize.height-self.insetTableView.bounds.size.height-floatViewHeight;
      //如果scrollview意外的contentsize 小于自己的大小,scrollview就不要滑了
      if (self.scrollY<0) {
        self.scrollY = 0;
      }
      
    }
    [self.scroll setContentOffset:CGPointMake(0, self.scrollY) animated:YES];
    
    //如果tablewview的cell過(guò)少或行高過(guò)少致使其contentsize 小于自己的大小,tableview就不要滑了
    if (self.tableY<0) {
      self.tableY = 0;
    }
    
  }
  
  
  //如果滑到了tableview的最上部,停止滑動(dòng)tablewview, 如果此時(shí)scrollview 沒(méi)有在最上部就滑動(dòng)scrollview到最上部
  if (self.tableY<0){
    self.scrollY += self.tableY;
    
    //scroll已經(jīng)在最上部了,scroll就不滑了
    if (self.scrollY<0) {
      self.scrollY = 0;
    }
    
    NSLog(@"scroll %lf",self.scrollY);
    [self.scroll setContentOffset:CGPointMake(0, self.scrollY) animated:YES];
    
     //停止滑動(dòng)tablewview
    self.tableY = 0;
    
  }
  NSLog(@"table %lf",self.tableY);
  
  
  [self.insetTableView setContentOffset:CGPointMake(0, self.tableY) animated:YES];
}
- (void)scrollScrollWithGesture:(YYGestureRecognizer *)gesture{
  CGFloat scrolly;
  
  if (self.scrollStartY != gesture.startPoint.y) {
    scrolly = -(gesture.currentPoint.y-gesture.startPoint.y) ;
  }else{
    scrolly = -(gesture.currentPoint.y-gesture.lastPoint.y) ;
  }
  self.scrollStartY = gesture.startPoint.y;
  
  self.scrollY += scrolly*moveScale;
  
  //如果滑到了scroll的底部就不要滑了
  if (self.scrollY> self.scroll.contentSize.height-self.insetTableView.bounds.size.height-floatViewHeight){
    self.scrollY = self.scroll.contentSize.height-self.insetTableView.bounds.size.height-floatViewHeight;
    //如果scrollview意外的contentsize 小于自己的大小,scrollview就不要滑了
    if (self.scrollY<0) {
      self.scrollY = 0;
    }
  }
  //如果滑到了scroll頂部就不要滑了
  if (self.scrollY<0){
    self.scrollY = 0;
  }
  NSLog(@"scroll %lf",self.scrollY);
  
  
  [self.scroll setContentOffset:CGPointMake(0, self.scrollY) animated:YES];
  
}


#pragma mark - 展示tableview的代理

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  ScrollTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ScrollTableViewCell"];
  if (!cell)
  {
    [tableView registerNib:[UINib nibWithNibName:@"ScrollTableViewCell" bundle:nil] forCellReuseIdentifier:@"ScrollTableViewCell"];
    cell = [tableView dequeueReusableCellWithIdentifier:@"ScrollTableViewCell"];
  }
  
    cell.backgroundColor = [UIColor clearColor];
   
 
  
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  cell.Titletext.text = [NSString stringWithFormat:@"\t第%zd行",indexPath.row];
  cell.detailText.text = @"滑屏呀滑屏呀劃呀";
  cell.detailText.textColor = self.navigationController.navigationBar.tintColor;
  cell.indexPath = indexPath;
  
  cell.selectCellBlock = ^(NSIndexPath *indexPath) {
    NSString *tip = [NSString stringWithFormat:@"點(diǎn)擊了第%ld組%ld行",indexPath.section,indexPath.row];;
    [MBProgressHUD showMessage:tip view:nil];
    
    NSLog(@"%@",tip);

  };
  
  return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  
  return 3;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
  v.backgroundColor = [UIColor orangeColor];
  UILabel *l = [[UILabel alloc]initWithFrame:v.bounds];
  l.text =[NSString stringWithFormat:@"tableview的組頭%ld",section];
  l.textColor = [UIColor whiteColor];
  l.textAlignment = NSTextAlignmentCenter;
  [v addSubview:l];
  return v;
}
//組頭高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  
  return 50;
  
  
}
//這個(gè)方法不可用了,除非點(diǎn)擊了cellcontenview之外的區(qū)域 只能同過(guò)加按鈕的方式接受點(diǎn)擊事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  NSLog(@"點(diǎn)擊了第%ld行",indexPath.row);
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS實(shí)現(xiàn)富文本編輯器的方法詳解

    iOS實(shí)現(xiàn)富文本編輯器的方法詳解

    大家在開(kāi)發(fā)的時(shí)候經(jīng)常會(huì)用到富文本編輯器,所以這篇文章就給大家整理了如何使用iOS實(shí)現(xiàn)富文本編輯器的方法,相信本文對(duì)大家具有一定的參考借鑒價(jià)值,有需要的朋友們可以一起來(lái)看看。
    2016-10-10
  • 總結(jié)IOS中隱藏軟鍵盤(pán)的三種方式

    總結(jié)IOS中隱藏軟鍵盤(pán)的三種方式

    在IOS開(kāi)發(fā)中,軟鍵盤(pán)是開(kāi)發(fā)者們經(jīng)常需要打交道的地方,下面為大家?guī)?lái)我整理總結(jié)的三種隱藏鍵盤(pán)的方法。有需要的可以參考借鑒。
    2016-08-08
  • iOS App通信之local socket示例

    iOS App通信之local socket示例

    這篇文章主要介紹了iOS App之間的通信 -local socket示例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS開(kāi)發(fā)中TabBar再次點(diǎn)擊實(shí)現(xiàn)刷新效果

    iOS開(kāi)發(fā)中TabBar再次點(diǎn)擊實(shí)現(xiàn)刷新效果

    這篇文章主要介紹了iOS開(kāi)發(fā)中TabBar再次點(diǎn)擊實(shí)現(xiàn)刷新效果,實(shí)現(xiàn)方法也很簡(jiǎn)單,需要的朋友可以參考下
    2018-04-04
  • swift 常用高階函數(shù)分享

    swift 常用高階函數(shù)分享

    Swift是一門(mén)面向協(xié)議的語(yǔ)言,在使用Swift時(shí)我們已經(jīng)充分享受到了面向協(xié)議編程帶給我們的便利,但是Swift相比Obj-C還有一個(gè)更重要的優(yōu)點(diǎn),那就是對(duì)函數(shù)式編程提供了很好的支持,其中Swift提供了map,filter,reduce這三個(gè)高階函數(shù)Higher Order function作為對(duì)容器的支持
    2017-12-12
  • iOS實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

    iOS實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié)

    iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié)

    Xcode環(huán)境中包含CoreMotion框架,能夠幫助我們調(diào)用硬件設(shè)備的加速度傳感器和陀螺儀等感應(yīng)器,下面比較詳細(xì)地整理了iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié),需要的朋友可以參考下:
    2016-07-07
  • ios實(shí)現(xiàn)簡(jiǎn)易隊(duì)列

    ios實(shí)現(xiàn)簡(jiǎn)易隊(duì)列

    這篇文章主要為大家詳細(xì)介紹了ios實(shí)現(xiàn)簡(jiǎn)易隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • ios app重提提交審核流程

    ios app重提提交審核流程

    本篇文章給大家講述了在APP第一次沒(méi)有審核通過(guò)后,重新提交的流程和注意的地方,學(xué)習(xí)一下吧。
    2017-12-12
  • iOS開(kāi)發(fā)實(shí)現(xiàn)下載器的基本功能(1)

    iOS開(kāi)發(fā)實(shí)現(xiàn)下載器的基本功能(1)

    這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)下載器基本功能的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評(píng)論