Segment浅析(附scrollView与tableView手势冲突处理)

涉及到Segment的时候就不自觉地找个第三方解决一下,前期还觉得沾沾自喜,任务解决了,当更深入的了解一下就懵逼了,啥都不会,最近觉得Segment用的比较频繁,而第三方写的很是复杂,所以就自己写了两个简单的:
1.具有手势切换能力(scrollView代理方法里面将滑动手势和标题按钮点击关联)
2.没有手势切换能力
下面是代码时间
快创建创建一个控制器以及四个子控制器试试吧


创建控制器
#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的点击关联,假如不需要滑动,则把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; // 点击状态栏的时候,这个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的删除原生的里面有个侧滑(向左滑),这时候就要做手势冲突处理,还是代码啦
创建一个


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

其实就是判断一下手势来自何方,使用方法也是很简单就是利用刚刚写好的子类去创建scrollView就好了

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

这样的话tableView的侧滑删除方能正常侧滑出来,否则多侧滑几次也是可以出来,但是那真的需要运气

- (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,结束,我的方法就是简单的实现一下功能,完成基本的功能,想要各种花式
1.比如最前面的全局属性button,你可以自定义,那样就是各种花式的button
2.假如需要titleView能滚动,那么就把titleView由UIView换成UIScrollView
沙漠骑士

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,945评论 4 60
  • 嗯哼嗯哼蹦擦擦~~~ 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 ...
    philiha阅读 4,796评论 0 6
  • 逆光总是会刺眼的,可这并不影响她高不可及的地位。很多人相继的扑向她,当然我也不例外。
    千浮生阅读 127评论 0 0
  • 好句当摘录。 读论语而不知论语。反过来说,即使不识字,还是可以读书。 自己看得懂和用耳朵听得懂,只是运用的方法不同...
    先行教育罗茜阅读 100评论 0 0