| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> 游戏开发 -> iOS应用如何减少UI代码-用此方法 -> 正文阅读 |
|
[游戏开发]iOS应用如何减少UI代码-用此方法 |
extension UIView { ? ? ///获取fram相关 ? ? var maxX: CGFloat { return frame.maxX } ? ? var maxY: CGFloat { return frame.maxY } ? ? var minX: CGFloat { return frame.minX } ? ? var minY: CGFloat { return frame.minY } ? ? var w: CGFloat { return frame.width } ? ? var h: CGFloat { return frame.height } ? ? var x: CGFloat { return frame.origin.x } ? ? var y: CGFloat { return frame.origin.y } ? ? ///设置视图 ? ? func setView(_ frame: CGRect, _ backgroundColor: UIColor) { ? ? ? ? self.frame = frame ? ? ? ? self.backgroundColor = backgroundColor ? ? } ? ? ///添加视图 ? ? func view(_ frame: CGRect) -> UIView { ? ? ? ? let view = UIView(frame: frame);addSubview(view);return view ? ? } ? ? func view(_ frame: CGRect, _ backgroundColor: UIColor) -> UIView { ? ? ? ? let view = UIView(frame: frame) ? ? ? ? view.backgroundColor = backgroundColor ? ? ? ? addSubview(view) ? ? ? ? return view ? ? } ? ? func view(_ frame: CGRect, _ backgroundColor: UIColor, _ radius: CGFloat) -> UIView { ? ? ? ? let view = UIView(frame: frame); view.backgroundColor = backgroundColor ? ? ? ? view.corner(radius); addSubview(view); return view ? ? } ? ? func view(_ frame: CGRect, _ backgroundColor: UIColor, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor) -> UIView { ? ? ? ? let view = UIView(frame: frame); view.backgroundColor = backgroundColor ? ? ? ? view.cornerBorder(radius, borderWidth, borderColor); ? ? ? ? addSubview(view); return view ? ? } ? ? ///添加滚动视图 ? ? func scrollView(_ frame: CGRect, _ backgroundColor: UIColor) -> UIScrollView { ? ? ? ? let sv = UIScrollView(frame: frame) ? ? ? ? sv.backgroundColor = backgroundColor ? ? ? ? if #available(iOS 11.0, *) { sv.contentInsetAdjustmentBehavior = .never } ? ? ? ? addSubview(sv) ? ? ? ? return sv ? ? } ? ? ///添加label ? ? func label(_ frame: CGRect) -> UILabel { ? ? ? ? let label = UILabel(frame: frame) ? ? ? ? addSubview(label) ? ? ? ? return label ? ? } ? ? func label(_ frame: CGRect, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment) -> UILabel { ? ? ? ? let label = UILabel(frame: frame) ? ? ? ? label.setLabel(textColor, font, textAlignment) ? ? ? ? addSubview(label) ? ? ? ? return label ? ? } ? ? func label(_ frame: CGRect, _ text: String, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment, _ numberOfLines: Int) -> UILabel { ? ? ? ? let label = UILabel(frame: frame) ? ? ? ? label.setLabel(text, textColor, font, textAlignment) ? ? ? ? label.numberOfLines = numberOfLines ? ? ? ? addSubview(label) ? ? ? ? return label ? ? } ? ? func label(_ frame: CGRect, _ text: String, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment) -> UILabel { ? ? ? ? let label = UILabel(frame: frame) ? ? ? ? label.setLabel(text, textColor, font, textAlignment) ? ? ? ? addSubview(label) ? ? ? ? return label ? ? } ? ? func label(_ frame: CGRect, _ attributeText: NSAttributedString, _ textAlignment: NSTextAlignment) -> UILabel { ? ? ? ? let label = UILabel(frame: frame) ? ? ? ? label.setLabel(attributeText, textAlignment) ? ? ? ? addSubview(label) ? ? ? ? return label ? ? } ? ? func label(_ frame: CGRect, _ backgroundColor: UIColor, _ text: String, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment) -> UILabel { ? ? ? ? let label = UILabel(frame: frame) ? ? ? ? label.setLabel(text, textColor, font, textAlignment) ? ? ? ? label.backgroundColor = backgroundColor ? ? ? ? addSubview(label) ? ? ? ? return label ? ? } ? ? ///添加按钮 返回YLButton ? ? func button(_ frame: CGRect) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.backgroundColor = backgroundColor ? ? ? ? button.tag = tag ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.tag = tag ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ isSelected: Bool, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.tag = tag ? ? ? ? button.isSelected = isSelected ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ isSelected: Bool, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.tag = tag ? ? ? ? button.isSelected = isSelected ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.corner(radius) ? ? ? ? button.backgroundColor = backgroundColor ? ? ? ? button.tag = tag;addSubview(button);return button ? ? } ? ? func button(_ frame: CGRect, _ radius: CGFloat, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.corner(radius) ? ? ? ? button.tag = tag ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func button(_ frame: CGRect, _ tag: Int, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.cornerBorder(radius, borderWidth, borderColor) ? ? ? ? button.tag = tag;addSubview(button);return button ? ? } ? ? func button(_ frame: CGRect, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ target: Any?, _ action: Selector) -> YLButton { ? ? ? ? let button = YLButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.cornerBorder(radius, borderWidth, borderColor) ? ? ? ? addSubview(button);return button ? ? } ? ? ///添加按钮 返回UIButton ? ? func uiButton(_ frame: CGRect, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button);return button ? ? } ? ? func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame) ? ? ? ? button.backgroundColor = backgroundColor ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame); button.tag = tag ? ? ? ? button.backgroundColor = backgroundColor ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button); return button ? ? } ? ? func uiButton(_ frame: CGRect, _ tag: Int, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame); button.tag = tag ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? addSubview(button); return button ? ? } ? ? func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame) ? ? ? ? button.backgroundColor = backgroundColor ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.corner(radius) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame); button.tag = tag ? ? ? ? button.backgroundColor = backgroundColor ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.corner(radius) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func uiButton(_ frame: CGRect, _ tag: Int, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame); button.tag = tag ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.corner(radius) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func uiButton(_ frame: CGRect, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.cornerBorder(radius, borderWidth, borderColor) ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? func uiButton(_ frame: CGRect, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ tag: Int, _ target: Any?, _ action: Selector) -> UIButton { ? ? ? ? let button = UIButton(frame: frame) ? ? ? ? button.addTarget(target, action: action, for: .touchUpInside) ? ? ? ? button.cornerBorder(radius, borderWidth, borderColor) ? ? ? ? button.tag = tag ? ? ? ? addSubview(button) ? ? ? ? return button ? ? } ? ? ///添加输入框 ? ? func textField(_ frame: CGRect,? _ text: String, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ adaptView: UIView) -> HWKeyBoardTextField { ? ? ? ? let tf = HWKeyBoardTextField(frame: frame) ? ? ? ? tf.adaptView(adaptView, beyondY: contentMargin) ? ? ? ? tf.setTextField(text, textColor, textFont, textAlignment, placeholder) ? ? ? ? addSubview(tf) ? ? ? ? return tf ? ? } ? ? func textField(_ frame: CGRect,? _ text: String, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ keyboard: UIKeyboardType, _ adaptView: UIView) -> HWKeyBoardTextField { ? ? ? ? let tf = HWKeyBoardTextField(frame: frame) ? ? ? ? tf.adaptView(adaptView, beyondY: contentMargin) ? ? ? ? tf.setTextField(text, textColor, textFont, textAlignment, placeholder, keyboard) ? ? ? ? addSubview(tf) ? ? ? ? return tf ? ? } ? ? func textField(_ frame: CGRect, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ keyboard: UIKeyboardType, _ adaptView: UIView) -> HWKeyBoardTextField { ? ? ? ? let tf = HWKeyBoardTextField(frame: frame) ? ? ? ? tf.adaptView(adaptView, beyondY: contentMargin) ? ? ? ? tf.setTextField(textColor, textFont, textAlignment, placeholder, keyboard) ? ? ? ? addSubview(tf) ? ? ? ? return tf ? ? } ? ? func textField(_ frame: CGRect, _ isEnable: Bool, _ text: String, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ keyboard: UIKeyboardType, _ adaptView: UIView) -> HWKeyBoardTextField { ? ? ? ? let tf = HWKeyBoardTextField(frame: frame) ? ? ? ? tf.isEnabled = isEnable ? ? ? ? tf.adaptView(adaptView, beyondY: contentMargin) ? ? ? ? tf.setTextField(text, textColor, textFont, textAlignment, placeholder, keyboard) ? ? ? ? addSubview(tf) ? ? ? ? return tf ? ? } ? ? ///添加图片视图 ? ? func imageView(_ frame: CGRect) -> UIImageView { ? ? ? ? let iv = UIImageView(frame: frame) ? ? ? ? addSubview(iv);return iv ? ? } ? ? func imageView(_ frame: CGRect, _ image: String, _ contentMode: ContentMode) -> UIImageView { ? ? ? ? let iv = UIImageView(frame: frame) ? ? ? ? iv.setImage(name: image, contentMode: contentMode) ? ? ? ? addSubview(iv) ? ? ? ? return iv ? ? } ? ? func imageView(_ frame: CGRect, _ image: String, _ radius: CGFloat) -> UIImageView { ? ? ? ? let iv = UIImageView(frame: frame) ? ? ? ? iv.setImage(name: image, contentMode: .scaleAspectFit) ? ? ? ? iv.corner(radius) ? ? ? ? addSubview(iv) ? ? ? ? return iv ? ? } ? ? func imageView(_ frame: CGRect, _ backgroundColor: UIColor, _ image: String, _ radius: CGFloat) -> UIImageView { ? ? ? ? let iv = UIImageView(frame: frame) ? ? ? ? iv.backgroundColor = backgroundColor ? ? ? ? iv.setImage(name: image, contentMode: .scaleAspectFit) ? ? ? ? iv.corner(radius);addSubview(iv);return iv ? ? } ? ? ///添加转换器 width: 51, height: 31 ? ? func uiSwitch(_ frame: CGRect, _ onTintColor: UIColor, _ isOn: Bool, _ target: Any?, _ action: Selector) -> UISwitch { ? ? ? ? let aSwitch = UISwitch(frame: frame) ? ? ? ? aSwitch.onTintColor = onTintColor ? ? ? ? aSwitch.isOn = isOn ? ? ? ? aSwitch.addTarget(target, action: action, for: .valueChanged) ? ? ? ? addSubview(aSwitch) ? ? ? ? return aSwitch ? ? } ? ? ///添加手势 ? ? func addTapGuesture(_ numberOfTouchs: Int, _ target: Any, _ action: Selector) { ? ? ? ? self.isUserInteractionEnabled = true ? ? ? ? let tap = UITapGestureRecognizer() ? ? ? ? tap.numberOfTouchesRequired = numberOfTouchs//手指个数 ? ? ? ? tap.addTarget(target, action: action) ? ? ? ? self.addGestureRecognizer(tap) ? ? } ?? ?? ? ? ? func corner(_ cornerRadius: CGFloat) { ? ? ? ? self.layer.masksToBounds = true ? ? ? ? self.layer.cornerRadius = cornerRadius ? ? } ? ? func cornerBorder(_ cornerRadius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor) { ? ? ? ? self.layer.masksToBounds = true ? ? ? ? self.layer.cornerRadius = cornerRadius ? ? ? ? self.layer.borderColor = borderColor.cgColor ? ? ? ? self.layer.borderWidth = borderWidth ? ? } } |
|
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/23 18:32:21- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |