我有代码来创建BubbleMenu按钮,最初来自here。
我想让BubbleMenu成为一个动作,但我不知道该怎么做。
例如" A" = LogOut," B" =登录或其他。
这是代码
//
// ViewController.swift
// DWBubbleMenuButton.Swift
//
// Created by feiin on 14/10/25.
// Copyright (c) 2014 year swiftmi. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var upMenuView:DWBubbleMenuButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func hiddenAll(sender: AnyObject) {
print("ok")
upMenuView.dismissButtons()
}
override func viewDidAppear(animated: Bool) {
// Create up menu button
let homeLabel2 = self.createHomeButtonView()
upMenuView = DWBubbleMenuButton(frame: CGRectMake(self.view.frame.size.width - homeLabel2.frame.size.width - 20.0,self.view.frame.size.height - homeLabel2.frame.size.height - 20.0,
homeLabel2.frame.size.width,homeLabel2.frame.size.height),expansionDirection: .DirectionUp)
upMenuView.homeButtonView = homeLabel2
upMenuView.addButtons(self.createDemoButtonArray())
self.view.addSubview(upMenuView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createHomeButtonView() -> UILabel {
let label = UILabel(frame: CGRectMake(0.0, 0.0, 40.0, 40.0))
label.text = "Tap";
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.layer.cornerRadius = label.frame.size.height / 2.0;
label.backgroundColor = UIColor(red:0.0,green:0.0,blue:0.0,alpha:0.5)
label.clipsToBounds = true;
return label;
}
func createDemoButtonArray() -> [UIButton] {
var buttons:[UIButton]=[]
var i = 0
for str in ["A","B","C","D","E","F"] {
let button:UIButton = UIButton(type: UIButtonType.System)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
button.setTitle(str, forState: UIControlState.Normal)
button.frame = CGRectMake(0.0, 0.0, 30.0, 30.0);
button.layer.cornerRadius = button.frame.size.height / 2.0;
button.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
button.clipsToBounds = true;
i += 1
buttons.append(button)
}
return buttons
}
}
答案 0 :(得分:1)
func createDemoButtonArray() -> [UIButton] {
var buttons:[UIButton]=[]
var i = 0
for str in ["A","B","C","D","E","F"] {
let button:UIButton = UIButton(type: UIButtonType.System)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
button.setTitle(str, forState: UIControlState.Normal)
button.frame = CGRectMake(0.0, 0.0, 30.0, 30.0);
button.layer.cornerRadius = button.frame.size.height / 2.0;
button.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
button.clipsToBounds = true;
i += 1
buttons.append(button)
if button.currentTitle == "A" {
button.addTarget(self, action: Selector("login"), forControlEvents: UIControlEvents.TouchUpInside)
}
if button.currentTitle == "B" {
button.addTarget(self, action: Selector("logout"), forControlEvents: UIControlEvents.TouchUpInside)
}
}
return buttons
}