reprint Please declare source: http://blog.csdn.net/jinnchang/article/details/44487407
------------------------------------------------------------------------------------------
1, Summary
------------------------------------------------------------------------------------------
2. Code
viewcontroller.swift//uislidersample////Created by Jinnchang on 15/3/19.//Copyright (c) 2015 Jinn Chang. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller {var myslider:uislider? Override Func Viewdidload () {//define a button so that the slider resets let button1 = Uibutton.buttonwithtype (. System) as? UIButton button1? frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2----), button1? Settitle ("Reset", ForState:UIControlState.Normal) button1? AddTarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) button1?. Tag = 1//define a button to get the value of the current slider let button2 = Uibutton.buttonwithtype (. System) as? UIButton button2? frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-$, button2,)? Settitle ("Get Value", ForState:UIControlState.Normal) button2?. AddTarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) Button2?. Tag = 2//define a slider Myslider = UISlider (Frame:cgrectmake (SELF.VIEW.FRAME.WIDTH/2-100, 400, 200, 100))//Set the minimum value that the slider can scroll to Myslider?. MinimumValue = 0//Set the maximum value Myslider the slider can scroll to?. MaximumValue = 100//Set default value Myslider?. Value = 20//Sets whether the event is continuously triggered, if set to false, does not fire during the slide, if set to true, the myslider will be triggered continuously during the sliding process. Continuous = TRUE//sets the picture of the left rail of the slider Myslider? Setminimumtrackimage (UIImage (named: "Track.png"), ForState:UIControlState.Normal)//Set slider right rail picture Myslider? Setmaximumtrackimage (UIImage (named: "Track.png"), ForState:UIControlState.Normal)//Set the slider for picture Myslider?. Setthumbimage (UIImage (named: "Thumb.png"), ForState:UIControlState.Normal)//Note the state of uicontrolstatehightlighted is added here, Otherwise, when you drag the slider, the slider becomes the native control Myslider?. Setthumbimage (UIImage (named: "Thumb.png"), forState:UIControlState.Highlighted)//Set slider to the left of the picture Myslider? . Minimumvalueimage = UIImage (named: "MIn.png ")//Set slider to the right picture Myslider?. Maximumvalueimage = UIImage (named: "Min.png")//Add control event Myslider?. AddTarget (Self,action:selector ("Slidervaluechange:"), forControlEvents:UIControlEvents.ValueChanged) Myslider?. AddTarget (Self,action:selector ("Sliderdragup:"), ForControlEvents:UIControlEvents.TouchUpInside) Self.view . Addsubview (button1!) Self.view.addSubview (button2!) Self.view.addSubview (myslider!) }///button response event func buttonaction (Sender:uibutton) {var num = Sender.tag Switch num {Case 1 : if (Myslider?. Value! =) {Myslider?. SetValue (animated:true)} case 2:println ("Value:\" (myslider!. Value) ") Default:break}}///Slider Drag event func Slidervaluechange (Sender:uislider) { println ("Value:\ (Sender.value)")}///slider after drag event func Sliderdragup (sender:uislider) {println ("Value:\ (Sender.value)}}
------------------------------------------------------------------------------------------
3, ResourceProject on Github address: uislidersample
Article last updated: March 20, 2015 09:54:24. The resources are as follows:
UIKit User Interface catalog:sliders
UISlider Class Reference
Introduction to Swift Development: slider (UISlider)