你的位置:首页 > 操作系统

[操作系统]iOS开发日记57


今天博主有一个自定义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);

    }];

}