美文网首页
iOS-UISegmentedControl和UIScrollV

iOS-UISegmentedControl和UIScrollV

作者: Mn_Su | 来源:发表于2016-09-23 13:19 被阅读0次

    一.声明属性和代理

        <UIScrollViewDelegate>
    @property(nonatomic,strong)UISegmentedControl * segmentedControl;
    @property(nonatomic,strong)UIScrollView *bgScroll;
    @property(nonatomic,strong) UIView *bgView ;
    @property(nonatomic,strong) UIView *bgView1 ;
    @property(nonatomic,strong) UIView *bgView2;
    

    二.实现

        #pragma mark - content内容
    - (void)creatContent{
        NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"实时数据",@"数据总览",@"评估报告",nil];
        //初始化UISegmentedControl
        self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
        self.segmentedControl.frame = CGRectMake(30, HEIGHT*0.12, WIDTH-60, 30);
        self.segmentedControl.selectedSegmentIndex = 0;
        self.segmentedControl.tintColor = [UIColor whiteColor];
       
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica" size:16] ,NSFontAttributeName,nil];
        [self.segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];
        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica" size:16] ,NSFontAttributeName,nil];
        [self.segmentedControl setTitleTextAttributes:dic1 forState:UIControlStateSelected];
    
        [self.segmentedControl addTarget:self action:@selector(segmentedClick:) forControlEvents:UIControlEventValueChanged];
        
        [self.view addSubview:self.segmentedControl];
        
    }
    
    - (void)segmentedClick:(UISegmentedControl *)sender{
        NSInteger index = sender.selectedSegmentIndex;
        switch (index) {
            case 0:
            {
                self.bgScroll.contentOffset = CGPointMake(0, 0);
    
            }
                break;
            case 1:
            {
                self.bgScroll.contentOffset = CGPointMake(WIDTH, 0);
            }
                break;
            case 2:
            {
                self.bgScroll.contentOffset = CGPointMake(WIDTH * 2, 0);
    
            }
                break;
        }
      
    }
    
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        [UIView animateWithDuration:1 animations:^{
            self.segmentedControl.selectedSegmentIndex = (scrollView.contentOffset.x + (WIDTH-20)*0.5)/(WIDTH-20);
        }];
    }
    
    
    - (void)createBottomLayer{
        self.bgScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HEIGHT*0.12 + 30 + 15, WIDTH, HEIGHT*0.88 -45)];
        self.bgScroll.pagingEnabled= YES;
        self.bgScroll.delegate = self;
        self.bgScroll.bounces = NO;
        self.bgScroll.contentSize = CGSizeMake(WIDTH*3, 0);
        [self.view addSubview:self.bgScroll];
        
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*0.88 - 45)];
        self.bgView.backgroundColor = [UIColor clearColor];
        [self.bgScroll addSubview:self.bgView];
        
        self.bgView1 = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT*0.88 - 45)];
        self.bgView1.backgroundColor = [UIColor yellowColor];
        self.bgView1.alpha = 0.7;
        [self.bgScroll addSubview:self.bgView1];
        
        self.bgView2 = [[UIView alloc]initWithFrame:CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT*0.88 - 45)];
        self.bgView2.backgroundColor = [UIColor purpleColor];
        self.bgView2.alpha = 0.5;
        [self.bgScroll addSubview:self.bgView2];
    }

    相关文章

      网友评论

          本文标题:iOS-UISegmentedControl和UIScrollV

          本文链接:https://www.haomeiwen.com/subject/geutyttx.html