2014-08-02 14:13:53 iteye_13725 阅读数 43

 

1. UiActionSheet 列表形式的按钮菜单

 

        var actionSheet = UIActionSheet()
        actionSheet.title = "菜单列表"
        actionSheet.delegate = self
        actionSheet.addButtonWithTitle("菜单1")
        actionSheet.addButtonWithTitle("菜单2")
        actionSheet.addButtonWithTitle("菜单3")
        actionSheet.addButtonWithTitle("菜单4")
        actionSheet.destructiveButtonIndex = 1
        actionSheet.showInView(self.view)

 

2015-10-07 09:06:39 wtt561111 阅读数 504

下载地址

http://download.csdn.net/detail/wtt561111/9159391

相关视频链接

http://www.maiziedu.com/course/ios/18-349/


编译环境

xcode6.3.1

代码清单

1 delegateTest工程,介绍了如何在delegate中进行界面的操作.还有如何利用tag获取组件。无法设置window背景色的原因在于没有初始化window

2 SegmentControlDemo2是关于他的一个各个功能的简单的介绍

3 textfieldTest工程不仅给出了textfeild的使用方法,还说出了输入完成后键盘的退     出:单击键盘的return还有界面的空白位置

4 textfieldLogin工程实现了简单的登录功能

5 sliderdemo这个项目,介绍了滑动按钮的使用

6 swichDemo这个项目,介绍了开关的用法

7 IndicatorViewDemo这个项目,介绍了指示器的用法

8 ProgressTest这个项目,介绍了进度条的使用,同时还用到了计时器NSTimer

9 StepperTest这个工程,介绍了计步器的使用,还涉及到如何讲一个数转换成字符串

10 ImageviewTest这个项目,介绍了图片视图的各个操作,包括加载本地和网络图片,   制作小动画

11 alertViewDemo这个工程,介绍了弹出视图的使用方法

12 UIActionSheet工程这个过程,介绍了动作列表的使用

13 DatePickerDemo 这个工程,介绍了时间选择器的使用

14 PickerViewDemo这个工程,介绍了选择器(包括多列)的操作。

同时很好的阐述了协议的使用。

15 relatedPicker这个项目,介绍了如何将选择器的列之间关联起来。

16 webviewDemo2这个项目,介绍了如何使用webview这个组件的使用。但是制作的过   程中,还是有一点问题,就是不能对webview进行位置限制,否则不能显示。

17 UIToolbarDemo这个项目,用工具栏的方式实现了简单的浏览器

可以在UIToolBar上面放置bar Button item。它可以是任意的UIView,但是加进去之后会被包装成bar button item。有两个特殊的bar button item:fixed space bar button item以及flexible space bar button。第一个是在两个item之间生成一个间隔。第二个是生成无限大的间隔,将其两边的item挤到两个边上。

18 ButtonSomeEvents简单介绍了按钮的几个用法




2016-02-27 15:51:19 HK_5788 阅读数 1063

1、滚动视图属性

属性                                功能
contentOffSet                滚动视图目前滚动的位置
contentSize                   滚动范围的大小
contentInset                   其他视图在滚动视图中的位置
delegate                        设置委托
directionalLockEnabled  指定滚动视图是否只能在一个方向上滚动
bounces                        指定滚动视图遇到边框后是否反弹
alwaysBounceVertical  指定垂直方向遇到边框是否反弹
alwaysBounceHorizontal  指定水平方向遇到边框是否反弹
pagingEnabled            是否能滚动
scrollEnabled               滚动视图是否整页翻动
showsHorizontalScrollIndicator  是否显示水平方向的滚动条
showsVerticalScrollIndicator  是否显示垂直方向的滚动条
scrollIndicatorInsets     指定滚动条在滚动视图中的位置
indicatorStyle                设定滚动条的样式
minimumZoomScale   缩小的最小比例
maximumZoomScale  放大的最大比例
zoomScale                   设置变化比例
bouncesZoom              指定缩放的时候是否会反弹

