iOS开发——UI篇Swift篇&UISlider

UISlider

 

  1  

  2     override func viewDidLoad() {

  3         super.viewDidLoad()

  4         

  5         titleLabel.text = titleString

  6         

  7         

  8         // Do any additional setup after loading the view.

  9         

 10         

 11         

 12         

 13         //创建

 14         var slider:UISlider = UISlider(frame: CGRectMake(20, 100, 200, 60))

 15         

 16         //添加到视图上

 17         self.view.addSubview(slider)

 18         

 19         //设置最小值

 20         slider.minimumValue = 10

 21         

 22         //设置最大值

 23         slider.maximumValue = 80

 24         

 25         //设置默认值

 26         slider.value = 30

 27         

 28         //设置左端图片

 29         slider.setMinimumTrackImage(UIImage(named: "minimumTrackImage"), forState: UIControlState.Normal)

 30         

 31         //设置又端图片

 32         slider.setMaximumTrackImage(UIImage(named: "maximumTrackImage"), forState: UIControlState.Normal)

 33         

 34         

 35         

 36         //读取当滑块控件的值:

 37         var value = slider.value

 38         println(value)

 39         

 40         

 41         //添加通知,获取值改变

 42         slider.addTarget(self, action: "sliderValueChange:", forControlEvents: UIControlEvents.ValueChanged)

 43         

 44         //continuous默认是true,设置成false后,只有停止拖动才会触发sliderValueChange:方法

 45         slider.continuous = false

 46         

 47         

 48         

 49         //-----自定义滑块

 50         

 51         //定义左右图片

 52         var stetchLeftTrack:UIImage! = UIImage(named: "leftTrack.png")

 53         var stetchRightTrack:UIImage! = UIImage(named: "rightTrack.png")

 54        

 55         //定义滑块图片

 56          var thumbImage:UIImage! = UIImage(named: "thumbImage.png")

 57         

 58         //创建UISlider对象

 59         var newSlider:UISlider = UISlider(frame: CGRectMake(30, 300, 257, 8))

 60         newSlider.backgroundColor = UIColor.clearColor()

 61         newSlider.minimumValue = 0.5

 62         newSlider.maximumValue = 1.5

 63         newSlider.value = 1.0

 64         

 65         //设置左右图片

 66         newSlider.setMinimumTrackImage(stetchLeftTrack, forState: UIControlState.Normal)

 67         newSlider.setMaximumTrackImage(stetchRightTrack, forState: UIControlState.Normal)

 68         

 69         //设置拖动圆点的图片,注意这里要加UIControlState.Highlighted的状态,否则当拖动滑块时滑块将变成原生的控件

 70         newSlider.setThumbImage(thumbImage, forState: UIControlState.Normal)

 71         newSlider.setThumbImage(thumbImage, forState: UIControlState.Highlighted)

 72         

 73         //添加通知,获取值改变

 74         newSlider.addTarget(self, action: "newSliderValueChange:", forControlEvents: UIControlEvents.ValueChanged)

 75         

 76         

 77         //滑动拖动后的事件

 78         newSlider.addTarget(self, action: "newSliderTouchUpInside:", forControlEvents: UIControlEvents.TouchUpInside)

 79         

 80         self.view.addSubview(newSlider)

 81     }

 82 

 83     override func didReceiveMemoryWarning() {

 84         super.didReceiveMemoryWarning()

 85         // Dispose of any resources that can be recreated.

 86     }

 87     

 88 

 89     /*

 90     // MARK: - Navigation

 91 

 92     // In a storyboard-based application, you will often want to do a little preparation before navigation

 93     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

 94         // Get the new view controller using segue.destinationViewController.

 95         // Pass the selected object to the new view controller.

 96     }

 97     */

 98     

 99     

100     //slider值改变触发的方法

101     func sliderValueChange(sender: AnyObject?)

102     {

103 //        var newSlider:UISlider = sender as UISlider

104         

105         //2015年5月2后修改

106         var newSlider:UISlider = sender as! UISlider

107         var value = newSlider.value

108         println(value)

109     }

110     

111     

112     //自定义滑块使用

113     

114     //拖动事件

115     func newSliderValueChange(sender: AnyObject?)

116     {

117 //        var newSlider:UISlider = sender as UISlider

118         

119         //2015年5月2后修改

120         var newSlider:UISlider = sender as! UISlider

121         println(newSlider.value)

122     }

123     

124     //滑动拖动后的事件

125     func newSliderTouchUpInside(sender: AnyObject?)

126     {

127 //        var newSlider:UISlider = sender as UISlider

128         

129         //2015年5月2后修改

130         var newSlider:UISlider = sender as! UISlider

131         println(newSlider.value)

132     }

133     

 

 

 

 

你可能感兴趣的:(slider)