論 Swift 開發入門 : 滑塊(UISlider)

來源:互聯網
上載者:User

標籤:ios   swift   滑塊   uislider   

轉載請聲明出處: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() {                // 定義一個按鈕,使 slider 複位        let button1 = UIButton.buttonWithType(.System) as? UIButton        button1?.frame = CGRectMake(self.view.frame.width/2 - 100, 50, 200, 50)        button1?.setTitle("reset", forState: UIControlState.Normal)        button1?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)        button1?.tag = 1                // 定義一個按鈕,擷取當前 slider 的值        let button2 = UIButton.buttonWithType(.System) as? UIButton        button2?.frame = CGRectMake(self.view.frame.width/2 - 100, 150, 200, 50)        button2?.setTitle("get value", forState: UIControlState.Normal)        button2?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)        button2?.tag = 2                // 定義一個 slider        mySlider = UISlider(frame:CGRectMake(self.view.frame.width/2 - 100, 400, 200, 100))        // 設定滑塊所能滾動到的最小值        mySlider?.minimumValue = 0        // 設定滑塊所能滾動到的最大值        mySlider?.maximumValue = 100        // 設定預設值        mySlider?.value = 20        // 設定是否連續觸發事件,如果設為 false,在滑動過程中不觸發,如果設為 true,在滑動過程中會連續觸發        mySlider?.continuous = true        // 設定滑塊左軌的圖片        mySlider?.setMinimumTrackImage(UIImage(named: "track.png"), forState: UIControlState.Normal)        // 設定滑塊右軌的圖片        mySlider?.setMaximumTrackImage(UIImage(named: "track.png"), forState: UIControlState.Normal)        // 設定滑塊的圖片        mySlider?.setThumbImage(UIImage(named: "thumb.png"), forState: UIControlState.Normal)        // 注意這裡要加UIControlStateHightlighted的狀態,否則當拖動滑塊時滑塊將變成原生的控制項        mySlider?.setThumbImage(UIImage(named: "thumb.png"), forState: UIControlState.Highlighted)        // 設定 slider 左側圖片        mySlider?.minimumValueImage = UIImage(named: "min.png")        // 設定 slider 右側圖片        mySlider?.maximumValueImage = UIImage(named: "min.png")        // 添加控制事件        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!)    }        /// 按鈕響應事件    func buttonAction(sender: UIButton) {        var num = sender.tag        switch num {        case 1:            if(mySlider?.value != 20){                mySlider?.setValue(20, animated: true)            }        case 2:            println("value:\(mySlider!.value)")        default:            break        }    }        /// 滑塊拖動時的事件    func sliderValueChange(sender: UISlider) {        println("value:\(sender.value)")    }        /// 滑塊拖動後的事件    func sliderDragUp(sender: UISlider) {        println("value:\(sender.value)")    }}
------------------------------------------------------------------------------------------
3、ResourceGithub 上項目地址:UISliderSample

文章最後更新時間:2015年3月20日09:54:24。參考資料如下:

UIKit User Interface Catalog: Sliders

UISlider Class Reference

論 Swift 開發入門 : 滑塊(UISlider)

相關文章

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.