关闭

UI第六天::UIControl及其⼦类

112人阅读 评论(0) 收藏 举报
分类:
⼀、UISegmentedControl的使⽤
UISegmentedControl是iOS中的分段控件。
每个segment都能被点击,相当于集成了若干个button。
通常我们会点击不同的segment来切换不同的view。
常⽤⽅法和属性
initWithItems: //UISegmentedControl独有的初始化⽅法,⽤来创建多个分段
setTitle: forSegmentAtIndex: //为指定下标的分段设置title
selectedSegmentAtIndex //(property)被选中的segment
tintColor //(property)segmentedControl条的颜⾊(含每个segment的颜⾊)
addTarget: action: forControlEvents: 给UISegmentedControl添加事件, controlEvent为UIControlEventValueChanged。
 UIImage *i1= [UIImage imageNamed:@"01"];
   
UIImage *i2= [UIImage imageNamed:@"02"];
   
UIImage *i3= [UIImage imageNamed:@"03"];
   
   
NSArray *array = [NSArray arrayWithObjects:i1,i2,i3, nil]; //初始化的数组中必须是字符串或者图片
   
   
UISegmentedControl *segement = [[UISegmentedControl alloc]initWithItems:array];
    segement.
frame = CGRectMake(50, 100, 300, 50);
    segement.
backgroundColor = [UIColor yellowColor];
   
//设置默认选中
    [segement
setSelectedSegmentIndex:0];
   
//修改中间宽带
    [segement
setWidth:150 forSegmentAtIndex:1];
   
//插入图片必须使用镂空图
//    [segement setImage:[UIImage imageNamed:@"01"] forSegmentAtIndex:0];
//    [segement setImage:[UIImage imageNamed:@"02"] forSegmentAtIndex:1];
//    [segement setImage:[UIImage imageNamed:@"03"] forSegmentAtIndex:2];
   
//修改选中颜色
    segement.
tintColor = [UIColor grayColor];
    [
self.view addSubview:segement];
    [segement
addTarget:self action:@selector(segement:) forControlEvents:UIControlEventValueChanged];
    [
self.view bringSubviewToFront:second.view];
    [
self.view bringSubviewToFront:segement];
    [segement
release];
}
-(
void)segement:(UISegmentedControl *)segement
{
   
if (segement.selectedSegmentIndex == 0) {
       
UIView *view = [self.view viewWithTag:1000];
        [
self.view bringSubviewToFront:view];
        [
self.view bringSubviewToFront:segement];
    }
if (segement.selectedSegmentIndex == 1) {
       
UIView *view = [self.view viewWithTag:1001];
        [
self.view bringSubviewToFront:view];
        [
self.view bringSubviewToFront:segement];
    }
if (segement.selectedSegmentIndex == 2) {
       
UIView *view = [self.view viewWithTag:1002];
        [
self.view bringSubviewToFront:view];
        [
self.view bringSubviewToFront:segement];
    }
}
⼆、UISlider的使⽤
UISlider是iOS中的滑块控件。
通常⽤于控制视频播放进度,控制⾳量等。
它也是继承于UIControl,滑块提供了⼀系列连续的值,滑块停在不 同的位置,获取到滑块上的值也不同。
minimumValue //设置滑块的最⼩值
maximumValue //设置滑块的最⼤值
value //设置滑块的当前值
minimumTrackTinkColor //定义划过区域的颜⾊
addTarget: action: forControlEvents: 给UISlider添加事件,controlEvent为UIControlEventValueChanged。
 self.slider1 = [[UISlider alloc]initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, 50)];
   
self.slider1.minimumValue = 0;
   
self.slider1.maximumValue = 256;
   
self.slider1.minimumValueImage = [UIImage imageNamed:@"01"];
   
self.slider1.maximumValueImage = [UIImage imageNamed:@"03"];
    [
self.slider1 setThumbImage:[UIImage imageNamed:@"01"] forState:UIControlStateNormal];
    [
self.slider1 setThumbImage:[UIImage imageNamed:@"02"] forState:UIControlStateHighlighted];
    [
self.slider1 addTarget:self action:@selector(slider1:) forControlEvents:UIControlEventValueChanged];
    [
self.view addSubview:self.slider1];
    [self.slider1 release];

