首页 > 代码库 > UISegmentedControl(分页控制器) AND UISlider(滑块控制器)

UISegmentedControl(分页控制器) AND UISlider(滑块控制器)

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     UIImageView * animatedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(6080200300)];  
  6.     animatedImageView.backgroundColor = [UIColor redColor];  
  7.     animatedImageView.tag = 100;  
  8.     [self.view addSubview:animatedImageView];  
  9.     [animatedImageView release];  
  10.       
  11.     NSMutableArray * arrayImages = [NSMutableArray array];  
  12.     for (int i = 1; i < 8; i++) {  
  13.           
  14.         [arrayImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"huoju_%i.tiff",i]]];  
  15.   
  16.     }  
  17.     // 添加要实现动画效果的一系列图片  
  18.     animatedImageView.animationImages = arrayImages;  
  19.   
  20. #pragma mark - 分段控制器  
  21.     //initWithItems:可以添加图片或者字符串  
  22.     UISegmentedControl * segment = [[UISegmentedControl alloc]initWithItems:@[@"statr",@"stop",@"HMT"]];  
  23.     segment.frame = CGRectMake(102030040);  
  24.     // 分了几段  
  25.     NSLog(@"%lu",segment.numberOfSegments);  
  26.     // 选中时,是一直呈现高亮状态(NO)还是瞬间的高亮(YES)  
  27.     segment.momentary = YES;  
  28.     [segment addTarget:self action:@selector(onClickSegmentedControl:) forControlEvents:UIControlEventValueChanged];  
  29.     // 把下标为2的区段,设置为不可点击  
  30.     [self.view addSubview:segment];  
  31.     [segment release];  
  32.       
  33.       
  34.     // 滑动控制器  
  35.     UISlider * slider  = [[UISlider alloc]initWithFrame:CGRectMake(1050030040)];  
  36.     slider.maximumValue = 3;  
  37.     slider.minimumValue = 0.1;  
  38.     NSLog(@"%f",slider.value);  
  39.     [self.view addSubview:slider];  
  40.     [slider release];  
  41.     [slider addTarget:self action:@selector(changeSliderValue:) forControlEvents:UIControlEventValueChanged];  
  42.     
  43. }  
  44.   
  45.   
  46. - (void)onClickSegmentedControl:(UISegmentedControl *)segmented{  
  47.       
  48.      // selectedSegmentIndex  
  49.     if (segmented.selectedSegmentIndex == 0) {  
  50.         // 启动动画  
  51.         [((UIImageView *)[self.view viewWithTag:100]) startAnimating];  
  52.           
  53.     } else {  
  54.         // 停止动画  
  55.         [((UIImageView *)[self.view viewWithTag:100]) stopAnimating];  
  56.           
  57.     }  
  58. }  
  59.   
  60. - (void)changeSliderValue:(UISlider *)sLider{  
  61.       
  62.     // 通过滑动控件,增减速度  
  63.     ((UIImageView *)[self.view viewWithTag:100]).animationDuration = sLider.value;  
  64.     [((UIImageView *)[self.view viewWithTag:100]) startAnimating];  
  65. }  
  66.   
  67.   
  68. - (void)didReceiveMemoryWarning  
  69. {  
  70.     [super didReceiveMemoryWarning];  
  71.     // Dispose of any resources that can be recreated.  
  72. }