#pragma --------UISegmentedContro分段选择控件
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"娱乐",@"军事",@"科技"]];//分段选择控件 在初始化的时候 需要给它一个标题数组
segment.frame = CGRectMake(100, 100, 200, 50);//设置一下坐标
segment.momentary = YES;
[segment addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];//触发方法的方式
[self.view addSubview:segment];
- (void)segmentedAction:(UISegmentedControl *)sender
{
NSLog(@"%ld",(long)sender.selectedSegmentIndex);
switch (sender.selectedSegmentIndex) {
case 0:
bgView.backgroundColor = [UIColor brownColor];
break;
case 1:
bgView.backgroundColor = [UIColor whiteColor];
break;
case 2:
bgView.backgroundColor = [UIColor purpleColor];
break;
default:
break;
}
#pragma --------UISwitch开关按钮控件
UISwitch *switchButton = [[UISwitch alloc]initWithFrame:CGRectMake(10, 160, 50, 40)]; // 开关按钮一般需要记录用户设置的状态 1、可以用后台提供的接口设置开关按钮的开关(可以在不同设备之间同步(信息)) 2、在本地保存设置
switchButton.onTintColor = [UIColor redColor]; // 设置开关按钮 打开时候的轨道颜色
switchButton.tintColor = [UIColor grayColor]; // 设置开关关闭时候的颜色
switchButton.thumbTintColor = [UIColor yellowColor]; // 设置开关小圆圈的颜色
[switchButton addTarget:self action:@selector(switchAction:)
forControlEvents:UIControlEventValueChanged ];//触发方法的方式
// 设置switch的默认状态
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
switchButton.on = [userDefaults boolForKey:@"isOn"];
[self.view addSubview:switchButton];
- (void)switchAction:(UISwitch *)sender
{
NSLog(@"%d",sender.isOn);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:sender.isOn forKey:@"isOn"];
[userDefaults synchronize];
}
#pragma --------滑杆控件 UISlider (通过滑竿可以设置动画的速度)
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 250, 200, 10)];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 1; // 设置滑竿的最小值
slider.maximumValue =10; // 设置滑竿的最大值
slider.value = 1; // 设置默认值
slider.minimumTrackTintColor = [UIColor redColor]; // 设置最小值的轨道颜色
slider.maximumTrackTintColor = [UIColor purpleColor]; // 设置最大值的轨道颜色
slider.thumbTintColor = [UIColor orangeColor]; // 设置小圆圈的颜色
[self.view addSubview:slider];
- (void)sliderAction:(UISlider *)sender
{
NSLog(@"%0.2f",sender.value);
animationView.animationDuration = sender.value;
}
#pragma --------利用下面三个方法写一个动画
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event//当手指触摸到屏幕上的时候 触发这个事件
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isonOn"] != YES) {
UITouch *touch = [touches anyObject]; // 获得触摸时的点的位置
CGPoint touchPoint = [touch locationInView:self.view];
animationView.center = touchPoint;
if (animationView.isAnimating != YES)
// 动画没有被执行的时候 调用这个方法
{
animationView.alpha = 1.0;
animationView.center = touchPoint;
[animationView startAnimating];
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//手指在屏幕上移动 触发这个事件
{
UITouch *touch = [touches anyObject]; // 获得触摸时的点的位置
CGPoint touchPoint = [touch locationInView:self.view];
// animationView.center = touchPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // 手指离开时候触发的方法
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isonOn"] != NO) {
[UIView animateWithDuration:5 animations:^{
animationView.alpha = 0.8;
} completion:^(BOOL finished) {
[animationView stopAnimating];
}];
}
}