class XFJLoginViewController: XFJBaseViewController {
/// 登录时候转圈提示
@IBOutlet weak var activityView: UIActivityIndicatorView!
/// 登录的view
@IBOutlet weak var loginView: UIView!
/// 密码文本
@IBOutlet weak var passWordTextField: UITextField!
/// 账号文本
@IBOutlet weak var countTextField: UITextField!
//自动登录
@IBOutlet weak var autoLogin: UIButton!
//记住密码
@IBOutlet weak var remberPassword: UIButton!
}
//MARK: - 判断账号和密码是否为空和密码错误
extension XFJLoginViewController {
@IBAction func loginBtnClick() { //通过对按钮拖线
//获取账号和密码
let countTF = countTextField.text
let passwordTF = passWordTextField.text
//判断内容为空是否登录
if countTF?.isEmpty == true || passwordTF?.isEmpty == true {
showError("密码或账号不能为空")
//为空的话,后面就没必要执行了
return
}
//判断账号和面是否正确
activityView.startAnimating()
//转圈的时候停止交互
view.userInteractionEnabled = false
//延迟执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
//停止转圈
self.activityView.stopAnimating()
//允许交互
self.view.userInteractionEnabled = true
//判断账号和密码是否正确
if countTF == "123" && passwordTF == "123" {
//登录成功跳转下一个界面(这里采用修改根控制器的方法实现)
self.view.window?.rootViewController = XFJHomeViewController()
}else {
self.showError("账号或密码错误")
}
})
}
}
//MARK: - 处理登录的抖动情况
extension XFJLoginViewController {
//登录时候的抖动效果和弹框提示
private func showError (messaage : String) {
let alertControler = UIAlertController(title: "登录失败", message: messaage, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "确认", style: .Cancel, handler: nil)
//添加
alertControler.addAction(alertAction)
//弹出框
presentViewController(alertControler, animated: true, completion: nil)
//登录时候的抖动效果
let shakeAnimted = CAKeyframeAnimation(keyPath: "transform.translation.x")
shakeAnimted.repeatCount = 3
shakeAnimted.duration = 0.1
shakeAnimted.values = [-10,0,10]
loginView.layer.addAnimation(shakeAnimted, forKey: nil)
}
}
//MARK: - 记住密码
extension XFJLoginViewController {
@IBAction func remberBtnClick(button : UIButton) { //需要通过连线拿到
//反选中
button.selected = !button.selected
if button.selected == false {
autoLogin.selected = false
}
}
}
//MARK: - 自动登录
extension XFJLoginViewController {
@IBAction func automaticLogin(button : UIButton) { //需要通过连线拿到
//反选中
button.selected = !button.selected
if button.selected == true {
remberPassword.selected = true
}
}
}
//对键盘的处理(NEXT和Down)----> 需要在storyboard中设置代理否则是下面代码是没用的
extension XFJLoginViewController : UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
//判断
if textField == countTextField {
//如果判断正确,让密码键盘成为第一响应者
passWordTextField.becomeFirstResponder()
}else if passWordTextField == textField {
//如果密码相等,调用登录方法
loginBtnClick()
}
return true
}
}
class XFJBaseViewController: UIViewController {
//设置状态栏的样式
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
//懒加载
private lazy var dock : XFJDock = {
//创建dock
let dock = XFJDock()
//添加dock到view中
self.view.addSubview(dock)
//设置高度
dock.frame.size.height = self.view.frame.height
//设置autoresizing跟随父控件的拉伸而拉伸
dock.autoresizingMask = .FlexibleHeight
return dock
}()
//懒加载底部工具栏
private lazy var bottomMenu : XFJBottom = {
//创建底部工具栏
let bottomMenu = XFJBottom()
//将创建好的底部工具条添加到view中
self.addSubview(bottomMenu)
//设置子控件跟随父控件的顶部拉伸而拉伸
bottomMenu.autoresizingMask = .FlexibleTopMargin
return bottomMenu
}()
//横屏
let kDockLaspaceWidth : CGFloat = 270.0
//竖屏
let kDockProtraitWidth : CGFloat = 70.0
//item的高度
let kDockLaspaceHeight : CGFloat = 70.0
//头像横屏时候的高度
let kIconButtonLaspaceHeight : CGFloat = kIconButtonLaspaceWidth + kIconTextHeight
//头像横屏时候的宽度
let kIconButtonLaspaceWidth : CGFloat = 90.0
//头像竖屏时候的高度和宽度
let kIconProtraitWH : CGFloat = 60
//头像横屏和竖屏的时候y值一样
let kIconLpaceY : CGFloat = 40.0
//头像文字的高度
let kIconTextHeight : CGFloat = 30.0
//当屏幕即将旋转的时候调用,其实并没有旋转
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
//判断当前屏幕的true还是false
let isLaspace = size.width > size.height
//设置dock的宽度
let duration = coordinator.transitionDuration()
UIView.animateWithDuration(duration) { () -> Void in
//将屏幕方向传入dock中
self.dock.roteLspaces(isLaspace)
//更新旋转后的x值
self.contentive?.frame.origin.x = self.dock.frame.width
}
}
//懒加载底部工具栏
private lazy var bottomMenu : XFJBottom = {
//创建底部工具栏
let bottomMenu = XFJBottom()
//将创建好的底部工具条添加到view中
self.addSubview(bottomMenu)
//设置子控件跟随父控件的顶部拉伸而拉伸
bottomMenu.autoresizingMask = .FlexibleTopMargin
return bottomMenu
}()
//提供一个方法,让外界将屏幕当前的状态传进来
func rateLaspace(isLaspace : Bool) {
//取出按钮总数
let count = subviews.count
//设置自身的宽度
self.frame.size.width = (superview?.frame.width)!
//根据传入的屏幕方法设置高度
self.frame.size.height = isLaspace ? kDockLaspaceHeight : kDockLaspaceHeight * CGFloat(count)
//y值
self.frame.origin.y = (superview?.frame.height)! - frame.size.height
//遍历
for var i = 0 ; i < count ;i++ {
//根据角标取出按钮
let item = subviews[i]
//设置按钮的尺寸
item.frame.size.width = isLaspace ? frame.width/CGFloat(count) : frame.width
item.frame.size.height = kDockLaspaceHeight
item.frame.origin.x = isLaspace ? CGFloat(i) * item.frame.size.width : 0
item.frame.origin.y = isLaspace ? 0 : CGFloat(i) * item.frame.size.width
}
}
override init(frame: CGRect) {
super.init(frame: frame)
//初始化子控件
setUpButton("tabbar_blog",type: .blog)
setUpButton("tabbar_mood",type: .mood)
setUpButton("tabbar_photo",type: .photo)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//给类扩充一个方法,用来创建子控件数
extension XFJBottom {
@objc private func setUpButton(imageName : String, type : ButtomMenuType ) {
//创建按钮
let iconButton = UIButton()
//绑定tag,方便点击按钮的时候可以传入对应的值
iconButton.tag = type.rawValue
//添加按钮
addSubview(iconButton)
//设置按钮图片(普通图片)
iconButton.setImage(UIImage(named: imageName), forState: .Normal)
//设置高亮图片
iconButton.setBackgroundImage(UIImage(named: "tabbar_separate_selected_bg"), forState: .Highlighted)
//监听按钮点击(这是oc的方法,一定要加上@objc)
iconButton.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
}
}
//懒加载中间工具栏
private lazy var middleMenu : XFJTabBar = {
//创建中间工具栏
let middleMenu = XFJTabBar()
//添加
self.addSubview(middleMenu)
//设置中间工具栏底部不变,顶部随着父控件拉伸而拉伸
middleMenu.autoresizingMask = .FlexibleTopMargin
//返回
return middleMenu
}()
//根据外界传入的屏幕方向,计算高度和宽度
func roteTabLaspace(isLaspace : Bool) {
//中间部分的子控件总数
let count = subviews.count
//宽度跟随父控件
frame.size.width = (superview?.frame.width)!
//高度
frame.size.height = kDockLaspaceHeight * CGFloat(count)
//遍历
for var i = 0 ; i < count ; i++ {
//取出item
let item = subviews[i]
//设置尺寸
item.frame.size.width = (superview?.frame.width)!
item.frame.size.height = kDockLaspaceHeight
item.frame.origin.x = 0
item.frame.origin.y = item.frame.size.height * CGFloat(i)
}
}
//重写initWithFrame
override init(frame: CGRect) {
super.init(frame: frame)
//添加标题
setUpTabBarItem(imageName: "tab_bar_feed_icon", title: "全部动态")
setUpTabBarItem(imageName: "tab_bar_passive_feed_icon", title: "与我相关")
setUpTabBarItem(imageName: "tab_bar_pic_wall_icon", title: "照片墙")
setUpTabBarItem(imageName: "tab_bar_e_album_icon", title: "电子相框")
setUpTabBarItem(imageName: "tab_bar_friend_icon", title: "好友")
setUpTabBarItem(imageName: "tab_bar_e_more_icon", title: "其它")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: - 添加中间的子标题
extension XFJTabBar {
private func setUpTabBarItem(imageName imageName : String, title : String) {
//创建按钮
let button = XFJItemBtn()
//绑定tag
button.tag = subviews.count
//添加按钮
addSubview(button)
//设置普通按钮
button.setTitle(title, forState: .Normal)
//设置普通图片
button.setImage(UIImage(named: imageName), forState: .Normal)
//设置高亮图片
button.setBackgroundImage(UIImage(named: "tabbar_separate_selected_bg"), forState: .Selected)
//监听按钮
button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchDown)
}
}
//定义一个属性,用来记录按钮点击的状态
private var selectButton = UIButton?()
/MARK: - 实现事件监听的方法
extension XFJTabBar {
@objc private func buttonClick(button : UIButton) {
//如果selectButton?.ta有值就用它的值,否则就用0
let tag = selectButton?.tag ?? 0
//实现代理方法
tabBarItemDelegate?.XFJTabBarItem!(tag, to: button.tag)
selectButton?.selected = false
selectButton = button
selectButton?.selected = true
}
}
- (void)setHighlighted:(BOOL)highlighted{
}
class XFJItemBtn: XFJCustomBtn {
//定义一个属性用来计算图片占的比例
var ratio : CGFloat = 0.4
override init(frame: CGRect) {
super.init(frame: frame)
//仅仅让图片高亮
adjustsImageWhenHighlighted = false
//设置图片居中
imageView?.contentMode = .Center
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//MARK: - 按钮标题
extension XFJItemBtn {
override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
if frame.height == frame.width { //竖屏
return CGRectZero
}else{//横屏
let width : CGFloat = frame.width * (1 - ratio)
let height : CGFloat = frame.height
let x : CGFloat = frame.width * ratio
let y : CGFloat = 0
return CGRectMake(x, y, width, height)
}
}
}
//MARK: - 按钮图片
extension XFJItemBtn {
override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
if frame.height == frame.width { //竖屏
return bounds
}else{//横屏
let width : CGFloat = frame.width * ratio
let height : CGFloat = frame.height
let x : CGFloat = 0.0
let y : CGFloat = 0.0
return CGRectMake(x, y, width, height)
}
}
}
//懒加载头像
private lazy var iconButton : XFJIconButton = {
//创建头像
let iconButton = XFJIconButton()
//添加头像
self.addSubview(iconButton)
//
//返回
return iconButton
}()
func roteToLapace(isLapace : Bool) {
//icon的高度
frame.size.height = isLapace ? kIconButtonLaspaceHeight : kIconProtraitWH
frame.size.width = isLapace ? kIconButtonLaspaceWidth : kIconProtraitWH
frame.origin.y = kIconLpaceY
frame.origin.x = ((superview?.frame.width)! - frame.width) * 0.5
self.isLapace = isLapace
}
//重写init方法
override init(frame: CGRect) {
super.init(frame: frame)
//设置头像图片
setImage(UIImage(named: "icon"), forState: .Normal)
//设置文字
setTitle("不良少年J", forState: .Normal)
//设置文字显示位置
titleLabel?.textAlignment = .Center
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: - 头部按钮的图片
extension XFJIconButton {
override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
if frame.width == frame.height { //竖屏
return bounds
}else{//横屏
let width : CGFloat = kIconButtonLaspaceWidth
let height : CGFloat = kIconButtonLaspaceHeight
let x : CGFloat = 0.0
let y : CGFloat = 0.0
return CGRectMake(x, y, width, height)
}
}
}
//MARK: - 按钮的标题
extension XFJIconButton {
override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
if frame.width == frame.height {//竖屏
return CGRectZero
}else{//横屏
let width : CGFloat = kIconButtonLaspaceWidth
let height : CGFloat = kIconTextHeight
let x : CGFloat = 0.0
let y : CGFloat = kIconButtonLaspaceHeight
return CGRectMake(x, y, width, height)
}
}
}
//MARK : - 定义枚举
@objc enum ButtomMenuType : Int {
case blog
case mood
case photo
}
//MARK : - 定义协议
@objc protocol XFJBottomDelegate : NSObjectProtocol {
//可实现的
optional func XFJBottomMenuClickItem(menu : XFJBottom, type : ButtomMenuType)
}
//设置代理属性
weak var BottomMenuDelegate :XFJBottomDelegate?
//监听按钮点击(这是oc的方法,一定要加上@objc)
iconButton.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
//MARK: - 点击底部按钮,将相应的值传入协议方法中
extension XFJBottom {
@objc private func buttonClick(button : UIButton) {
BottomMenuDelegate?.XFJBottomMenuClickItem!(self, type: ButtomMenuType(rawValue: button.tag)!)
}
}
//设置底部工具条的代理
dock.getBottomMenu.BottomMenuDelegate = self
//MARK : - 实现底部协议的方法
extension XFJHomeViewController {
func XFJBottomMenuClickItem(menu: XFJBottom, type: ButtomMenuType) {
switch type {
case .blog :
print("点击了blog")
case .mood :
//创建控制器
let moodVC = XFJMoodViewController()
//将该控制器作为导航控制器的根控制器
let nav = UINavigationController(rootViewController: moodVC)
//设置控制器样式
nav.modalPresentationStyle = .FormSheet
//model
presentViewController(nav, animated: true, completion: nil)
print("点击了mood")
case .photo :
print("点击了photo")
}
}
}
//MARK : - 添加子控制器
extension XFJHomeViewController {
private func setUpAllChildController() {
//全部动态
let allDynamicStateController = XFJAllSegViewController()
allDynamicStateController.view.backgroundColor = UIColor.blueColor()
setUpNavController(allDynamicStateController)
//与我相关
let andMeCorrelationController = UIViewController()
andMeCorrelationController.view.backgroundColor = UIColor.redColor()
andMeCorrelationController.title = "与我相关"
setUpNavController(andMeCorrelationController)
//照片墙
let photoWallController = UIViewController()
photoWallController.view.backgroundColor = UIColor.yellowColor()
photoWallController.title = "照片墙"
setUpNavController(photoWallController)
//电子相框
let electronRahmenController = UIViewController()
electronRahmenController.view.backgroundColor = UIColor.purpleColor()
electronRahmenController.title = "电子相框"
setUpNavController(electronRahmenController)
//好友
let friendsController = UIViewController()
friendsController.view.backgroundColor = UIColor.orangeColor()
friendsController.title = "好友"
setUpNavController(friendsController)
//其它
let otherController = UIViewController()
otherController.view.backgroundColor = UIColor.whiteColor()
otherController.title = "其它"
setUpNavController(otherController)
//添加一个头像子控制器
let iconController = UIViewController()
iconController.view.backgroundColor = UIColor.brownColor()
iconController.title = "个人中心"
setUpNavController(iconController)
}
}
//MARK : - 定义一个方法用来设置导航控制器
extension XFJHomeViewController {
private func setUpNavController(root : UIViewController){
let nav1 = UINavigationController(rootViewController: root)
addChildViewController(nav1)
}
}
//MARK : - 设置内容的view
extension XFJHomeViewController {
private func setUpContentView() {
//判断是否子控制器存在,如果存在就直接返回
guard childViewControllers != [] else{
return
}
//创建一个属性
let contentive = UIView()
//添加contentView
view.addSubview(contentive)
//设置子控制器的view尺寸
contentive.frame.origin.x = dock.frame.width
contentive.frame.origin.y = 20.0
contentive.frame.size.height = view.frame.height - 20.0
//高度跟随父控件拉伸而拉伸
contentive.autoresizingMask = .FlexibleHeight
//用宽度和高度取出里面最小的
let ProtraitWidth = min(view.frame.height, view.frame.width)
//计算子控制器的宽度
contentive.frame.size.width = ProtraitWidth - kDockProtraitWidth
//赋值(用于屏幕旋转的时候更新x值)
self.contentive = contentive
}
}
//MARK: - 协议方法
@objc
protocol XFJTabBarDelegate : NSObjectProtocol {
//这里用两个参数的原因是由于选中状态和没有选中状态需要切换,那么需要将不同的tag传入其中,才能令对应的控制器切换
optional func XFJTabBarItem(from : Int, to : Int)
}
//定义代理
weak var tabBarItemDelegate : XFJTabBarDelegate?
//实现代理方法
tabBarItemDelegate?.XFJTabBarItem!(tag, to: button.tag)
//设置中间部分工具条的代理
dock.getTabBarItem.tabBarItemDelegate = self
//MARK: - 实现中间部分协议方法
extension XFJHomeViewController : XFJTabBarDelegate{
func XFJTabBarItem(from: Int, to: Int) {
//根据传入的参数,取出上移个view
let previousController = childViewControllers[from]
//移除上一个控制器的view
previousController.view.removeFromSuperview()
//取出下一个控制器的view
let nextVC = childViewControllers[to]
//将切换的子控制器的view添加到contentView中
contentive?.addSubview(nextVC.view)
//计算nextVC的尺寸
nextVC.view.frame = (contentive?.bounds)!
//下一个按钮的角标
selectButtonIndex = to
}
}
//监听头像点击
dock.getIcon.addTarget(self, action: "iconButtonClick", forControlEvents: .TouchUpInside)
//MARK : - 实现头像监听方法
extension XFJHomeViewController {
@objc private func iconButtonClick() {
//调用点击中间部分的内容
XFJTabBarItem(selectButtonIndex, to: childViewControllers.count - 1)
//调用取消选中状态
dock.getTabBarItem.unSelecButton()
}
}
//对底部工具栏提供只读方法
var getBottomMenu : XFJBottom {
get{
return bottomMenu
}
}
//对中间工具条提供只读的方法
var getTabBarItem : XFJTabBar {
get{
return middleMenu
}
}
//给头像按钮提供一个只读的属性,让外界可以拿到iconButton按钮
var getIcon : XFJIconButton {
get{
return iconButton
}
}
//设置底部工具条的代理
dock.getBottomMenu.BottomMenuDelegate = self
//设置中间部分工具条的代理
dock.getTabBarItem.tabBarItemDelegate = self
//监听头像点击
dock.getIcon.addTarget(self, action: "iconButtonClick", forControlEvents: .TouchUpInside)
//提供一个方法取消选中状态(由外界调用)
func unSelecButton() {
selectButton?.selected = false
}
//设置一个属性当前按钮角标
private var selectButtonIndex : Int = 0
//MARK : - 实现头像监听方法
extension XFJHomeViewController {
@objc private func iconButtonClick() {
//调用点击中间部分的内容
XFJTabBarItem(selectButtonIndex, to: childViewControllers.count - 1)
//调用取消选中状态
dock.getTabBarItem.unSelecButton()
}
}
class XFJAllSegViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//设置背景颜色
view.backgroundColor = UIColor.whiteColor()
//创建seg
let seg = UISegmentedControl(items: ["全部","特别关心","好友动态","认证空间"])
//添加
navigationItem.titleView = seg
//设置颜色
seg.tintColor = UIColor.grayColor()
//设置默认选中第0个
seg.selectedSegmentIndex = 0
//设置选中文字
seg.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: .Normal)
//监听点击事件
seg.addTarget(self, action: "segClick", forControlEvents: .TouchUpInside)
}
}
class XFJMoodViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//设置标题
self.title = "发表说说"
//设置颜色
view.backgroundColor = UIColor.redColor()
//设置导航条左边的内容
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "exit")
//设置导航条右边的内容
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发表", style: .Plain, target: self, action: "deliver")
}
}
//Mark : - dismiss控制器
extension XFJMoodViewController {
@objc private func exit() {
dismissViewControllerAnimated(true, completion: nil)
}
}
原文:http://blog.csdn.net/xf931456371/article/details/51311171