资讯详情

iOS-滑竿、分段选择器、开关按钮的简单实现

滑杆、分段选择器、开关按钮的简单实现,都是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;

}

}

标签: 襄樊滑竿电位器延安滑竿电位器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台