UIButton - ボタンを表示
説明
UIButtonはUIControlのサブクラス
ボタンの形式をUIButtonType (System, DetailDisclosure, InfoLight, InfoDark, ContactAdd)で指定して作成することも可能.(DetailDisclosure, InfoLight, InfoDarkはiOS7.0以降では全て同じ)
ソースコード
UIButtonSample.swift
import UIKit class UIButtonSample: UIView { override init(frame: CGRect) { super.init(frame: frame) let button = UIButton(frame: CGRectMake(0, 0, 200, 40)) self.addSubview(button) button.center = self.center // ボタンの背景色 button.backgroundColor = colorPattern.main() // ボタンの影 button.layer.shadowOffset = CGSizeMake(10.0, 10.0) button.layer.shadowOpacity = 0.5 // ボタンの文字 button.setTitle("Button", forState: UIControlState.Normal) // ボタンの文字色 button.setTitleColor(colorPattern.lightText(), forState: UIControlState.Normal) // ボタンの文字の影 (影の位置を変更しないと影は表示されないので注意) button.setTitleShadowColor(colorPattern.shadow(), forState: UIControlState.Normal) // ボタンの文字の影の位置 button.titleLabel!.shadowOffset = CGSize(width: 2.0, height: 2.0) // ボタンが押された時の文字 button.setTitle("Touched", forState: UIControlState.Highlighted) // ボタンが押された時の文字色 button.setTitleColor(colorPattern.accent(), forState: UIControlState.Highlighted) // ボタンのフォント button.titleLabel?.font = UIFont.boldSystemFontOfSize(30.0) // 押し始めた時の動作 button.addTarget(self, action: "buttonMoveDown:", forControlEvents: UIControlEvents.TouchDown) // 押し終わった時の動作 button.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside) // 離された時の動作 button.addTarget(self, action: "buttonMoveUp:", forControlEvents: UIControlEvents.TouchUpInside | UIControlEvents.TouchUpOutside | UIControlEvents.TouchCancel) // UIButtonType には System, DetailDisclosure, InfoLight, InfoDark, ContactAddがある // アイコンなし(文字を設定しないと何も表示されない) let button0 = UIButton.buttonWithType(UIButtonType.System) as UIButton self.addSubview(button0) let frame0 = CGRect(x: 0, y: 70, width: self.frame.width, height: 50) button0.frame = frame0 button0.setTitle("System", forState: UIControlState.Normal) // 丸の中にi (iOS 7.0 からDetailDisclosure, InfoLight, InfoDarkのデザインが同じものになっている) let button1 = UIButton.buttonWithType(UIButtonType.InfoLight) as UIButton self.addSubview(button1) let frame1 = CGRect(x: 0, y: 120, width: self.frame.width, height: 50) button1.frame = frame1 // 丸の中に+ let button2 = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton self.addSubview(button2) let frame2 = CGRect(x: 0, y: 170, width: self.frame.width, height: 50) button2.frame = frame2 } func buttonTouched(sender: UIButton) { //sender.center = CGPointMake(sender.center.x - 20, sender.center.y - 20) println("touched: \(sender)") } func buttonMoveDown(sender: UIButton) { sender.center = CGPointMake(sender.center.x + 5, sender.center.y + 5) sender.layer.shadowOpacity = 0.0 } func buttonMoveUp(sender: UIButton) { sender.center = CGPointMake(sender.center.x - 5, sender.center.y - 5) sender.layer.shadowOpacity = 0.5 } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
ViewController.swift
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = colorPattern.back() self.view.addSubview(UIButtonSample(frame: self.view.frame)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }