美文网首页Swift - UI
7、UIControl 、UIButton、UISegmente

7、UIControl 、UIButton、UISegmente

作者: 爱玩游戏的iOS菜鸟 | 来源:发表于2020-03-14 16:16 被阅读0次

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

UIControl

初始化

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

let control = UIControl(frame: CGRect(x:0, y: 0, width: 40, height: 40))
重要属性
  • backgroundColor default is clear
  • isEnabled default is YES ,为否则忽略触摸事件
  • isSelected default is NO ,该属性可能会被其他子类使用(UIButton)
  • isHighlighted default is NO,高亮状态
  • state Control状态,可能会返回不止一种状态
  • contentVerticalAlignment 内容的垂直对齐方式
  • contentHorizontalAlignment 内容水平对齐方式
重要方法
  //添加点击事件
  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
重要属性
  • currentTitle
  • currentTitleColor 返回UIColor 有默认值white
  • currentTitleShadowColor
  • currentImage
  • currentBackgroundImage
    以上属性均为只读属性,当前状态所返回的值,除了currentTitleColor其余返回值均为可选值
  • titleLabel
  • imageView
重要方法
//设置按钮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))
重要属性
  • numberOfSegments 只读属性 获取segment的item数
  • isMomentary 如果为true,不会显示选定状态 default is false
  • apportionsSegmentWidthsByContent 如果为true则会根据item的内容自适应宽度 default is false,均等分配
  • selectedSegmentIndex 在isMomentary为true时失效,可设置或获取当前选择下标
  • selectedSegmentTintColor 此属性用于当前选中高亮状态的显示颜色 tintColor设置无效
重要方法
//插入指定位置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))
重要属性
  • isContinuous 触发事件的频率 default = YES if false,按钮结束才会触发事件,但是value依然会一直增加
  • autorepeat 按住按住value就会一直变化 if false,按钮结束value才会变化
  • wraps 设置控制器其值是否循环 default = NO 在设置最小最大值的时候可以
  • value 当前值,可读可写
  • minimumValue 最小值
  • maximumValue 最大值
  • stepValue 单次点击value变化,default 1
重要方法
//设置增加图片
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有效

重要属性及方法
  • onTintColor switch为on时,控件颜色
  • thumbTintColor switch的按钮颜色
  • onImage 设置后无效
  • offImage 设置后无效
  • isOn 当前UISwitch状态
//设置当前状态,是否需要动画 常用于其他控件控制该控件的状态
func setOn(_ on: Bool, animated: Bool)
响应事件
switchV.addTarget(self, action: #selector(tapAction(_:)), for:.valueChanged)

@objc func tapAction(_ switchV: UISwitch){
        kLog(switchV.isOn)
    }

相关文章

网友评论

    本文标题:7、UIControl 、UIButton、UISegmente

    本文链接:https://www.haomeiwen.com/subject/paiirhtx.html