iOS-滑竿、分段选择器、开关按钮的简单实现
2016-03-23 19:51
489 查看
滑竿、分段选择器、开关按钮的简单实现,它们都是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++) {
[images
addObject:[UIImage
imageNamed:[NSString
stringWithFormat:@"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 = [[UISegmentedControl
alloc]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;
}
}
#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++) {
[images
addObject:[UIImage
imageNamed:[NSString
stringWithFormat:@"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 = [[UISegmentedControl
alloc]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;
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事
- IOS 改变键盘颜色代码