Segment淺析(附scrollView與tableView手勢沖突處理)

涉及到Segment的時候就不自覺地找個第三方解決一下,前期還覺得沾沾自喜,任務解決了,當更深入的了解一下就懵逼了,啥都不會,最近覺得Segment用的比較頻繁,而第三方寫的很是復雜,所以就自己寫了兩個簡單的:
1.具有手勢切換能力(scrollView代理方法里面將滑動手勢和標題按鈕點擊關聯(lián))
2.沒有手勢切換能力
下面是代碼時間
快創(chuàng)建創(chuàng)建一個控制器以及四個子控制器試試吧


創(chuàng)建控制器
#import <UIKit/UIKit.h>

@interface GestureCommoneViewController : UIViewController

@end

//
//  GestureCommoneViewController.m
//  SGSegmentedControlExample
//
//  Created by APEW on 2017/9/8.
//  Copyright ? 2017年 Jason. All rights reserved.
//

#import "GestureCommoneViewController.h"
//下面的四個控制器只做了背景色處理
#import "TestOneVC.h"
#import "TestTwoVC.h"
#import "TestThreeVC.h"
#import "TestFourVC.h"
//UIView的category 涉及frame必備,沒有加類似于mas_這樣的前綴,最討厭這種工具類加前綴的,好像不知道他寫的似的
#import "UIView+Extension.h"
//下面都是些快捷寫法,一般在PCH文件中
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define VHeight SCREEN_HEIGHT - 64
#define NavH 64
#define TabH 49

NSString  *const TitleButtonDidRepeatClickNotification = @"TitleButtonDidRepeatClickNotification";
@interface GestureCommoneViewController ()<UIScrollViewDelegate>//若是不要手勢可不加
@property (nonatomic ,strong)UIScrollView *mainScrlooView;
@property (nonatomic,  weak) UIView *titlesView;
/**標題下滑線*/
@property(nonatomic,strong) UIView * titleUnderline;
/**滑動按鈕*/
@property(nonatomic,strong) UIButton * titleBtn;
@end

@implementation GestureCommoneViewController
//將scrollView的scrollEnable和處理標題view里面button的點擊關聯(lián),假如不需要滑動,則把scrollView的代理去掉
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;//navigation下面為坐標原點(0,0)
    [self setupAllChildControllers];
    // 添加第0個子控制器的view
    [self addChildVcViewIntoScrollView:0];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setupAllChildControllers{
    TestOneVC *vc1 = [TestOneVC new];
    TestTwoVC *vc2 = [TestTwoVC new];
    TestThreeVC *vc3 = [TestThreeVC new];
    TestFourVC *vc4 = [TestFourVC new];
    [self addChildViewController:vc1];
    [self addChildViewController:vc2];
    [self addChildViewController:vc3];
    [self addChildViewController:vc4];
    [self setupMainScrollerView];
    [self setupTitleView];
}
- (void)setupMainScrollerView{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, VHeight)];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
//    scrollView.scrollEnabled = NO;//默認為Yes//若是不要手勢設置為NO
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.scrollsToTop = NO; // 點擊狀態(tài)欄的時候,這個scrollView不會滾動到最頂部
    scrollView.delegate = self;//若是不要手勢可不加
    [self.view addSubview:scrollView];
    self.mainScrlooView = scrollView;
    NSUInteger count = self.childViewControllers.count;
    //    CGFloat scrollViewW = scrollView.width;
    scrollView.contentSize = CGSizeMake(count * SCREEN_WIDTH, 0);
    
}