三、UIImageView的使⽤
image //设置图⽚
animationImages //设置⼀组动态图⽚
animationDuration //设置播放⼀次⼀组动态图⽚的时间
animationRepeatCount //设置重复次数
startAnimating //开始动画
stopAnimating //结束动画
//imageview 跑图
   
self.image = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-300)/2, 200, 300, 300)];
   
self.image.backgroundColor = [UIColor whiteColor];
//    self.image.animationImages = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"],[UIImage imageNamed:@"3.jpg"],[UIImage imageNamed:@"4.jpg"],[UIImage imageNamed:@"5.jpg"],[UIImage imageNamed:@"6.jpg"],[UIImage imageNamed:@"7.jpg"],[UIImage imageNamed:@"8.jpg"],[UIImage imageNamed:@"9.jpg"],[UIImage imageNamed:@"10.jpg"], nil];
   
NSMutableArray *array =[NSMutableArray array];
   
// 构建一个图片数组
   
for (int i = 1; i < 11; i++) {
       
//把图片名字拼接出来
       
NSString *str = [NSString stringWithFormat:@"%d.jpg",i];
       
UIImage *image = [UIImage imageNamed:str];
        [array
addObject:image];
    }
   
self.image.animationImages = array;
   
self.image.animationDuration = 2.0;
   
self.image.animationRepeatCount = 0;
    [
self.image startAnimating];
    [
self.view addSubview:self.image];
    [
self.image release];
   
self.slider =[[UISlider alloc]initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, 50)];
   
   
self.slider.maximumValue = 5.0;
   
self.slider.minimumValue = 0.1;
   
self.slider.value = 2.0;
   
    [
self.slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
    [
self.view addSubview:self.slider];
    [
self.slider release];
}
-(
void)slider:(UISlider *)slider
{
   
if (self.slider.value == 5.0) {
        [
self.image stopAnimating];
        [
self.slider setThumbImage:[UIImage  imageNamed:@"03" ] forState:UIControlStateNormal];
       
self.image.image = [UIImage imageNamed:@"005.jpg"];
       
    }
else{
   
self.image.animationDuration =self.slider.value;
    [
self.image startAnimating];
    }

}


四、UIControl的作⽤
UIControl是所有控制控件(⽐如UIButton、UISlider、 UISegmentedControl等)的基类。
只要跟控制有关的控件都是继承于该类。
UIControl的核⼼功能:
为控制控件通过addTarget: action: forControlEvents: ⽅法来添加事 件。
通过removeTarget: action: forControlEvents: 来移除事件。
// button倒数计数器的建立
-(void)button:(UIButton *)button
{
   
//倒计时 核心每个一秒时间递减
   
//计时器(每个多少时间调用一个方法)
   
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:@"button倒计时" repeats:YES];
   
//计时器开始
    [timer
fire];
   
UIButton *b =(UIButton *)[self.view viewWithTag:1000];
    b.
userInteractionEnabled = NO;
}
-(
void)timer:(NSTimer *)timer
{
   
//Button标题
   
NSString *title = [NSString stringWithFormat:@"%ld",self.num--];
   
UIButton *b = (UIButton *)[self.view viewWithTag:1000];
    [b
setTitle:title forState:UIControlStateNormal];
   
//判断倒计时是否结束
   
if([[b titleForState:UIControlStateNormal] isEqualToString:@"0"])
    {
       
//停止计时器
        [timer
invalidate];
        b.
userInteractionEnabled = YES;
        [b
setTitle:@"重新发送验证码" forState:UIControlStateNormal];
       
self.num = 5;
    }
}

总结

UIControl是所有控制类控件的基类。
UISlider是可以响应滑动事件的控件
UISegmentedControl是可以实现单选的控件
UIImageView除了能显⽰单张图⽚,还能处理⼀组图⽚的播放。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4424次
    • 积分:359
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类