爬爬爬之路:UI(六) UISegmentedControl UISlider NSTimer的简单应用
来源:互联网 发布:win10清理垃圾软件 编辑:程序博客网 时间:2024/06/07 22:35
UISegmentedControl
UISegmentedControl(分段控制器)继承自UIControl
属性
@property(nonatomic,readonly) NSUInteger numberOfSegments;
用于获取分段数, 只读. 不能用于更改segment的分段数@property(nonatomic) NSInteger selectedSegmentIndex;
本属性非常重要.
用于设置或者获得当前被选中的分段下标@property(nonatomic,retain) UIColor *tintColor;
作用, 用于设置边框及未选中文字的填充色
方法
初始化方法:
- (instancetype)initWithItems:(NSArray *)items;
初始化方法, items可以存放字符串或者图片(必须是镂空图), 可以两者混合在同一个数组里. 生成的分段个数和数组的长度相同. 每个分段的标题和数组的内容一一对应. (标题可以是一张镂空图, 也可以是一段文字)
按照索引插入标题方法
插入的标题为文字的方法
- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
插入的标题为图片的方法
- (void)insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;
删除标题的方法
按照索引删除某个标题的方法
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
- 删除全部标题的方法
- (void)removeAllSegments;
根据索引重新设置某个标题
根据索引将某个标题重新设置为新的文字标题
- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment;
根据索引将某个标题重新设置为新的镂空图
- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
根据索引获取某个分段的标题
获取标题为文字的标题
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
获取标题为镂空图的标题
- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
根据索引定制某个分段的宽度
- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
根据索引获取某个分段的宽度
- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
核心方法:
重写UIControl类中添加响应事件方法:- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;// 特别注意的是, UISegmentControl选取的响应事件为UIControlEventValueChanged
UISlider
UISlider和UISegmentControl相同, 均继承于UIControl类
控件大小不受高度控制.但是会影响其所铺设的view的高度
但是若高度太小, 会导致UISlider所在的view高度小于控件高度, 会导致滑动事件从响应者链上无法被识别.
UISlider是固定从左往右递增的, 左侧为最小值, 右侧为最大值. 最大值不能小于最小值 否则会导致滑块无法右滑.
属性
@property(nonatomic) float value;
默认值为0, 用于设置或者获取当前状态下UISlider对象的值@property(nonatomic) float minimumValue;
默认值为0.0 用于设置UISlider能够获取的的最小值@property(nonatomic) float maximumValue;
默认值为1.0 用于设置UISlider能够获取的最大值注意的是, 若要通过value设置滑块的初始值必须在最大值和最小值设置完成以后
@property(nonatomic,retain) UIImage *minimumValueImage;
设置最小值图片. 最小值图片为滑块条整体的左侧, 当滑块条左侧空间太小时会挤占原来属于滑块条的空间.@property(nonatomic,retain) UIImage *maximumValueImage;
设置最大值图片. 最大值图片在滑块条整体的右侧, 一样在空间不足的情况下会挤占滑块条的控件.@property(nonatomic,getter=isContinuous) BOOL continuous;
默认是YES, 若设置成NO, slider的值只会在拖动停止的时候发生变化@property(nonatomic,retain) UIColor *minimumTrackTintColor;
设置左侧轨迹的颜色 (已走过的轨迹)@property(nonatomic,retain) UIColor *maximumTrackTintColor;
设置右侧轨迹的颜色 (未走过的轨迹)@property(nonatomic,retain) UIColor *thumbTintColor;
设置滑动球的填充色
方法
- 设置滑块的值, 并选择是否带有动画
- (void)setValue:(float)value animated:(BOOL)animated;
- 设置滑块某状态下的图片.
- (void)setThumbImage:(UIImage *)image forState:(UIControlState)state;
值得注意的是, 滑块在滑动的过程中是高亮状态(HighLight) 核心方法
和绝大多数继承自UIControl的类一样
核心方法均为:- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;// UISlider选取的响应事件也是 UIControlEventValueChanged
利用NSTimer完成倒计时功能
#import "RootViewController.h"@interface RootViewController ()// 设置倒计时的时间@property (nonatomic, assign) NSInteger times;@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // 倒计时设置为10秒 self.times = 10; UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)]; button.tag = 1000; button.frame = CGRectMake(100, 100, 200, 100); [self.view addSubview:button]; button.backgroundColor = [UIColor redColor]; [button setTitle:@"发送验证码" forState:(UIControlStateNormal)]; [button addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)]; [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; [button setTitleColor:[UIColor grayColor] forState:(UIControlStateSelected)]; [button setTitle:@"111" forState:(UIControlStateSelected)];}- (void)click:(UIButton *)button { // 每隔一秒钟 时间递减. // 利用计时器完成(实现每隔一段时间调用一个方法). // NSTimeInterval 代表时间间隔 // userInfo 用户信息 用于描述timer // 调用fire方法, 表示开始调用. button.selected = YES; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timer:) userInfo:@"button倒计时" repeats:YES]; // 让定时器开始工作. [timer fire];}- (void)timer:(NSTimer *)timer { UIButton *button = (UIButton *)[self.view viewWithTag:1000]; if (self.times < 1) { // 倒计时结束, button弹出, 恢复可点击状态, 重设title 并关闭定时器 button.selected = NO; button.userInteractionEnabled = YES; [button setTitle:@"重新发送验证码" forState:(UIControlStateNormal)]; [timer invalidate]; // 倒计时完成, 重置时间 self.times = 10; } else { // 倒计时中, 关闭button的编辑状态 显示剩余时间. button.userInteractionEnabled = NO; [button setTitle:[NSString stringWithFormat:@"%ld秒", self.times] forState:(UIControlStateSelected)]; self.times = self.times - 1; }}
1 0
- 爬爬爬之路:UI(六) UISegmentedControl UISlider NSTimer的简单应用
- 进击的KFC:UI(六)UISegmentControl,UISlider,NSTimer,UIImageView
- UI基础之-UIImage,UIImageView, UISegmentedControl,UISlider
- 【学习ios之路:UI系列】ios中常见的几种控件.(UISlider,UISwitch,UIStepper,UISegmentedControl)
- 【学习ios之路:UI系列】ios中常见的几种控件.(UISlider,UISwitch,UIStepper,UISegmentedControl)
- 【学习ios之路:UI系列】ios中常见的几种控件.(UISlider,UISwitch,UIStepper,UISegmentedControl)
- UISegmentedControl之简单应用
- UI 常用方法总结之--- UIImageView UISlider UISegmentedControl (不断更新中)
- UI - UISegmentedControl、UISlider、UIImageView、UISwitch
- UI控件之UISlider简单图片放大与缩小的应用
- UISlider的简单应用
- UISegmentedControl,UISlider,UISwitch,代码截屏,UIStepper的简单使用
- iOS开发之八:UISlider、UISegmentedControl、UIPageControl的使用
- 随意细解:UI -- UISegmentedControl、UISlider
- UISegmentedControl UISlider
- OC学习之MPMoviePlayerViewController;UISegmentedControl;UISlider;
- 关于NSTimer的简单应用
- UI控件之UISlider
- hdoj--4857--逃生(拓扑排序+反向建图)
- 最近知识小节
- poj3083Children of the Candy Corn
- 游戏服务器端开发要点
- 认识Java
- 爬爬爬之路:UI(六) UISegmentedControl UISlider NSTimer的简单应用
- springmvc006-处理模型数据
- hdu 3664 dp
- HDOJ 1075 What Are You Talking About (字典树映射)
- LeetCode 27 : Remove Element (Java)
- 淘宝下单高并发解决方案
- 读取txt数据,向excel中写入数据
- 30大恐怖游戏
- 程序员之路——一个老程序员对刚上大学的学弟学妹的忠告