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