2、滚动视图事件

事件                                         功能
scrollViewDidScroll(_:)       已经滑动
scrollViewWillBeginDragging(_:)                     开始拖动
scrollViewDidEndDragging(_:willDecelerate:)                结束拖动
scrollViewShouldScrollToTop(_:)             是否支持滑动至顶部
scrollViewDidScrollToTop(_:)               滑动到顶部时调用该方法
scrollViewWillBeginDecelerating(_:)             开始减速
scrollViewDidEndDecelerating(_:)                减速停止
viewForZoomingInScrollView(_:)                   返回一个放大或者缩小的视图
scrollViewWillBeginZooming(_:withView:)  开始放大或者缩小
scrollViewDidEndZooming(_:withView:atScale:)  缩放结束时
scrollViewDidZoom(_:)                                    视图已经放大或缩小

2017-12-22 15:02:20 qq_31050781 阅读数 648

**第一学习使用ui swift写ui控件的控制代码,记录学习
UIButton和UISlider的一个小案例

ViewController.swift

//
//  ViewController.swift
//  uiTesting
//
//  Created by Friderick_Lee on 2017/12/19.
//  Copyright © 2017年 Friderick_Lee. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var lab: UILabel!
    @IBOutlet weak var lav: UILabel!
    var v = 0
    var i = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func d(_ sender: UISlider) {
        label.text="\(sender.value)"
        v=Int(sender.value)
        lav.text="\(i+v)"
    }


    @IBAction func btn(_ sender: UIButton) {
        i=i+1
        lab.text="\(i)"
        lav.text="\(i+v)"
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Main.storyboard

这里写图片描述

效果图

效果图片

2015-09-23 16:44:48 y550918116j 阅读数 736
//
//  DefaultShowVC.swift
//  Swift-UI
//
//  Created by yangjun on 15/6/15.
//  Copyright (c) 2015年 阳君. All rights reserved.
//

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("未找到")
        }
    }

    // MARK: - UILable
    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)// 加载到当前view
    }

    // MARK: - UIButton
    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)// 加载到当前view
    }

    // MARK: 用户点击UIButton
    func buttonTouchUpInside(button:UIButton) {
        button.selected = !button.selected
        self.title = "UIButton(\(button.selected))"
    }

    // MARK: - UISegmentedControl
    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)
    }

    // MARK: 用户点击UISegmentedControl
    func segmentedControlValueChanged(segmentedControl:UISegmentedControl) {
        self.title = "UISegmentedControl(\(segmentedControl.selectedSegmentIndex))"
    }

    // MARK: - UITextField
    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)// 完成按钮放在键盘上方
    }

    // MARK: UITextFieldDelegate
    // MARK: 正要进入编辑状态
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        return true
    }

    // MARK: 实现监听返回按钮
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()// 关闭键盘
        return true
    }

    // MARK: 输入完毕
    func textFieldDidEndEditing(textField: UITextField) {
        self.title = "textField:\(textField.text)"
    }

    // MARK: - initUISlider
    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)
    }

    // MARK: 用户点击UISlider
    func sliderValueChanged(slider: UISlider) {
        self.title = "UISlider:\(slider.value)"
    }

    // MARK: - UISwitch
    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)
    }

    // MARK: 用户点击UISwitch
    func switchValueChanged(uiSwitch: UISwitch) {
        self.title = "UISwitch:\(uiSwitch.on)"
    }

    // MARK: - UIActivityIndicatorView
    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)
    }

    // MARK: - UIProgressView
    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)
        // 0.5秒触发一次
        let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "changeNSTimer:", userInfo: progressView, repeats: true)
        timer.fire()// 启动
    }

    // MARK: - UIPageControl
    private func initUIPageControl() {
        let pageControl = UIPageControl(frame: CGRectMake(0, 0, 100, 37))
        pageControl.numberOfPages = 10// 默认10页
        pageControl.currentPage = 1// 选中第1页
        pageControl.pageIndicatorTintColor = UIColor.greenColor()// 默认背景色
        pageControl.currentPageIndicatorTintColor = UIColor.blueColor()// 选中时背景色
        pageControl.center = self.view.center
        self.view.addSubview(pageControl)
        // 0.5秒触发一次
        let timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "changeNSTimer:", userInfo: pageControl, repeats: true)
        timer.fire()// 启动
    }

    // MARK: - UIStepper
    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)
    }

    // MARK: 用户点击UIStepper
    func stepperValueChanged(stepper: UIStepper) {
        self.title = "UIStepper:\(stepper.value)"
    }

    // MARK: - UIImageView
    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()
    }

    // MARK: - UITextView
    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)
    }

    // MARK: - UITextViewDelegate
    func textViewDidEndEditing(textView: UITextView) {
        println("UITextView: \(textView.text)")
    }

    // MARK: - UIDatePicker
    private func initUIDatePicker() {
        let datePicker = UIDatePicker(frame: CGRectMake(0, 0, self.width, 400))
        datePicker.datePickerMode = UIDatePickerMode.DateAndTime //设置样式,当前设为同时显示日期和时间
        datePicker.minuteInterval = 5// 设置分钟表盘的时间间隔(必须能让60整除,默认是1分钟)
        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.setDate(NSDate(), animated: true)// 回到默认日期
        datePicker.center = self.view.center
        self.view.addSubview(datePicker)
    }

    // MARK: 用户选中UIDatePicker
    func datePickerValueChanged(datePicker: UIDatePicker) {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
        self.title = "\(dateFormatter.stringFromDate(datePicker.date))"
    }

    // MARK: - UIPickerView
    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)
    }

    // MARK: UIPickerViewDataSource
    // MARK: 列数
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

        return self.dataArray.count
    }

    // MARK: 行数
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataArray[component].count
    }

    // MARK: UIPickerViewDelegate
    // MARK: 列宽
    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 40
    }

    // MARK: 行高
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }

    // MARK: 每行每列显示的数据
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        // 也可以实现func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView 二者取其一
        let array: Array = self.dataArray[component]
        return array[row]
    }

    // MARK: 用户选中UIPickerView
    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)"
    }

    // MARK: - UIAlertView
    private func initUIAlertView() {
        let alertView = UIAlertView(title: "UIAlertView", message: "阳君测试", delegate: self, cancelButtonTitle: "取消")
        // 增加其他按钮
        alertView.addButtonWithTitle("1")
        alertView.addButtonWithTitle("2")
        alertView.show()// 显示
    }

    // MARK: UIAlertViewDelegate
    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        // 判断是否点击取消按钮
        if (alertView.cancelButtonIndex == buttonIndex) {
            return
        }
        self.title = "UIAlertView:\(buttonIndex)"
    }

    // MARK: - UIActionSheet
    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)// 显示
    }

    // MARK: UIActionSheetDelegate
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        // 判断是否点击取消按钮
        if (actionSheet.cancelButtonIndex == buttonIndex) {
            return
        }
        self.title = "UIActionSheet:\(buttonIndex)"
    }

    // MARK: - 键盘上方添加确定按钮
    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)//定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
        let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "onClickDone:")//定义完成按钮
        topView.items = [spaceButton, spaceButton, doneButton] //在toolBar上加上这些按钮

        // UITextView加确定按钮
        let textView:UITextView? = view as? UITextView
        textView?.inputAccessoryView = topView
        // UITextField加确定按钮
        let textField:UITextField? = view as? UITextField
        textField?.inputAccessoryView = topView
    }

    // 点击完成
    func onClickDone(button: UIBarButtonItem){
        self.view.endEditing(true)// 关闭键盘
    }

    // MARK: - 时间触发器
    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)"
        }
    }

}

iOS开发之基础控件的Swift版

博文 来自: yangshebing21

swift开发的仪表控件

博文 来自: ynmaoyong
没有更多推荐了,返回首页