IOS學習筆記 (4) – iPhone手機開發技術文章 iPhone軟體開發教學課程

使用UISlider實現指定范圍選擇

創建一個滑動條並且把它放在試圖控制器的視圖中。

@interface SliderView:UIViewController

@property(non atomic,strong)UISlider *mySlider;

@end

@synthesize mySlider;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.mySlider = [ [UISlider alloc] initWithFrame:CGRectMake(0.0f,0.0f,200.0f,23.0f)];

    self.mySlider.center = self.view.center;

    self.mySlider.minimumValue = 0.0f;//選擇范圍的最小值

    self.mySlider.maximumValue= 100.0f;//選擇范圍的最大值

    self.mySlider.value = self.mySlider.maximumValue / 2.0;//當前值

    self.mySlider.continuous = NO;//默認YES,若為YES,thumb滑動時會連續調用slider d target。

    [self.mySlider addTarget:self action:@selecter(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubView:self.mySlider];

}

slider上的小滑塊叫做thumb;

target

-(void)sliderValueChanged:(UISlider *)paramSender{

    if([paramSender isEqual:self.mySlider]){

        NSLog(@"%@",paramSender.value);

    }

}

slider 的 thumb 圖片選中設置

setThumbImage:forState:方法

UIControlStateNormal  //正常狀態下的thumb ,即沒有點擊的。

UIControlStateHighlighted //選中狀態的thumb,即用戶點擊thumb時顯示的圖片

[self.mySlider setThumbImage:[UIImage imaged:@"ThumbNormal.png"] forState:UIControlStateNormal];

[self.mySlider setThumbImage:[UIImage imaged:@"ThumbHighlighted.png"] forState:UIControlStateHighlighted];

 

UISegmentedControl 做簡單選項分組

@interface SegmentedControl :UIViewController

@property(non atomic,strong)UISegmentedControl *mySegmentedControl;

@end;

@synthesize mySegmentedControl;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgrounderColor = [UIColor whiteColor];

    NSArray *segments = [[NSArray alloc] initWithObjects:@"iPhone",@"iPad",nil];

    self.mySegmentedControl = [[UISegmentedControl alloc]initWithItems:segments];

    self.mySegmentedControl.center = self.view.center;

    [self.view addSubview:self.mySegmentedControl];

}

You May Also Like