UI控件使用案例<一>

本文介绍了iOS应用中UISegmentedControl和UISlider的使用案例,包括设置分段控件的选项,滑块的最小值图片,以及如何用滑块控制图片动画播放速度。同时讲解了UIControl的事件处理机制和基本用法。
摘要由CSDN通过智能技术生成


1.  //********UISegmentedControl**********

    UIImage *image = [UIImageimageNamed:@"2.png"];

    NSArray *arr = @[@"分组",@"全部",@"推荐",image];//不能在一个seg里面既有文字又有图片

   UISegmentedControl *control = [[UISegmentedControl alloc]initWithItems:arr];

  

    [control addTarget:selfaction:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];

   control.selectedSegmentIndex = 1;//开始的时候就选中一个下标

    [controlsetTitle:@"喜欢" forSegmentAtIndex:3];//给指定下标设置标题

    //还可以设置指定下标的宽度

    //可以获取指定下标的宽度

   

    control.frame =CGRectMake(60, 100, 200, 40);

   control.backgroundColor = [UIColor whiteColor];

    [self.viewaddSubview:control];

[controlrelease];

 

 

2.  //*********UISlider滑块***********

    UISlider*slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 400, 280, 5)];

    [self.viewaddSubview:slider];

    [slideraddTarget:self action:@selector(red:)forControlEvents:UIControlEventValueChanged];

   slider.minimumValueImage = [UIImage imageNamed:@"2.png"];//给最小值处加图片

//   slider.maximumValue = 1;

//   slider.minimumValue = 0;

   //slider.transform =CGAffineTransformMakeRotation(M_PI_2*3);//滑块垂直状态

 

    [sliderrelease];

   

    UISlider*slider2 = [[UISlider alloc] initWithFrame:CGRectMake(20, 360, 280, 5)];

    [self.viewaddSubview:slider2];

//   slider2.maximumValue = 1;

//   slider2.minimumValue = 0;

    [slider2addTarget:self action:@selector(green:)forControlEvents:UIControlEventValueChanged];

   slider2.minimumValueImage = [UIImage imageNamed:@"2.png"];//给最小值处加图片

   

[slider2 release];

 

 

3.  //******用滑块控制播放图片速度*******

    UIImageView*imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 100, 100)];

   

    NSMutableArray*images = [NSMutableArray arrayWithCapacity:14];//创建一个存放图片的动态数组

    for (int i = 0;i <= 12; i++) {//把动画依次加入数组

        NSString*imageName = [NSString stringWithFormat:@"VMCXT6R3EB4P3VN@]UIN({X-%d(被拖移).tiff",i];

        UIImage*image = [UIImage imageNamed:imageName];

        [imagesaddObject:image];

    }

   imageView.animationImages = images;//添加播放数组

    imageView.animationDuration= 1;//动画播放速度

    [imageViewstartAnimating];//开始播放

   //imageView.animationRepeatCount = 4;//动画播放次数

   imageView.backgroundColor = [UIColor grayColor];

    imageView.tag =1000;

    [self.viewaddSubview:imageView];

    [imageViewrelease];

   

    UISlider*slider5 = [[UISlider alloc] initWithFrame:CGRectMake(20, 400, 280, 5)];

    [self.viewaddSubview:slider5];

   

    [slider5addTarget:self action:@selector(run:)forControlEvents:UIControlEventValueChanged];

    slider5.minimumValueImage= [UIImage imageNamed:@"2.png"];//给最小值处加图片

   

[slider5 release];

***********方法*********

-(void)run:(UISlider *)slider

{

    UIImageView *view = (UIImageView*)[self.view viewWithTag:1000];

    [view stopAnimating];

    view.animationDuration = slider.value;

    [view startAnimating];

   

}

 

4.  //************UIControl的学习**********

    //UIControl是很多种控制类的父类,例如:

    //UIButton(响应点击操作的控件),

    //UITextField(响应输入的控件),

    //UISlider(响应滑动的控件),

   //UISegmentedControl(分段控件),

    //UISwitch(开关),

    //UIStepper(递增,递减控件)等等

    //凡是需要响应用户控制的类,都继承与UIControl

    //UIControl作为基类,封装了事件的分发功能,封装了触发时机(枚举值,touchUpInside,valueChanged等).

    //你可以通过addTarget:action:forControlEvents:给UIControl及其子类添加事件(一个控件可以添加多个target-action对)

    //也可以通过removeTarget:action:forControlEvents:从UIControl及其子类中移除事件

   

   

    UITextField*tff = [[UITextField alloc] initWithFrame:CGRectMake(60, 60, 200, 30)];

    tff.borderStyle= UITextBorderStyleRoundedRect;

    [self.viewaddSubview:tff];

    tff.tag = 102;

    [tff release];


