import UIKit
enum DefaultShowStyle : Int {
case UILable
case UIButton
case UISegmentedControl
case UITextField
case UISlider
case UISwitch
case UIActivityIndicatorView
case UIProgressView
case UIPageControl
case UIStepper
case UIImageView
case UITextView
case UIDatePicker
case UIPickerView
case UIAlertView
case UIActionSheet
}
class DefaultShowVC: UIViewController, UITextFieldDelegate, UITextViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UIActionSheetDelegate {
var style: DefaultShowStyle = DefaultShowStyle.UILable;
private var width: CGFloat!
private var dataArray: Array<Array<String>> = []
override func viewDidLoad() {
super.viewDidLoad()
self.width = self.view.frame.size.width
switch (self.style) {
case DefaultShowStyle.UILable:
self.initUILabel()
case DefaultShowStyle.UIButton:
self.initUIButton()
case DefaultShowStyle.UISegmentedControl:
self.initUISegmentedControl()
case DefaultShowStyle.UITextField:
self.initUITextField()
case DefaultShowStyle.UISlider:
self.initUISlider()
case DefaultShowStyle.UISwitch:
self.initUISwitch()
case DefaultShowStyle.UIActivityIndicatorView:
self.initUIActivityIndicatorView()
case DefaultShowStyle.UIProgressView:
self.initUIProgressView()
case DefaultShowStyle.UIPageControl:
self.initUIPageControl()
case DefaultShowStyle.UIStepper:
self.initUIStepper()
case DefaultShowStyle.UIImageView:
self.initUIImageView()
case DefaultShowStyle.UITextView:
self.initUITextView()
case DefaultShowStyle.UIDatePicker:
self.initUIDatePicker()
case DefaultShowStyle.UIPickerView:
self.initUIPickerView()
case DefaultShowStyle.UIAlertView:
self.initUIAlertView()
case DefaultShowStyle.UIActionSheet:
self.initUIActionSheet()
default:
println("未找到")
}
}
private func initUILabel() {
let label:UILabel = UILabel(frame: CGRectMake(0, 0, 110, 30))
label.backgroundColor = UIColor.clearColor()
label.font = UIFont.systemFontOfSize(17)
label.textColor = UIColor.blueColor()
label.textAlignment = NSTextAlignment.Center
label.text = self.title
label.center = self.view.center
self.view.addSubview(label)
}
private func initUIButton() {
let button:UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
button.addTarget(self, action: "buttonTouchUpInside:", forControlEvents: UIControlEvents.TouchUpInside)
button.setImage(UIImage(named: "checked_un"), forState: UIControlState.Normal)
button.setImage(UIImage(named: "checked"), forState: UIControlState.Selected)
button.center = self.view.center
self.view.addSubview(button)
}
func buttonTouchUpInside(button:UIButton) {
button.selected = !button.selected
self.title = "UIButton(\(button.selected))"
}
private func initUISegmentedControl() {
let segmentedControl:UISegmentedControl = UISegmentedControl(items: ["1", "2", "3"])
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.center = self.view.center
self.view.addSubview(segmentedControl)
}
func segmentedControlValueChanged(segmentedControl:UISegmentedControl) {
self.title = "UISegmentedControl(\(segmentedControl.selectedSegmentIndex))"
}
private func initUITextField() {
let textField:UITextField = UITextField(frame: CGRectMake(0, 0, 155, 30))
textField.font = UIFont.systemFontOfSize(14)
textField.delegate = self
textField.keyboardType = UIKeyboardType.URL
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.clearButtonMode = UITextFieldViewMode.WhileEditing
textField.center = self.view.center
self.view.addSubview(textField)
self.inputAccessoryView(textField)
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
self.title = "textField:\(textField.text)"
}
private func initUISlider() {
let slider: UISlider = UISlider(frame: CGRectMake(0, 0, 200, 31))
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 50
slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
slider.center = self.view.center
self.view.addSubview(slider)
}
func sliderValueChanged(slider: UISlider) {
self.title = "UISlider:\(slider.value)"
}
private func initUISwitch() {
let uiSwitch = UISwitch(frame: CGRectMake(0, 0, 51, 31))
uiSwitch.tintColor = UIColor.blueColor()
uiSwitch.onTintColor = UIColor.greenColor()
uiSwitch.addTarget(self, action: "switchValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
uiSwitch.center = self.view.center
self.view.addSubview(uiSwitch)
}
func switchValueChanged(uiSwitch: UISwitch) {
self.title = "UISwitch:\(uiSwitch.on)"
}
private func initUIActivityIndicatorView() {
let activityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
activityIndicatorView.backgroundColor = UIColor.greenColor()
activityIndicatorView.alpha = 0.5
activityIndicatorView.layer.cornerRadius = 6;
activityIndicatorView.layer.masksToBounds = true;
activityIndicatorView.startAnimating()
activityIndicatorView.center = self.view.center
self.view.addSubview(activityIndicatorView)
}
private func initUIProgressView() {
let progressView = UIProgressView(frame: CGRectMake(0, 0, 150, 5))
progressView.progressViewStyle = UIProgressViewStyle.Default
progressView.center = self.view.center
self.view.addSubview(progressView)
let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "changeNSTimer:", userInfo: progressView, repeats: true)
timer.fire()
}
private func initUIPageControl() {
let pageControl = UIPageControl(frame: CGRectMake(0, 0, 100, 37))
pageControl.numberOfPages = 10
pageControl.currentPage = 1
pageControl.pageIndicatorTintColor = UIColor.greenColor()
pageControl.currentPageIndicatorTintColor = UIColor.blueColor()
pageControl.center = self.view.center
self.view.addSubview(pageControl)
let timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "changeNSTimer:", userInfo: pageControl, repeats: true)
timer.fire()
}
private func initUIStepper() {
let stepper = UIStepper(frame: CGRectMake(0, 0, 100, 50))
stepper.maximumValue = 10
stepper.minimumValue = 0
stepper.value = 5
stepper.stepValue = 0.1
stepper.continuous = true
stepper.wraps = true
stepper.addTarget(self, action: "stepperValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
stepper.center = self.view.center
self.view.addSubview(stepper)
}
func stepperValueChanged(stepper: UIStepper) {
self.title = "UIStepper:\(stepper.value)"
}
private func initUIImageView() {
let image = UIImage(named: "tab_1")
let imageView = UIImageView(image: image)
println("\(imageView.frame)")
imageView.center = self.view.center
self.view.addSubview(imageView)
self.view.backgroundColor = UIColor.lightGrayColor()
}
private func initUITextView() {
let textView = UITextView(frame: CGRectMake(0, 0, self.width-30, 400), textContainer:nil)
textView.delegate = self
textView.font = UIFont.systemFontOfSize(14)
textView.textAlignment = NSTextAlignment.Left
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.lightGrayColor().CGColor
textView.layer.cornerRadius = 5
self.inputAccessoryView(textView)
textView.center = self.view.center
self.view.addSubview(textView)
}
func textViewDidEndEditing(textView: UITextView) {
println("UITextView: \(textView.text)")
}
private func initUIDatePicker() {
let datePicker = UIDatePicker(frame: CGRectMake(0, 0, self.width, 400))
datePicker.datePickerMode = UIDatePickerMode.DateAndTime
datePicker.minuteInterval = 5
datePicker.date = NSDate()
datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
datePicker.maximumDate = dateFormatter.dateFromString("2015-05-16 08:00:00")
datePicker.minimumDate = dateFormatter.dateFromString("2015-07-16 08:00:00")
datePicker.center = self.view.center
self.view.addSubview(datePicker)
}
func datePickerValueChanged(datePicker: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
self.title = "\(dateFormatter.stringFromDate(datePicker.date))"
}
private func initUIPickerView() {
self.dataArray.append(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
self.dataArray.append(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
let pickerView = UIPickerView(frame: CGRectMake(0, 0, self.width, 400))
pickerView.dataSource = self
pickerView.delegate = self
pickerView.showsSelectionIndicator = true
pickerView.center = self.view.center
self.view.addSubview(pickerView)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return self.dataArray.count
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.dataArray[component].count
}
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 40
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 44
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
let array: Array = self.dataArray[component]
return array[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println("\(component)-\(row)")
let first = self.dataArray[0][pickerView.selectedRowInComponent(0)]
let second = self.dataArray[1][pickerView.selectedRowInComponent(1)]
self.title = "UIPickerView:\(first)\(second)"
}
private func initUIAlertView() {
let alertView = UIAlertView(title: "UIAlertView", message: "阳君测试", delegate: self, cancelButtonTitle: "取消")
alertView.addButtonWithTitle("1")
alertView.addButtonWithTitle("2")
alertView.show()
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if (alertView.cancelButtonIndex == buttonIndex) {
return
}
self.title = "UIAlertView:\(buttonIndex)"
}
private func initUIActionSheet() {
let actionSheet = UIActionSheet()
actionSheet.title = "UIActionSheet"
actionSheet.addButtonWithTitle("默认")
actionSheet.addButtonWithTitle("显著")
actionSheet.addButtonWithTitle("取消")
actionSheet.destructiveButtonIndex = 1
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1
actionSheet.delegate = self
actionSheet.showInView(self.view.window)
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
if (actionSheet.cancelButtonIndex == buttonIndex) {
return
}
self.title = "UIActionSheet:\(buttonIndex)"
}
private func inputAccessoryView(view: UIView) {
let topView:UIToolbar = UIToolbar(frame: CGRectMake(0, 0, self.width, 30))
let spaceButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "onClickDone:")
topView.items = [spaceButton, spaceButton, doneButton]
let textView:UITextView? = view as? UITextView
textView?.inputAccessoryView = topView
let textField:UITextField? = view as? UITextField
textField?.inputAccessoryView = topView
}
func onClickDone(button: UIBarButtonItem){
self.view.endEditing(true)
}
func changeNSTimer(timer: NSTimer) {
let progressView: UIProgressView? = timer.userInfo as? UIProgressView
let pageControl: UIPageControl? = timer.userInfo as? UIPageControl
if (progressView != nil) {
var progress = progressView!.progress
progress = progress == 1 ? 0 : progress + 0.01
progressView!.progress = progress
self.title = NSString(format: "UIProgressView:%.2f", progress) as String
} else if (pageControl != nil) {
var currentPage = pageControl!.currentPage
currentPage = currentPage == pageControl!.numberOfPages - 1 ? 0 : currentPage + 1
pageControl!.currentPage = currentPage
self.title = "UIProgressView:\(currentPage)"
}
}
}