UISlider的使用,UISlider使用

來源:互聯網
上載者:User

UISlider的使用,UISlider使用

//建立UISlider,高度是定值,但是不能設定為0

    //如果高度為0 滑動不了

    UISlider *slider = [[UISlideralloc] initWithFrame:CGRectMake(20,50, 200, 10)];


    //設定最值,別設定反了,不然不能滑動

    slider.maximumValue = 1;

    slider.minimumValue = 0;

    

    //設定劃條的初始值

    slider.value = .5;

//    [slider setValue:.5 animated:YES];

    

    //建立需要在滑動按鈕上顯示的圖片

    UIImage *img1 = [UIImageimageNamed:@"com_thumb_max_n-Decoded"];

    UIImage *img2 = [UIImageimageNamed:@"com_thumb_max_h-Decoded"];

    

    //設定滑動按鈕的圖片

    [slider setThumbImage:img1forState:UIControlStateNormal];

    [slider setThumbImage:img2forState:UIControlStateHighlighted];


    UIImage *sliderImg1 = [UIImageimageNamed:@"com_slider_min_l-Decoded"];

    UIImage *sliderImg2 = [UIImageimageNamed:@"com_slider_max_l-Decoded"];

    

    //設定展開點(------重點------)

    sliderImg1 = [sliderImg1 stretchableImageWithLeftCapWidth:10topCapHeight:0];

    sliderImg2 = [sliderImg2 stretchableImageWithLeftCapWidth:10topCapHeight:0];

    

    

    //設定劃條顯示的圖片

    [slider setMinimumTrackImage:sliderImg1forState:UIControlStateNormal];

    [slider setMaximumTrackImage:sliderImg2forState:UIControlStateNormal];

    

    //設定劃條顯示的顏色

//    [slider setMinimumTrackTintColor:[UIColor redColor]];

//    [slider setMaximumTrackTintColor:[UIColor greenColor]];

    

    //添加滑動事件

    [slider addTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];

    

    [_window addSubview:slider];

    

    

    

    

    /*------------------------UISwitch的使用----------------------*/

    //寬高固定的

    UISwitch *swithch = [[UISwitchalloc] initWithFrame:CGRectMake(90,90, 0, 0)];

    

    //設定開關的狀態,預設是關了

//    swithch.on = YES;

    [swithch setOn:YESanimated:YES];

    

    [swithch addTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];

    

    [_window addSubview:swithch];

    

    

    

    

    

    return YES;

}



- (void)sliderAction:(UISlider *)slider {


    NSLog(@"slider:%.2f",slider.value);


}


- (void)switchAction:(UISwitch *)sw {


    if (sw.on ==YES) {

        NSLog(@"開了");

    }else {

        NSLog(@"關了");

    }

    

}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.