最近想在自己的项目中添加一个右下角的悬浮按钮,这种按钮最初是在安卓中兴起来的,但是再很多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中就是d出按钮的相关属性,跟上面类似。
导入这两个文件到自己的工程后,就可以在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) }
这样运行一下就能得到自己想要的悬浮按钮了。总的来说这个例子就是简单实用,效果可能相对其他的例子简单些,但是功能是一样的。喜欢的话就尝试下吧 :)大家加油!
总结以上是内存溢出为你收集整理的Swift右下角悬浮按钮简单实现全部内容,希望文章能够帮你解决Swift右下角悬浮按钮简单实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)