segmentedControl与swipe联合控制页面

燕昊东
2023-12-01

添加HMSegmentedControl 和 SwipeView 第三方控件加入项目

//subviews

@property (nonatomic,strong)HMSegmentedControl *segmentedControl;

@property (nonatomic,strong)SwipeView *swipeView;


viewDidLoad中添加

 // add subviews

    for (NSInteger index =0; index <self.categoryProjectsViewControllers.count; index++) {

        UIViewController *viewController =self.categoryProjectsViewControllers[index];

        [self.viewaddSubview:viewController.view];

    }

    [self.viewaddSubview:self.segmentedControl];

    [self.viewaddSubview:self.swipeView];

    [_swipeViewsetCurrentItemIndex:0];

    self.segmentedControl.selectedSegmentIndex =self.swipeView.currentItemIndex;  //显示swipeView的关键语句,否则会产生矛盾


在viewWillAppear中进行masonry布局时,不对视图数组中的单个视图进行布局而只是

    //layout subviews

    [self.segmentedControlmas_updateConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(weakSelf.view.mas_top);

        make.left.mas_equalTo(weakSelf.view.mas_left);

        make.right.mas_equalTo(weakSelf.view.mas_right);

        make.height.mas_equalTo(40);

    }];

    [self.swipeViewmas_updateConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(weakSelf.view.mas_top).offset(40);

        make.left.mas_equalTo(weakSelf.view.mas_left);

        make.right.mas_equalTo(weakSelf.view.mas_right);

        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);

    }];


随后添加

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    returnYES;

}

以及swipeview的代理

#pragma mark -SwipeViewDataSource


- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView

{

    //return the total number of items in the carousel

    returnself.categoryProjectsViewControllers.count;

}


- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view

{


   UIViewController *viewController =self.categoryProjectsViewControllers[index];

    self.segmentedControl.selectedSegmentIndex = index; //swipeView与segment联动的关键语句

        return viewController.view;

    

}

添加segment的切换逻辑

- (void)setSelectedIndex:(NSInteger)selectedIndex

{

    _selectedIndex = selectedIndex;

    

    for (NSInteger index =0; index <self.categoryProjectsViewControllers.count; index++) {

        

        UIViewController *viewController =self.categoryProjectsViewControllers[index];

        

        if(index == selectedIndex) {

            viewController.view.hidden =NO;


        }

        else {

            viewController.view.hidden =YES;


        }

    }

}

最后设置属性

- (HMSegmentedControl *)segmentedControl

{

    if (!_segmentedControl) {

        _segmentedControl = [[HMSegmentedControlalloc]initWithSectionTitles:self.categoryTitles];

        _segmentedControl.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleWidth;

        _segmentedControl.selectionIndicatorColor =  [HXColorcolorWithHexString:@"#4dad43"];

        _segmentedControl.selectionIndicatorLocation =HMSegmentedControlSelectionIndicatorLocationDown;

        _segmentedControl.selectionIndicatorHeight =2;

        _segmentedControl.selectedTitleTextAttributes =@{NSForegroundColorAttributeName : [HXColorcolorWithHexString:@"#4dad43"]};

        [_segmentedControladdTarget:selfaction:@selector(zjx_segmentedControlValueChanged:)forControlEvents:UIControlEventValueChanged];

        _segmentedControl.layer.borderWidth =1;

        _segmentedControl.layer.borderColor =TEWSubjectGreyColor().CGColor;


    }

    return_segmentedControl;

}



- (SwipeView *)swipeView

{

    if (!_swipeView) {

        _swipeView = [[SwipeViewalloc]init];

        _swipeView.dataSource =self;

        _swipeView.delegate =self;

        _swipeView.wrapEnabled =YES;

    }

    return_swipeView;

}

*(重要)以及动作

#pragma mark - Actions


- (void) zjx_segmentedControlValueChanged:(id)sender

{

    self.swipeView.currentItemIndex =self.segmentedControl.selectedSegmentIndex;

}

PS:

惨惨惨

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view;

会产生bug  只有一有滑动动作segment上的标签会马上变动


- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView;

加上

self.segmentedControl.selectedSegmentIndex = index;可以完美结局

 类似资料: