Swift右下角悬浮按钮简单实现
来源:互联网 发布:大拿韩代 知乎 编辑:程序博客网 时间:2024/05/21 17:26
最近想在自己的项目中添加一个右下角的悬浮按钮,这种按钮最初是在安卓中兴起来的,但是再很多iOS App中都能看到它身影,下面就推荐一个比较适合新手使用的悬浮按钮例子ActionButton。(在GitHub上翻出来的)
GitHub:https://github.com/lourenco-marinho/ActionButton
使用方法:除了官网提供的使用CocoaPods安装外,就是直接下载下来,手动添加它的有用文件。找到这两个文件拉到自己的工程目录下即可。
这个ActionButton.swift文件中就是gif中的橙色+号按钮的各种属性。这是这个按钮样式的相关属性
public init(attachedToView view: UIView, items: [ActionButtonItem]?) { super.init() self.parentView = view self.items = items let bounds = self.parentView.bounds self.floatButton = UIButton(type: .Custom) self.floatButton.layer.cornerRadius = CGFloat(floatButtonRadius / 2) self.floatButton.layer.shadowOpacity = 1 self.floatButton.layer.shadowRadius = 2 self.floatButton.layer.shadowOffset = CGSize(width: 1, height: 1) self.floatButton.layer.shadowColor = UIColor.grayColor().CGColor self.floatButton.setTitle("+", forState: .Normal) self.floatButton.setImage(nil, forState: .Normal) self.floatButton.backgroundColor = self.backgroundColor self.floatButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 35) self.floatButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0) self.floatButton.userInteractionEnabled = true self.floatButton.translatesAutoresizingMaskIntoConstraints = false self.floatButton.addTarget(self, action: #selector(ActionButton.buttonTapped(_:)), forControlEvents: .TouchUpInside) self.floatButton.addTarget(self, action: #selector(ActionButton.buttonTouchDown(_:)), forControlEvents: .TouchDown) self.parentView.addSubview(self.floatButton) self.contentView = UIView(frame: bounds) // 下面3行是设置点击按钮后背景View的视觉效果(类似毛玻璃),这里注释掉让背景无视觉效果 // self.blurVisualEffect = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) // self.blurVisualEffect.frame = self.contentView.frame // self.contentView.addSubview(self.blurVisualEffect) let tap = UITapGestureRecognizer(target: self, action: #selector(ActionButton.backgroundTapped(_:))) self.contentView.addGestureRecognizer(tap) self.installConstraints() }
ActionButtonItem.swift中就是弹出按钮的相关属性,跟上面类似。
导入这两个文件到自己的工程后,就可以在ViewController.swift中使用了。
var actionButton: ActionButton! // 首先右下角悬浮按钮声明// 视图创建override func viewDidLoad() { super.viewDidLoad() setContentView() // 配置界面 addActionButton() // 添加右下角悬浮按钮}func addActionButton() -> Void { let twitterImage = UIImage(named: "icon_1.png")! let plusImage = UIImage(named: "icon_2.png")! let google = ActionButtonItem(title: "密码设置", image: plusImage) google.action = { item in print("Google Plus...") } let twitter = ActionButtonItem(title: "退 出", image: twitterImage) twitter.action = { item in print("Twitter...") } actionButton = ActionButton(attachedToView: self.view, items: [twitter, google]) actionButton.action = { button in button.toggleMenu() } // 在这里设置按钮的相关属性,其实就是把刚刚那两个文件中的原始属性给覆盖了一遍,这里仅覆盖了2个旧属性 actionButton.setTitle("=", forState: .Normal) actionButton.backgroundColor = UIColor(red: 238.0/255.0, green: 130.0/255.0, blue: 34.0/255.0, alpha:1.0) }
这样运行一下就能得到自己想要的悬浮按钮了。总的来说这个例子就是简单实用,效果可能相对其他的例子简单些,但是功能是一样的。喜欢的话就尝试下吧 :)大家加油!
0 0
- Swift右下角悬浮按钮简单实现
- Android 悬浮按钮的简单实现
- jQuery实现网页右下角悬浮层提示
- 悬浮按钮的实现
- onTouchEventListener实现悬浮按钮
- 实现悬浮按钮
- 创建简单的悬浮按钮
- android悬浮按钮简单可拖拽
- android 超简单的拖动按钮 悬浮按钮 吸附按钮
- Android用悬浮按钮实现翻页效果
- Android用悬浮按钮实现翻页效果
- Android用悬浮按钮实现翻页效果
- Android用悬浮按钮实现翻页效果
- Jquery实现的悬浮按钮,浮现图片
- Android用悬浮按钮实现翻页效果
- Android用悬浮按钮实现翻页效果
- WZSZF飞鸽传书 如何实现网页悬浮按钮
- 使用XML实现悬浮的添加按钮
- 手写数字识别(2)---- CNN网络模型
- 数据结构实验之栈三:后缀式求值
- Google Code服务要关门了
- Tomcat热部署配置
- python读取文件时地址书写
- Swift右下角悬浮按钮简单实现
- SDUT3364数据结构实验之图论八:欧拉回路
- BestCoder Round #85-1005 gcd
- java/tomcat /http协议
- java获取本地cpu、内存、硬盘信息
- 猴子吃桃问题 南阳理工ACM 324
- ofdm_waveform_simplified_diagram_v0.4
- linux定时执行任务
- linq连接(Union)时报错问题处理