Swift - UI

7、UIControl 、UIButton、UISegmente

2020-03-14  本文已影响0人  爱玩游戏的iOS菜鸟

本章主要讲述UIControl及其相关常用子类的用法

UIControl

初始化

继承自UIView,无特有初始化器

let control = UIControl(frame: CGRect(x:0, y: 0, width: 40, height: 40))
重要属性
重要方法
  //添加点击事件
  func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event)
  //移除点击事件
  func removeTarget(_ target: Any?, action: Selector?, for controlEvents: UIControl.Event)

UIButton

初始化器
  //便捷初始化器
  convenience init(type buttonType: UIButton.ButtonType)
  类方法 创建系统按钮 基本不用
  class func systemButton(with image: UIImage, target: Any?, action: Selector?) -> Self
重要属性
重要方法
//设置按钮title
func setTitle(_ title: String?, for state: UIControl.State)
//设置按钮title颜色
func setTitleColor(_ color: UIColor?, for state: UIControl.State)
//设置按钮title阴影
func setTitleShadowColor(_ color: UIColor?, for state: UIControl.State)
//设置按钮image
func setImage(_ image: UIImage?, for state: UIControl.State)
//设置按钮BackgroundImage
func setBackgroundImage(_ image: UIImage?, for state: UIControl.State)
//设置按钮富文本
func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State)
//相对应都有get方法,就不予展示了

UISegmentedControl

初始化

有特有初始化器,也可使用继承自UIView初始化器

//items [Any]?类型 可以是NSString或UIImage NSStrings or UIImages. 会根据宽度平等划分
let segment = UISegmentedControl(items: ["我","是"])
//先设置frame 后续通过添加方法添加item
let segment = UISegmentedControl(frame: CGRect(x: 20, y: 140, width: 100, height: 30))
重要属性
重要方法
//插入指定位置SegmentTitle
func insertSegment(withTitle title: String?, at segment: Int, animated: Bool)
//插入指定位置SegmentImage
func insertSegment(with image: UIImage?, at segment: Int, animated: Bool)
//移出指定下标Segment
func removeSegment(at segment: Int, animated: Bool)
//移出所有Segments
func removeAllSegments()

//修改指定位置Segment的Title
func setTitle(_ title: String?, forSegmentAt segment: Int)
//获取指定位置SegmentTitle的title
func titleForSegment(at segment: Int) -> String?
//修改指定位置Segment的Image
func setImage(_ image: UIImage?, forSegmentAt segment: Int)
//获取指定位置SegmentImage的Image
func imageForSegment(at segment: Int) -> UIImage?

//设置指定位置Segment宽度
func setWidth(_ width: CGFloat, forSegmentAt segment: Int)
//获取指定位置Segment宽度
func widthForSegment(at segment: Int) -> CGFloat

//设置指定位置Segment内容的偏移量
func setContentOffset(_ offset: CGSize, forSegmentAt segment: Int)
//获取指定位置Segment内容的偏移量
func contentOffsetForSegment(at segment: Int) -> CGSize
//设置指定位置Segment的Enabled
func setEnabled(_ enabled: Bool, forSegmentAt segment: Int) // default is YES
//获取指定位置Segment的Enabled
func isEnabledForSegment(at segment: Int) -> Bool
//设置整个segment的按钮文字颜色
func setTitleTextAttributes(_ attributes: [NSAttributedString.Key : Any]?, for state: UIControl.State)
//获取整个segment的按钮文字颜色
func titleTextAttributes(for state: UIControl.State) -> [NSAttributedString.Key : Any]?

其余属性类似ScopeBar,可以设置背景图片以及不同状态下的分割图片
响应事件
segment.addTarget(self, action: #selector(tapAction(_:)), for: UIControl.Event.valueChanged)

@objc func tapAction(_ segment: UISegmentedControl){
        kLog(segment.selectedSegmentIndex)
    }

UIStepper步进器

初始化

继承自UIView,无特有初始化器

let step = UIStepper(frame: CGRect(x: 20, y: 140, width: 100, height: 30))
重要属性
重要方法
//设置增加图片
func setIncrementImage(_ image: UIImage?, for state: UIControl.State)
//设置减少图片
func setDecrementImage(_ image: UIImage?, for state: UIControl.State)
对应的get方法也有,在UIKit标准库中查看
其余同UISegmentControl,也有设置背景图片以及不同状态下的分割图片
响应事件
step.addTarget(self, action: #selector(tapAction(_:)), for: UIControl.Event.valueChanged)

@objc func tapAction(_ step: UIStepper){
        kLog(step.value)
    }

UISwitch

初始化

继承自UIView,无特有初始化器

let switchV = UISwitch(frame: CGRect(x: 15, y: 150, width: 100, height: 50))

UISwitch该类控件,强制了其Size,因此frame中仅origin有效

重要属性及方法
//设置当前状态,是否需要动画 常用于其他控件控制该控件的状态
func setOn(_ on: Bool, animated: Bool)
响应事件
switchV.addTarget(self, action: #selector(tapAction(_:)), for:.valueChanged)

@objc func tapAction(_ switchV: UISwitch){
        kLog(switchV.isOn)
    }
上一篇 下一篇

猜你喜欢

热点阅读