码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发日记57-自定义SegmentControl

时间:2015-12-29 17:41:18      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

今天博主有一个自定义SegmentControl的需求,遇到了一些困难点,在此和大家分享,希望能够共同进步.

先来看看效果图

利用视觉差实现的效果,代码并不难,贴出来请各位看官自行研究

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSArray *array=[NSArray arrayWithObjects:@"Apple",@"Hello",@"Swift",@"World", nil];

    for (int i=0; i<4; i++) {

        UILabel *blackLabel=[[UILabel alloc]initWithFrame:CGRectMake(50+i*70, 300, 70, 30)];

        blackLabel.text=array[i];

        blackLabel.textAlignment=NSTextAlignmentCenter;

        blackLabel.textColor=[UIColor blackColor];

        [self.view addSubview:blackLabel];

    }

    self.redView=[[UIView alloc]initWithFrame:CGRectMake(50, 300, 70, 30)];

    _redView.clipsToBounds=YES;

    _redView.backgroundColor=[UIColor redColor];

    _redView.layer.cornerRadius=15.0;

    [self.view addSubview:_redView];

    self.whiteView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 70, 30)];

    [_redView addSubview:_whiteView];

    for (int i=0; i<4; i++) {

        UILabel *dLabel=[[UILabel alloc]initWithFrame:CGRectMake(i*70, 0, 70, 30)];

        dLabel.text=array[i];

        dLabel.textAlignment=NSTextAlignmentCenter;

        dLabel.textColor=[UIColor whiteColor];

        [_whiteView addSubview:dLabel];

    }

    for (int i=0; i<4; i++) {

        UIButton *buttonOF=[UIButton buttonWithType:UIButtonTypeCustom];

        buttonOF.backgroundColor=[UIColor clearColor];

        buttonOF.adjustsImageWhenHighlighted=NO;

        buttonOF.tag=1000+i;

        buttonOF.frame=CGRectMake(50+i*70, 300, 70, 30);

        [buttonOF addTarget:self action:@selector(moveToNew:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:buttonOF];

    }

}

 

-(void)moveToNew:(UIButton *)button

{

    [UIView animateWithDuration:5 animations:^{

        _redView.frame=CGRectMake(50+(button.tag-1000)*70, 300, 70, 30);

        _whiteView.frame=CGRectMake(-(button.tag-1000)*70, 0, 70, 30);

    }];

}

 

iOS开发日记57-自定义SegmentControl

标签:

原文地址:http://www.cnblogs.com/Twisted-Fate/p/5086446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!