滑竿、分段选择器、开关按钮的简单实现,它们都是UIControl的子类、下面是代码示例:
#import"ViewController.h"
@interface ViewController ()
{
UIView *bgView;
UIImageView *animationView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
bgView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:bgView];
NSMutableArray *images = [NSMutableArray array];
for (int i=1; i<=6;i++) {
[imagesaddObject:[UIImageimageNamed:[NSStringstringWithFormat:@"niao2-%d(被拖移).tiff",i]]];
}
animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
animationView.animationImages= images;
animationView.animationRepeatCount = -1;
// animationView.animationDuration = 3;
[self.view addSubview:animationView];
// 1、分段选择控件使用多个按钮的时候可以选择使用分段选择控件
// 2、开关按钮
// 3、滑杆
//今天练习的都是UIControl的子类包括之前学的按钮
// 1、分段选择控件
//分段选择控件再出始化的时候需给他一个标题的数组
UISegmentedControl*segment = [[UISegmentedControlalloc]initWithItems:@[@"娱乐",@"军事",@"科技"]];
segment.frame = CGRectMake(100, 100, 200, 40);
//设置是否记忆上一个按钮
segment.momentary = YES;
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
//开关按钮
//开关按钮一般需要记录用户设置的状态1、可以用后台提供的接口设置开关按钮的开关(可以在不同设备间同步状态(信息))2、在本地保存设置
UISwitch *switchButton = [[UISwitch alloc]initWithFrame:CGRectMake(100, 200, 50, 40)];
[switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//设置Switch的默认状态
switchButton.on = [userDefaults boolForKey:@"isOn"];
//设置开关按钮打开的时候轨道的颜色
switchButton.onTintColor = [UIColor redColor];
//设置开关按钮关闭时候的轨道颜色
switchButton.tintColor = [UIColor greenColor];
//设置开关按钮小圆圈的颜色
switchButton.thumbTintColor = [UIColor yellowColor];
[self.view addSubview:switchButton];
//滑杆
UISlider *slider = [[UISlideralloc]initWithFrame:CGRectMake(100,300, 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 = [UIColoryellowColor];
[self.view addSubview:slider];
}
//手指触摸到屏幕上的时候触发
- (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.0;
animationView.center = touchPoint;
[animationView startAnimating];
}
}
}
//手指在屏幕上移动触发
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"] != NO) {
//获得触摸事件
UITouch *touch = [touches anyObject];
//获得触摸的点
CGPoint touchPoint = [touch locationInView:self.view];
animationView.center = touchPoint;
}
}
//手指离开屏幕的时候(触摸结束)触发
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"] != NO) {
[UIView animateWithDuration:2 animations:^{
animationView.alpha = 0.0;
} completion:^(BOOL finished) {
[animationView stopAnimating];
}];
}
}
- (void)sliderAction:(UISlider*)sender
{
NSLog(@"%0.2f",sender.value);
animationView.animationDuration = sender.value;
}
- (void)switchAction:(UISwitch*)sender
{
NSLog(@"%d",sender.isOn);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:sender.isOn forKey:@"isOn"];
[userDefaults synchronize];
}
- (void)segmentAction:(UISegmentedControl*)sender
{
NSLog(@"%ld",sender.selectedSegmentIndex);
switch (sender.selectedSegmentIndex) {
case 0:
bgView.backgroundColor = [UIColorbrownColor];
break;
case 1:
bgView.backgroundColor = [UIColorwhiteColor];
break;
case 2:
bgView.backgroundColor = [UIColorlightGrayColor];
break;
default:
break;
}
}