- (void)setupTitleView{
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor];
    
    titlesView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    // 標題欄按鈕
    [self setupTitleButtons];    
    // 標題下劃線
    [self setupTitleUnderline];
}
- (void)setupTitleButtons{
    NSArray* titles = @[@"天氣",@"科技",@"生活",@"娛樂"];//隨便啥寫了四個button標題
    NSUInteger count = titles.count;
    //標題按鈕的尺寸
    CGFloat titleButtonW = self.titlesView.width/count;
    CGFloat titleButtonH = self.titlesView.height;
    for (NSUInteger i = 0; i<count; i++) {
        UIButton * titleButton = [[UIButton alloc]init];
        [titleButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        titleButton.tag = i;
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titlesView addSubview:titleButton];
        titleButton.frame = CGRectMake(i*titleButtonW, 0, titleButtonW, titleButtonH);
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
    }
    
}
- (void)setupTitleUnderline{
    UIButton * firstTitleButton = self.titlesView.subviews.firstObject;
    //下劃線
    UIView * titleUnderline = [[UIView alloc]init];
    titleUnderline.height = 2;
    titleUnderline.y = self.titlesView.height - titleUnderline.height;
    titleUnderline.backgroundColor = [UIColor redColor];
    [self.titlesView addSubview:titleUnderline];
    self.titleUnderline = titleUnderline;
    firstTitleButton.selected = YES;
    self.titleBtn = firstTitleButton;
    [firstTitleButton.titleLabel sizeToFit];
    self.titleUnderline.width = firstTitleButton.width;
    self.titleUnderline.centerX = firstTitleButton.centerX;
}
- (void)titleButtonClick:(UIButton *)titleButton{
    
    if (self.titleBtn == titleButton) {
        [[NSNotificationCenter defaultCenter]postNotificationName:TitleButtonDidRepeatClickNotification object:nil];
    }
    // 處理標題按鈕點擊
    [self dealTitleButtonClick:titleButton];
}
- (void)dealTitleButtonClick:(UIButton*)titleButton{
    
    self.titleBtn.selected = NO;
    titleButton.selected = YES;
    self.titleBtn = titleButton;
    NSUInteger index = titleButton.tag;
    [UIView animateWithDuration:0.25 animations:^{
        //處理下劃線
        self.titleUnderline.width = titleButton.width;
        self.titleUnderline.centerX = titleButton.centerX;
        CGFloat offsetX = self.mainScrlooView.width*index;
        self.mainScrlooView.contentOffset = CGPointMake(offsetX, self.mainScrlooView.contentOffset.y);
    }completion:^(BOOL finished) {
        [self addChildVcViewIntoScrollView:index];
    }];    
    for (NSUInteger i = 0; i<self.childViewControllers.count; i++) {  
        UIViewController * childVc = self.childViewControllers[i];
        if (!childVc.isViewLoaded)continue;
        UIScrollView *scrollView = (UIScrollView*)childVc.view;
        if (![scrollView isKindOfClass:[UIScrollView class]]) continue;
        scrollView.scrollsToTop = (i == index);
    }   
}
- (void)addChildVcViewIntoScrollView:(NSInteger)index{ 
    UIViewController*childVc = self.childViewControllers[index];
    if (childVc.isViewLoaded) return;
    // 設置子控制器view的frame
    // 取出index位置對應的子控制器view
    UIView *childVcView = childVc.view;
    CGFloat scrollViewW = self.mainScrlooView.width;
    childVcView.frame = CGRectMake(index * scrollViewW, 44, scrollViewW, self.mainScrlooView.height);
    // 添加子控制器的view到scrollView中
    [self.mainScrlooView addSubview:childVcView];    
}
//如果不添加手勢,既沒有scrollView的代理及方法了
#pragma -mark scrolleViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 求出標題按鈕的索引
    NSUInteger index = scrollView.contentOffset.x / scrollView.width;
    // 點擊對應的標題按鈕
    self.titleBtn = self.titlesView.subviews[index];
    //    [self titleButtonClick:titleButton];
    [self dealTitleButtonClick:self.titleBtn];    
}
@end

千外別走開哦,涉及到手勢問題就會涉及到手勢沖突處理。
說一個我遇到的情況,子控制器的里面是一個最常見的tableView,cell的刪除原生的里面有個側(cè)滑(向左滑),這時候就要做手勢沖突處理,還是代碼啦
創(chuàng)建一個


scrollView子類繼承UIScrollView
#import <UIKit/UIKit.h>
@interface FSScrollViewGes : UIScrollView<UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end
#import "FSScrollViewGes.h"
@implementation FSScrollViewGes
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [otherGestureRecognizer.view.superview isKindOfClass:[UITableView class]];
}
@end

其實就是判斷一下手勢來自何方,使用方法也是很簡單就是利用剛剛寫好的子類去創(chuàng)建scrollView就好了

//用FSScrollViewGes去創(chuàng)建ScrollView,既下面第一句話替代原有的創(chuàng)建方法就好
UIScrollView *scrollView = [[FSScrollViewGes alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, VHeight)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, VHeight)];

這樣的話tableView的側(cè)滑刪除方能正常側(cè)滑出來,否則多側(cè)滑幾次也是可以出來,但是那真的需要運氣

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
添加一個刪除action(UITableViewRowAction)
}

OK,結(jié)束,我的方法就是簡單的實現(xiàn)一下功能,完成基本的功能,想要各種花式
1.比如最前面的全局屬性button,你可以自定義,那樣就是各種花式的button
2.假如需要titleView能滾動,那么就把titleView由UIView換成UIScrollView
沙漠騎士

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,352評論 4 61
  • 嗯哼嗯哼蹦擦擦~~~ 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 ...
    philiha閱讀 5,259評論 0 6
  • 逆光總是會刺眼的,可這并不影響她高不可及的地位。很多人相繼的撲向她,當然我也不例外。
    千浮生閱讀 150評論 0 0
  • 好句當摘錄。 讀論語而不知論語。反過來說,即使不識字,還是可以讀書。 自己看得懂和用耳朵聽得懂,只是運用的方法不同...
    先行教育羅茜閱讀 146評論 0 0

友情鏈接更多精彩內(nèi)容