swift UIbutton
2020-07-17 本文已影响0人
哎呦我去叫什么呢
有关于swift的UIbutton有以下的几种类型
UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.InfoDark:为感叹号“!”圆形按钮
UIButtonType.InfoLight:为感叹号“!”圆形按钮
常用的触摸事件类型:
TouchDown:单点触摸按下事件,点触屏幕
TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
TouchDragInside:触摸在控件内拖动时
TouchDragOutside:触摸在控件外拖动时
TouchDragEnter:触摸从控件之外拖动到内部时
TouchDragExit:触摸从控件内部拖动到外部时
TouchUpInside:在控件之内触摸并抬起事件
TouchUpOutside:在控件之外触摸抬起事件
TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
//初始化but
let loginBut = UIButton.init(type: .custom)
view.addSubview(loginBut)
//设置大小
loginBut.mas_makeConstraints { (make) in
make?.top.equalTo()(passTExtFiled.mas_bottom)?.offset()(100)
make?.centerX.mas_equalTo()(view.mas_centerX)
make?.width.mas_equalTo()(270)
make?.height.mas_equalTo()(45)
}
//圆角
loginBut.layer.cornerRadius = 23
loginBut.layer.masksToBounds = true
//背景颜色
loginBut.backgroundColor = .blue
//添加点击方式,不区分传参与不传参
//不传参
loginBut.addTarget(self, action: #selector(tapaction), for: .touchUpInside)
//传参
loginBut.addTarget(self, action: #selector(logAction(button:)), for: .touchUpInside)
loginBut.setTitle("按下", for: .normal)//设置文字
loginBut.setTitle("没有按下", for: .highlighted)//设置文字
loginBut.setImage(UIImage(named: "1"), for: .normal)//设置图片
loginBut.setTitleColor(UIColor.red, for: .normal)//设置文字颜色
loginBut.imageView?.contentMode = .scaleToFill//设置图片展示
loginBut.imageEdgeInsets = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)//图片边距
loginBut.setBackgroundImage(UIImage(named: "2"), for: .normal)//背景图
loginBut.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)//设置文字大小
loginBut.titleEdgeInsets = UIEdgeInsets(top: 11, left: 22, bottom: 2, right: 2 )//设置文字的位置
loginBut.isEnabled = false//是否可以点击,默认是true
//设置某几个个角为圆角
let shape:CAShapeLayer = CAShapeLayer();
// let bepath:UIBezierPath = UIBezierPath(roundedRect: loginBut.bounds, byRoundingCorners: UIRectCorner.topRight , cornerRadii: CGSize(width: 15, height: 15));
let bepath:UIBezierPath = UIBezierPath(roundedRect: loginBut.bounds, byRoundingCorners: UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.bottomLeft.rawValue) , cornerRadii: CGSize(width: 15, height: 15));
UIColor.black.setStroke();
shape.path = bepath.cgPath;
loginBut.layer.mask = shape;
实现button的点击事件
不传参
@objc func tapaction(){
print("点击了按钮")
}
不传参
@objc func logAction(button:UIButton) {
print(button.isSelected)
}