连带滑块效果图

  1 //
  2 //  ViewController.m
  3 //  连带滑动效果图
  4 //
  5 //  Created by apple on 16/8/20.
  6 //  Copyright © 2016年 apple. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #define kScreenWidth [UIScreen mainScreen].bounds.size.width
 11 #define kScreenHeight [UIScreen mainScreen].bounds.size.height
 12 @interface ViewController ()<UIScrollViewDelegate>
 13 {
 14     bool isNo[7];
 15 }
 16 @property (strong,nonatomic)NSArray *colorarr;
 17 @property (strong,nonatomic)NSArray *colorname;
 18 @property (strong,nonatomic)UIScrollView *huaview;
 19 @property (strong,nonatomic)UIScrollView *colorview;
 20 
 21 @end
 22 
 23 @implementation ViewController
 24 
 25 - (void)viewDidLoad {
 26     [super viewDidLoad];
 27     self.automaticallyAdjustsScrollViewInsets=NO;
 28     
 29     self.view.backgroundColor=[UIColor whiteColor];
 30     _colorname=[[NSArray alloc]initWithObjects:@"橙黄色",@"绿色",@"青色",@"白色",@"红色",@"蓝色",@"黄色", nil];
 31     _colorarr=[[NSArray alloc]initWithObjects:[UIColor orangeColor],[UIColor greenColor],[UIColor cyanColor],[UIColor whiteColor],[UIColor redColor],[UIColor blueColor],[UIColor yellowColor], nil];
 32     [self huakuaiview];
 33 }
 34 -(void)huakuaiview{
 35     _huaview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
 36     _huaview.backgroundColor=[UIColor blackColor];
 37     [self.view addSubview:_huaview];
 38     _huaview.contentSize=CGSizeMake(7*kScreenWidth/5, 0);
 39     _huaview.pagingEnabled=YES;
 40     _huaview.delegate=self;
 41     for (int i=0; i<7; i++) {
 42         UIButton *namebutton=[[UIButton alloc]initWithFrame:CGRectMake(i*kScreenWidth/5, 0, kScreenWidth/5, 60)];
 43         [namebutton setTitle:_colorname[i] forState:UIControlStateNormal];
 44         [namebutton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
 45         [namebutton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
 46         namebutton.titleLabel.textAlignment=NSTextAlignmentCenter;
 47         namebutton.titleLabel.font=[UIFont systemFontOfSize:22];
 48         [_huaview addSubview:namebutton];
 49         namebutton.tag=100+i;
 50         [namebutton addTarget:self action:@selector(colorview:) forControlEvents:UIControlEventTouchUpInside];
 51     }
 52     
 53     _colorview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, kScreenWidth, kScreenHeight-60)];
 54     [self.view addSubview:_colorview];
 55     _colorview.pagingEnabled=YES;
 56     _colorview.delegate=self;
 57     _colorview.contentSize=CGSizeMake(7*kScreenWidth, 0);
 58     for (int k=0; k<7; k++) {
 59         UIView *view=[[UIView alloc]initWithFrame:CGRectMake(k*kScreenWidth, 0, kScreenWidth, kScreenHeight-60)];
 60         view.backgroundColor=_colorarr[k];
 61         [_colorview addSubview:view];
 62     }
 63 }
 64 -(void)colorview:(UIButton*)senden{
 65     UIButton *button=[_huaview viewWithTag:senden.tag];
 66     CGRect rect=button.frame;
 67     NSInteger pagenum=rect.origin.x/(kScreenWidth/5);
 68     CGFloat contentOffsetX=pagenum*kScreenWidth;
 69     [_colorview setContentOffset:CGPointMake(contentOffsetX, 0) animated:YES];
 70     //防止多次点击
 71     isNo[senden.tag-100]=NO;
 72     //实现单次选中
 73     for (int k=100; k<107; k++) {
 74         if(k==senden.tag){
 75        button.selected=!isNo[senden.tag-100];
 76         isNo[senden.tag-100]=!isNo[senden.tag-100];
 77         }
 78         else{
 79           UIButton *button1=[_huaview viewWithTag:k];
 80             if(isNo[k-100]==YES){
 81                  isNo[k-100]=NO;
 82                 button1.selected=isNo[k-100];
 83             }else
 84             button1.selected=isNo[k-100];
 85             }
 86     }
 87 
 88     if (contentOffsetX>=kScreenWidth*5)
 89         [_huaview setContentOffset:CGPointMake(kScreenWidth/5*2, 0) animated:NO];
 90 }
 91 -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
 92     CGPoint point = *targetContentOffset;
 93     NSInteger pageNum = point.x / kScreenWidth;
 94     UIButton *button=[_huaview viewWithTag:pageNum+100];
 95     isNo[pageNum]=NO;
 96     for (int k=0; k<7; k++) {
 97         if(k==pageNum){
 98              button.selected=!isNo[pageNum];
 99             isNo[pageNum]=!isNo[pageNum];
100         }
101         else{
102             UIButton *button1=[_huaview viewWithTag:k+100];
103             if(isNo[k]==YES){
104                 isNo[k]=NO;
105                 button1.selected=isNo[k];
106             }else
107                 button1.selected=isNo[k];
108         }
109     }
110     if (pageNum>4)
111         [_huaview setContentOffset:CGPointMake(kScreenWidth/5*2, 0) animated:NO];
112     if (pageNum<2)
113         [_huaview setContentOffset:CGPointMake(0, 0) animated:NO];
114 }
115 
116 
117 @end
原文地址:https://www.cnblogs.com/chenrenshui/p/6131577.html