iOS swift5 微信朋友圈cell 微博的cell

文章目录

  • 1.代码
    • 1.1 FDMomentCell.swift
    • 1.2 FDMomentRootVC
    • 1.3 FDMomentRootVC
  • 2.照片选择器(我的另外一篇博客)
  • 3.图片浏览器

请添加图片描述

文章目录

  • 1.代码
    • 1.1 FDMomentCell.swift
    • 1.2 FDMomentRootVC
    • 1.3 FDMomentRootVC
  • 2.照片选择器(我的另外一篇博客)
  • 3.图片浏览器

1.代码

1.1 FDMomentCell.swift

import UIKitclass FDMomentCell: UITableViewCell {var imageUrlStr:[String] = ["lufei","lufei","lufei","lufei","lufei","lufei"]lazy var headImageView:UIImageView = {let imageView = UIImageView()imageView.image = UIImage(named: "lufei")imageView.layer.cornerRadius = 17.5imageView.layer.masksToBounds = truereturn imageView}()lazy var nameLabel:UILabel = {let label = UILabel()let attributedString = NSAttributedString(string: "爱吃咖喱的小麦", attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0x424242), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 13)!])label.attributedText = attributedStringreturn label}()lazy var timeLabel:UILabel = {let label = UILabel()let attributedString = NSAttributedString(string: "2小时前", attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0xa6a6a6), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 11)!])label.attributedText = attributedStringreturn label}()lazy var attentionBtn:UIButton = {let btn = UIButton()let attributedString = NSAttributedString(string: "已关注", attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0xB3B3B3), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 13)!])btn.setAttributedTitle(attributedString, for: .normal)btn.layer.cornerRadius = 13btn.layer.masksToBounds = truebtn.layer.borderWidth = 0.5btn.layer.borderColor = UIColor.hexColor(hexValue: 0xf0f0f0).cgColorreturn btn}()lazy var contentLabel:UILabel = {var str = "内容的测试\n"for i in 0...50{str += "内容的测试"}let label = UILabel()label.textAlignment = .leftlabel.numberOfLines = 5let attributedString = NSAttributedString(string: str, attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0x0f0f0f), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 15)!])label.attributedText = attributedStringreturn label}()let imageBtnBackView:UIView = {let view = UIView()return view}()var constraintTag = truefunc addImageBtn(){let count = imageUrlStr.countlet btnWidthAndHeight = (windowWidth - 45)/3let distance = btnWidthAndHeight + 7.5let totalList = (count-1)/3constraintTag = truefor i in 0..<count {let row = i%3let list = i/3let btn = UIButton(type: .custom)btn.backgroundColor = .greenbtn.tag = 100+ibtn.addTarget(self, action: #selector(imageBtnClick(btn:)), for: .touchUpInside)btn.setBackgroundImage(UIImage(named: imageUrlStr[i]), for: .normal)btn.layer.cornerRadius = 10btn.layer.masksToBounds = trueimageBtnBackView.addSubview(btn)btn.snp.makeConstraints { (make) inmake.top.equalToSuperview().offset(CGFloat(list) * distance)make.left.equalToSuperview().offset(CGFloat(row) * distance)make.width.height.equalTo(btnWidthAndHeight)if list == totalList {if constraintTag {make.bottom.equalToSuperview().offset(-10)constraintTag = false}}}}}@objc func imageBtnClick(btn: UIButton) {printXY(btn.tag, obj: self, line: #line)}lazy var lookTimeLabel:UILabel = {let label = UILabel()let attributedString = NSAttributedString(string: "5198次观看", attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0xbfbfbf), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 11)!])label.attributedText = attributedStringreturn label}()lazy var likeBtn:UIButton = {let btn = UIButton()let attributedString = NSAttributedString(string: " 152", attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0x808080), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 11)!])btn.setAttributedTitle(attributedString, for: .normal)btn.setImage(UIImage(named: "icon_home_like"), for: .normal)return btn}()lazy var commentBtn:UIButton = {let btn = UIButton()let attributedString = NSAttributedString(string: " 11", attributes: [NSAttributedString.Key.foregroundColor : UIColor.hexColor(hexValue: 0x808080), NSAttributedString.Key.font : UIFont.init(name: "PingFang SC", size: 11)!])btn.setAttributedTitle(attributedString, for: .normal)btn.setImage(UIImage(named: "icon_home_comment"), for: .normal)return btn}()override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {super.init(style: style, reuseIdentifier: reuseIdentifier)setupUI()}func setupUI(){contentView.addSubview(headImageView)headImageView.snp.makeConstraints { (make) inmake.width.height.equalTo(35)make.top.equalToSuperview().offset(10)make.left.equalToSuperview().offset(17.5)}contentView.addSubview(nameLabel)nameLabel.snp.makeConstraints { (make) inmake.top.equalTo(headImageView).offset(2.5)make.left.equalTo(headImageView.snp_right).offset(5)}contentView.addSubview(timeLabel)timeLabel.snp.makeConstraints { (make) inmake.left.equalTo(nameLabel)make.top.equalTo(nameLabel.snp_bottom).offset(5)}contentView.addSubview(attentionBtn)attentionBtn.snp.makeConstraints { (make) inmake.right.equalToSuperview().offset(-15)make.top.equalToSuperview().offset(24.5)make.width.equalTo(63)make.height.equalTo(26)}contentView.addSubview(contentLabel)contentLabel.snp.makeConstraints { (make) inmake.top.equalTo(headImageView.snp_bottom).offset(15)make.left.equalToSuperview().offset(15)make.right.equalToSuperview().offset(-15)}contentView.addSubview(imageBtnBackView)imageBtnBackView.snp.makeConstraints { (make) inmake.top.equalTo(contentLabel.snp_bottom).offset(10)make.left.equalToSuperview().offset(15)make.right.equalToSuperview().offset(-15)}addImageBtn()contentView.addSubview(lookTimeLabel)lookTimeLabel.snp.makeConstraints { (make) inmake.left.equalTo(imageBtnBackView)make.top.equalTo(imageBtnBackView.snp_bottom)}contentView.addSubview(likeBtn)likeBtn.snp.makeConstraints { (make) inmake.right.equalTo(imageBtnBackView)make.top.equalTo(imageBtnBackView.snp_bottom)make.height.equalTo(25)make.width.equalTo(50)}contentView.addSubview(commentBtn)commentBtn.snp.makeConstraints { (make) inmake.right.equalTo(likeBtn.snp_left).offset(-15)make.top.equalTo(imageBtnBackView.snp_bottom)make.height.equalTo(25)make.width.equalTo(50)make.bottom.equalToSuperview().offset(-23)}}required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}

1.2 FDMomentRootVC

import UIKitclass HomeVC: FDMomentRootVC {override func viewDidLoad() {super.viewDidLoad()title = "首页"}}extension HomeVC {override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {5}override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell:FDMomentCell = tableView.dequeueReusableCell(withIdentifier: "FDMomentCell", for: indexPath) as! FDMomentCellreturn cell}
}

1.3 FDMomentRootVC

import UIKit
import MJRefreshclass FDMomentRootVC: UIViewController {/// 下拉刷新的控件lazy var refreshHeader: MJRefreshNormalHeader = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadData))lazy var refreshFooter: MJRefreshAutoNormalFooter = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadData))/// tableview的懒加载属性lazy var tableView: UITableView = {let tableView = UITableView(frame: CGRect.zero)tableView.delegate = selftableView.dataSource = self//自适应行高tableView.estimatedRowHeight = 200tableView.rowHeight = UITableView.automaticDimensiontableView.register(FDMomentCell.self, forCellReuseIdentifier: "FDMomentCell")
//        tableView.mj_header = self.refreshHeader
//        tableView.mj_footer = self.refreshFooterreturn tableView}()override func viewDidLoad() {super.viewDidLoad()view.addSubview(tableView)tableView.snp.makeConstraints { (make) inmake.top.equalTo(self.view)make.left.right.equalTo(self.view)make.bottom.equalTo(self.view)}}}// 数据加载
extension  FDMomentRootVC {@objc func loadData() {}
}extension FDMomentRootVC: UITableViewDelegate, UITableViewDataSource {func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return 5}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {return UITableViewCell()}}

2.照片选择器(我的另外一篇博客)

ios HXPhotoPicker 图片视频选择器

3.图片浏览器

XLPhotoBrowser - github

  • 用pod可能会因为SDWebImage的版本问题而出错,所以手动导入,将github示例中的下面的文件夹拖进项目里,在桥接文件中引入头文件
  • SDWebImage可以用pod引入
    请添加图片描述
  • 下面的博客中有框架的示例图

iOS图片浏览器 - XLPhotoBrowser(类似微信多图片浏览效果)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部