1.  //********UISegmentedControl**********

    UIImage *image = [UIImageimageNamed:@"2.png"];

    NSArray *arr = @[@"分组",@"全部",@"推荐",image];//不能在一个seg里面既有文字又有图片

   UISegmentedControl *control = [[UISegmentedControl alloc]initWithItems:arr];

  

    [control addTarget:selfaction:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];

   control.selectedSegmentIndex = 1;//开始的时候就选中一个下标

    [controlsetTitle:@"喜欢" forSegmentAtIndex:3];//给指定下标设置标题

    //还可以设置指定下标的宽度

    //可以获取指定下标的宽度

   

    control.frame =CGRectMake(60, 100, 200, 40);

   control.backgroundColor = [UIColor whiteColor];

    [self.viewaddSubview:control];

[controlrelease];

 

 

2.  //*********UISlider滑块***********

    UISlider*slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 400, 280, 5)];

    [self.viewaddSubview:slider];

    [slideraddTarget:self action:@selector(red:)forControlEvents:UIControlEventValueChanged];

   slider.minimumValueImage = [UIImage imageNamed:@"2.png"];//给最小值处加图片

//   slider.maximumValue = 1;

//   slider.minimumValue = 0;

   //slider.transform =CGAffineTransformMakeRotation(M_PI_2*3);//滑块垂直状态

 

    [sliderrelease];

   

    UISlider*slider2 = [[UISlider alloc] initWithFrame:CGRectMake(20, 360, 280, 5)];

    [self.viewaddSubview:slider2];

//   slider2.maximumValue = 1;

//   slider2.minimumValue = 0;

    [slider2addTarget:self action:@selector(green:)forControlEvents:UIControlEventValueChanged];

   slider2.minimumValueImage = [UIImage imageNamed:@"2.png"];//给最小值处加图片

   

[slider2 release];

 

 

3.  //******用滑块控制播放图片速度*******

    UIImageView*imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 100, 100)];

   

    NSMutableArray*images = [NSMutableArray arrayWithCapacity:14];//创建一个存放图片的动态数组

    for (int i = 0;i <= 12; i++) {//把动画依次加入数组

        NSString*imageName = [NSString stringWithFormat:@"VMCXT6R3EB4P3VN@]UIN({X-%d(被拖移).tiff",i];

        UIImage*image = [UIImage imageNamed:imageName];

        [imagesaddObject:image];

    }

   imageView.animationImages = images;//添加播放数组

    imageView.animationDuration= 1;//动画播放速度

    [imageViewstartAnimating];//开始播放

   //imageView.animationRepeatCount = 4;//动画播放次数

   imageView.backgroundColor = [UIColor grayColor];

    imageView.tag =1000;

    [self.viewaddSubview:imageView];

    [imageViewrelease];

   

    UISlider*slider5 = [[UISlider alloc] initWithFrame:CGRectMake(20, 400, 280, 5)];

    [self.viewaddSubview:slider5];

   

    [slider5addTarget:self action:@selector(run:)forControlEvents:UIControlEventValueChanged];

    slider5.minimumValueImage= [UIImage imageNamed:@"2.png"];//给最小值处加图片

   

[slider5 release];

***********方法*********

-(void)run:(UISlider *)slider

{

    UIImageView *view = (UIImageView*)[self.view viewWithTag:1000];

    [view stopAnimating];

    view.animationDuration = slider.value;

    [view startAnimating];

   

}

 

4.  //************UIControl的学习**********

    //UIControl是很多种控制类的父类,例如:

    //UIButton(响应点击操作的控件),

    //UITextField(响应输入的控件),

    //UISlider(响应滑动的控件),

   //UISegmentedControl(分段控件),

    //UISwitch(开关),

    //UIStepper(递增,递减控件)等等

    //凡是需要响应用户控制的类,都继承与UIControl

    //UIControl作为基类,封装了事件的分发功能,封装了触发时机(枚举值,touchUpInside,valueChanged等).

    //你可以通过addTarget:action:forControlEvents:给UIControl及其子类添加事件(一个控件可以添加多个target-action对)

    //也可以通过removeTarget:action:forControlEvents:从UIControl及其子类中移除事件

   

   

    UITextField*tff = [[UITextField alloc] initWithFrame:CGRectMake(60, 60, 200, 30)];

    tff.borderStyle= UITextBorderStyleRoundedRect;

    [self.viewaddSubview:tff];

    tff.tag = 102;

    [tff release];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值