IOS development diary 57-gradient effect SegmentControl, 57-segmentcontrol
Today, the blogger has a demand for custom SegmentControl and has encountered some difficulties. I would like to share with you the hope to make common progress.
Let's take a look at it. When the speed is slow, we will find that when the red view moves, the colors of the two sides of the Label are different.
The code is not difficult to achieve with poor visual effects. Post the code and ask the readers to study it on their own.
-(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];
_ Red view. 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 );
}];
}