1、使用多個按鈕的時候 可以選擇分段選擇控件
分段選擇控件在初始化的時候需要給他一個標題的數組,讓它知道需要初始化多少個分段按鈕
<span style="font-size:18px;color:#666666;"> UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"娛樂",@"軍事",@"科技"]];
segment.frame = CGRectMake(100, 100, 150, 35);
// 設置是否記憶上一個按鈕
segment.momentary = YES;
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];</span><span style="color:#ff2d22;">
</span>
1⃣️、設置是否記憶上一個按鈕
segment.momentary = YES;
2⃣️、分段選擇控件觸發方式
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
- (void)segmentAction:(UISegmentedControl *)sender
{
NSLog(@"%ld",sender.selectedSegmentIndex);
switch (sender.selectedSegmentIndex) {
case 0:
bgView.backgroundColor = [UIColor darkGrayColor];
break;
case 1:
bgView.backgroundColor = [UIColor grayColor];
break;
case 2:
bgView.backgroundColor = [UIColor lightGrayColor];
break;
default:
break;
}
}
2、開關按鈕
開關按鈕一般需要記錄用戶設置的狀態 1⃣️、可以使用後臺提供的接口 設置開關按鈕的開關(可以在不同設備間同步狀態(信息)) 2⃣️、在本地保存設置(只能在本地保存,其他設備上無法保存)
以下爲各個屬性以及代碼的實現
<span style="font-size:14px;"> UISwitch *swithButton = [[UISwitch alloc]initWithFrame:CGRectMake(100, 200, 50, 35)];
[swithButton addTarget:self action:@selector(swithAction:) forControlEvents:UIControlEventValueChanged];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// 設置switch的默認狀態
swithButton.on = [userDefaults boolForKey:@"isOn"];
// 設置開關按鈕 打開的時候 軌道的顏色
swithButton.onTintColor = [UIColor redColor];
// 設置開關按鈕 關閉時候 軌道的顏色
swithButton.tintColor = [UIColor whiteColor];
// 設置開關按鈕 小圓圈的顏色
swithButton.thumbTintColor = [UIColor blueColor];
[self.view addSubview:swithButton];</span>
3、滑桿
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(100, 300, 200, 10)];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// 設置滑桿的最小值
slider.minimumValue = 0.0;
// 設置滑桿的最大值
slider.maximumValue = 10.0;
// 設置滑桿默認的位置
slider.value = 50.0;
// 設置滑桿最小值的軌道顏色
slider.minimumTrackTintColor = [UIColor greenColor];
// 設置滑桿最大值的軌道顏色
slider.maximumTrackTintColor = [UIColor redColor];
// 設置小圓圈的顏色
slider.thumbTintColor = [UIColor yellowColor];
[self.view addSubview:slider];
- (void)sliderAction:(UISlider *)sender
{
NSLog(@"%0.2f",sender.value);
// self.view.alpha = sender.value/10;
animationView.animationDuration = sender.value;
}
<span style="font-size:14px;">4、// 手指 觸摸到屏幕上得時候 觸發
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isOn"]!=NO) {
// 獲得觸摸事件
UITouch *touch = [touches anyObject];
// 獲得觸摸的點
CGPoint touchPoint = [touch locationInView:self.view];
// 動畫沒有執行的時候 調用這個方法
if (animationView.isAnimating !=YES) {
animationView.alpha = 1;
animationView.center = touchPoint;
[animationView startAnimating];
}
}
5、手指在屏幕上移動觸發
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 獲得觸摸事件
UITouch *touch = [touches anyObject];
// 獲得觸摸的點
CGPoint touchPoint = [touch locationInView:self.view];
animationView.center = touchPoint;
[animationView startAnimating];
}
6、// 手指離開屏幕的時候觸發
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:2 animations:^{
animationView.alpha = 0.0;
} completion:^(BOOL finished) {
[animationView stopAnimating];
}];
}</span>