Swift5.x开发微博项目之访客视图第2部分

Swift5.x开发微博项目之访客视图第2部分

本项目有点bug ,文字的高度,没有添加好宽高的约束。其他正常
在这里插入图片描述
代码太多,我只附着核心源码,项目源码在我的主页下面,进行代码的重构务必备份一份。



import UIKit

// MARK: - 处理用户未登录的界面显示
class VisitorView: UIView{
    
    
    //MARK: - 设置视图信息
    /*
     图片名称 首页设置为nil
     消息文字
     **/
    func setupInfo(imageName: String?,title: String){
        messageLabel.text = title
        //如果图片名称为nil 说明是首页 直接返回
        guard let imgName = imageName else {
            //播放动画
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        //隐藏小房子
        homeIconView.isHidden = true
        //将遮罩图片移动到底层
        sendSubviewToBack(maskIconView)
    }
    //开启首页转轮动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //用在不断重复的动画上。当动画绑定的图层对应的视图被销毁 ,动画会自动被销毁
        anim.isRemovedOnCompletion = false //离开动画不删除
        // 添加到谁身上
        iconView.layer.add(anim, forKey: nil)
    }
    
    //initWithFrame 是 UIView 的指定构造函数
    // 使用纯代码开发入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //initWithCoder - 使用SB & XIB 开发加载的函数
    //使用sb开始的入口  抽取公共函数,扔到 两个地方都调用
    required init?(coder: NSCoder) {
        //导致使用sb开发 调用这个视图  会直接崩溃
        //重要提示  想用 storyboard 开发 ,只要把这句话注释掉就可以了   // fatalError  调用初始函数
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: coder)
        setupUI()
    }
    
    //MARK: - 懒加载控件
    //图标  使用Image 构造函数 imageView 默认就是image大小
    //图标
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩图片
    private lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
    private lazy var homeIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息文字
    private lazy var messageLabel : UILabel = {
        let label = UILabel ()
        label.text = "关注一些人,回这里看看有什么惊喜"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    //注册按钮
    private lazy var registerButton : UIButton = {
        
        let button = UIButton ()
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        // button.setBackgroundImage(UIImage(named: ""), for: .highlighted)
        return button
    }()
    //登录按钮
    private lazy var loginButton : UIButton = {
        
        let button = UIButton ()
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        // button.setBackgroundImage(UIImage(named: ""), for: .highlighted)
        return button
    }()
    
}
extension VisitorView {
    
    //设置界面
    private func setupUI(){
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加自动布局
        /*
         - 添加约束要添加到父视图上
         - 建议 子视图最好有一个统一的参照物
         */
        //translatesAutoresizingMaskIntoConstraints 默认是true 支持使用setFrame 的方式设置控件位置
        //false 支持使用自动布局来设置控件的位置
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, 
                                         attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self,
                                         attribute: .centerY, multiplier: 1.0, constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView,
                                         attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView,
                                         attribute: .centerY, multiplier: 1.0, constant: 0))
        // 3>消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView,
                                         attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView,
                                         attribute: .bottom, multiplier: 1.0, constant: 16))
        // 3>消息文字宽高
        /* addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil,
         attribute: .notAnAttribute, multiplier: 1.0, constant: 224.0))
         addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: nil,
         attribute: .notAnAttribute, multiplier: 1.0, constant: 36.0))*/
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: messageLabel,
                                         attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel,
                                         attribute: .bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil,
                                         attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil,
                                         attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        // 4> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messageLabel,
                                         attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: messageLabel,
                                         attribute: .bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil,
                                         attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil,
                                         attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        //5遮罩图片
        /*
         VFL 可视化格式语言
         H 水平
         V 垂直方向
         | 边界
         [] 包装控件
         view是一个字典。【名字:控件名】 VFL字符串中表示的控件的字符串
         metrics 是一个字典 VFL字符串中表示的 表示某一个数值
         **/
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight":-36], views: ["mask": maskIconView,"regButton": registerButton]))
        //设置背景颜色。 灰度图。 R = G = B 在UI 元素中。 大多数都使用灰度图。或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
    
}





import UIKit


class VisitorTableViewController: UITableViewController {
    //用户登录标记
   private var userLogin = false
   // private var userLogin = true
    //访客视图
    var visitorView: VisitorView?  //不能使用懒加载,会有内存消耗问题
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func loadView() {
        // print("comeHere")
        //        super.loadView()
        //根据用户登陆情况  决定显示的根视图
        userLogin ? super.loadView() : setupVisitorView()
        //view = UIView()
        //view.backgroundColor = UIColor.orange
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
       // print(visitorView)
    }
    
    //设置访客视图
    private func setupVisitorView() {
        //替换根视图
        visitorView = VisitorView()
        //     view.backgroundColor = UIColor.orange
        view = visitorView
    }
    
    
}

// MARK: - 访客视图监听方法
extension VisitorTableViewController {
    
    
}


import UIKit
// MARK:- 程序入口
class MainViewController: UITabBarController {
    //MARK: - 监听方法
    //点击撰写按钮 加 private 运行循环无法正确发送消息导致崩溃  报错
    //如果使用@objc 修饰符号 可以保证运行循环能够发送此消息 即使这个函数被标记为private
     @objc private func clickComposedButton(){
       print("点击了")

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.addChildViewControllers()
        setupComposedButton()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //将撰写按钮弄到最前面
        //会创建tabBar中的所有控件器对应的按钮
        tabBar.bringSubviewToFront(composedButton)
    }
    //MARK 懒加载控件//闭包
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

}
//MARK: -设置界面
extension MainViewController {
    //设置撰写按钮
    private func setupComposedButton(){
        tabBar.addSubview(composedButton)
        // 2 调整按钮位置
        let count = children.count
        //print("children 的数量是  \(count)") //children 的数量是  5
        //让按钮宽一点点  能够解决手指触摸容错的问题
         let w = tabBar.bounds.width / CGFloat(count) - 1
        composedButton.frame = tabBar.bounds.insetBy(dx: 2 * w,dy: 0) //缩放按钮,左右,上下缩放, 水平缩放 2个w
        //3. 添加监听方法
        composedButton.addTarget(self, action: #selector(clickComposedButton), for: .touchUpInside)
    }
    private func addChildViewControllers(){
        // 设置tintColor -图片渲染颜色
        tabBar.tintColor = UIColor.orange
        self.addChildViewController(vc: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        self.addChildViewController(vc: MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChild(UIViewController())
        self.addChildViewController(vc: DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        self.addChildViewController(vc: ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController,
                                        title: String,
                                        imageName: String){
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChild(nav)
    }
    private func addChildViewController(){
        let vc = HomeTableViewController()
        vc.title = "首页"
        vc.tabBarItem.image = UIImage(named: "tabbar_home")
        let nav = UINavigationController(rootViewController: vc)
        addChild(nav)
    }
}

import UIKit

class DiscoverTableViewController: VisitorTableViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
//             visitorView?.setupInfo(imageName: nil, title: "关注一些人,回这里看看有什么惊喜")
             visitorView?.setupInfo(imageName: "visitordiscover_image_message", title: "登录后,最新最热微博尽在掌握会与")
        }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值