UIControl及其子类
UISegmentedControl的用法
UISegmentedControl是iOS中得分段控件,每个segment都能被点击,相当于集成了若干个button.通常我们会点击不同的segment来切换不同的view
常用方法
initWithItems: //UISegmentedControl独有的初始化方法,⽤用来创建多个分段
setTitle: forSegmentAtIndex: //为指定下标的分段设置title
setImage: forSegmentAtIndex: //为指定下标的分段设置image insertSegmentWithTitle:
atIndex:animated: //指定下标插入分段
- (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"3005.png"]; NSArray *arr = @[@"分组",@"全部",@"推荐",@"共享",image]; UISegmentedControl *segmentcontrol = [[UISegmentedControl alloc] initWithItems:arr]; segmentcontrol.frame = CGRectMake(60, 100, 200, 30); [segmentcontrol addTarget:self action:@selector(choose:) forControlEvents:UIControlEventValueChanged]; segmentcontrol.selectedSegmentIndex = 2;//开始的时候就选中一个下标 [self.view addSubview:segmentcontrol]; [segmentcontrol release];
UISlider概述
UISlider是iOS中的滑块控件,通常用于控制视频播放进度,控制音量等,它也是继承于UIControl,滑块提
供了⼀系列连续的值,滑块停 在不同的位置,获取到滑块上的值也不同。
常用属性
minimumValue //设置滑块的最⼩小值
maximumValue //设置滑块的最⼤大值
value //设置滑块的当前值
addTarget: action: forControlEvents: 给UISlider添加事件, controlEvent为UIControlEventValueChanged。
UIImageView的使用
- (void)speed:(UISlider *)slider { UIImageView *imgv = (UIImageView *)[self.view viewWithTag:999]; [imgv stopAnimating]; imgv.animationDuration = slider.value; [imgv startAnimating]; } - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(120, 140, 68, 68)]; imageview.backgroundColor = [UIColor orangeColor]; NSMutableArray *images = [NSMutableArray arrayWithCapacity:10]; for (int i = 1; i <= 5; i++) { NSString *imagename = [NSString stringWithFormat:@"I]WXR8([Y]TFLAECY8EO2%%2-%d(被拖移).tiff",i]; UIImage *image = [UIImage imageNamed:imagename]; [images addObject:image]; } imageview.animationImages = images; imageview.animationDuration = 1;//动画播几次要用多长时间 // imageview.animationRepeatCount = -1; [imageview startAnimating]; [self.view addSubview:imageview]; imageview.tag = 999; [imageview release]; UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 420, 300, 20)]; [self.view addSubview:slider]; [slider addTarget:self action:@selector(speed:) forControlEvents:UIControlEventValueChanged]; [slider release]; }
image //设置图⽚片
animationImages //设置一组动态图片
animationDuration //设置播放一次一组动态图片的时间
animationRepeatCount //设置重复次数
startAnimating //开始动画
stopAnimating //结束动画
UIControl的作用
UIControl是所有控制控件(比如UIButton UISlider UISegmentedControl等)的基类,只要跟控制有关的控件都是继承于该类
UIControl的核心功能是可以为控制控件通过addTarget:action:forControlEvents:方法来添加事件.也可以通过removeTarget:action:forControlEvents:来移除事件