iOS开发技巧 - 使用UISlider来调整值的范围

(Swift)

import UIKit

class ViewController: UIViewController {
    var slider: UISlider!
    
    func sliderValueChanged(slider: UISlider) {
        println("Slider's new value is \(slider.value)")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        slider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 23))
        slider.center = view.center
        slider.minimumValue = 0
        slider.maximumValue = 100
        slider.value = slider.maximumValue / 2.0
        
        // customize slider and highlighted with own image
        slider.setThumbImage(UIImage(named: "ThumbNormal"), forState: .Normal)
        slider.setThumbImage(UIImage(named: "ThumbHighlighted"), forState: .Highlighted)
        
        slider.addTarget(self,
            action: "sliderValueChanged:",
            forControlEvents: .ValueChanged)
        
        view.addSubview(slider)
    }
}

 

(Objective-C)

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UISlider *slider;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0f,
        0.0f,
        200.0f,
        23.0f)];
    self.slider.center = self.view.center;
    self.slider.minimumValue = 0.0f;
    self.slider.maximumValue = 100.0f;
    self.slider.value = self.slider.maximumValue / 2.0;
    
    // customize slider and highlighted with own image
    [self.slider setThumbImage:[UIImage imageNamed:@"ThumbNormal.png"]
        forState:UIControlStateNormal];
    [self.slider setThumbImage:[UIImage imageNamed:@"ThumbHighlighted.png"]
        forState:UIControlStateHighlighted];
    
    [self.view addSubview:self.slider];
    
    [self.slider addTarget:self
        action:@selector(sliderValueChanged:)
        forControlEvents:UIControlEventValueChanged];
}

 

posted @ 2016-08-15 17:36  Master HaKu  阅读(2861)  评论(0编辑  收藏  举报