// 绑定事件
cell.privacySwitch.addTarget(self, action: #selector(RSMeSettingPrivacyViewController.switchTapped(_:)), for: UIControl.Event.valueChanged) @objc func switchTapped(_ sender: UISwitch) {
print(sender.isOn)
} // 跳转页面
var targetVc: UIViewController
targetVc = targetViewController()
navigationController?.pushViewController(targetVc, animated: true) // 导入指定cell
if cell == nil {
cell = Bundle.main.loadNibNamed("cell", owner: nil, options: nil)?.first
} // 隐藏 tableHeaderView
tableView.tableHeaderView?.removeFromSuperview()
tableView.tableHeaderView = UIView(frame: CGRect(x: , y: , width: , height: CGFloat.leastNonzeroMagnitude)) // 隐藏 presentView
self.presentingViewController?.dismiss(animated: true, completion: nil) // 创建 button
lazy private var logoutButton: UIButton = {
let logoutButton = UIButton()
logoutButton.setTitle("退出登录", for: .normal)
logoutButton.titleLabel?.font = UIFont.systemFont(ofSize: )
// 文字加粗
// submitButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
logoutButton.setTitleColor(UIColor.rs_purple_8127fd, for: .normal)
logoutButton.addTarget(self, action: #selector(logoutButtonTapped), for: UIControl.Event.touchUpInside) // 添加阴影
logoutButton.layer.shadowOpacity = 0.8
logoutButton.layer.showColor = UIColor.black.cgColor
logoutButton.layer.shadowOffset = CGSize(width: , height: )
logoutButton.clipsToBounds = false // 这行一定要加,而且得加载最后,不然阴影没有效果
return logoutButton
}() // 在 viewDidLoad() 中
view.addSubview(logoutButton)
logoutButton.snp.makeConstraints { make in
make.height.equalTo()
make.width.equalTo()
make.bottom.equalTo(self.bottomLayoutGuide.snp.top).offset(-)
make.centerX.equalToSuperview()
} // alert 弹框
let alertController = UIAlertController(title: "提示", message: "xxx", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { action in
cell.privacySwitch.setOn(true, animated: true) })
let okAction = UIAlertAction(title: "好的", style: .default, handler: { action in
cell.privacySwitch.setOn(false, animated: true) })
alertController.addAction(cancelAction)
alertController.addAction(okAction)
// 显示 alert
self.present(alertController, animated: true, completion: nil) //action sheet
lazy var actionSheet: UIAlertController = {
let actionSheet = UIAlertController(title: "提示", message: "some info...",
preferredStyle: .actionSheet)
let muteAction = UIAlertAction(title: "不看", style: .default, handler: nil)
let unFollowAction = UIAlertAction(title: "取消关注", style: .default, handler: nil)
let deleteAction = UIAlertAction(title: "举报", style: .destructive, handler: nil)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
actionSheet.addAction(muteAction)
actionSheet.addAction(unFollowAction)
actionSheet.addAction(deleteAction)
actionSheet.addAction(cancelAction)
return actionSheet
}() // 显示 action sheet
self.present(self.actionSheet, animated: true, completion: nil) // 导航栏添加按钮
let submitButton = UIBarButtonItem(title: "提交", style: .plain, target: self, action: #selector(submitButtonTapped))
self.navigationItem.rightBarButtonItem = submitButton @objc func submitButtonTapped() {
print("点击了提交...")
} // 导航栏按钮禁用
self.navigationItem.rightBarButtonItem?.isEnabled = false // 隐藏导航栏
self.navigationController?.setNavigationBarHidden(true, animated: true) // textView 限制字数
lazy private var textView: UITextView = {
let textView = UITextView(frame: CGRect.zero)
textView.delegate = self
textView.text = "输入文字..."
return textView
}() extension RSSettingFeedbackViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
// 限制字数
if textView.text.count >= {
textView.text = String(textView.text.prefix())
}
}
} // 拍照或从图库选择图片
// 上传照片
// uploadButton action: uploadButtonTapped
func uploadButtonTapped() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true var sourceType = UIImagePickerController.SourceType.camera
if !UIImagePickerController.isSourceTypeAvailable(sourceType) {
sourceType = UIImagePickerController.SourceType.photoLibrary
}
picker.sourceType = sourceType
self.present(picker, animated: true, completion: nil)
} // 获取当前拍摄或选择的照片
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let photo = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
let image = UIImageView.init(frame: self.view.frame)
image.image = photo
uploadButton.setImage(photo, for: UIControl.State.normal)
}
picker.dismiss(animated: true, completion: nil)
} // cancel后执行的方法
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
} // 打开连接(页面跳转)
let str = NSString(format: "itms-apps://itunes.apple.com/app/id%@?action=write-review", VendorConfig.tagAPPID)
UIApplication.shared.openURL(NSURL(string: str as String)! as URL) // 一个搜索框
lazy var textField: UITextField = {
let textField = UITextField(frame: CGRect.zero)
let leftView = UIImageView(image: UIImage(named: "release_search")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
textField.leftView = leftView
textField.leftViewMode = .always
leftView.contentMode = .center
leftView.frame = CGRect(x: , y: , width: , height: )
textField.clearButtonMode = .whileEditing
textField.placeholder = "搜索..."
textField.backgroundColor = UIColor.rs_gray_f2f2f2
textField.font = UIFont.rs_pingfangsc_regular(size: )
textField.cornerRadius =
textField.delegate = self
return textField
}() // 添加分隔线
let topLine = UIView()
topLine.backgroundColor = UIColor.rs_gray_d8d8d8 topLine.snp.makeConstraints { (make) in
make.left.equalTo()
make.right.equalTo(-)
make.top.equalTo(addressCell.snp.top).offset(-)
make.height.equalTo( / kScale)
} // tableview 去除边框线
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none // 添加阴影 —— xib 中添加 shadow
targetView.clipsToBounds = false // 根据屏幕宽度计算图片展示高度
let scale = finalImage.size.height / finalImage.size.width
self.collectionView.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.top.equalTo(self.tipLabel.snp.bottom).offset()
make.height.equalTo(self.view.snp.width).multipliedBy(scale)
} // 给视图添加手势(点击、双击)
// 单击切换音量控制
let singleTap = UITapGestureRecognizer(target: self, action: #selector(changeVolumeStatus))
singleTap.numberOfTapsRequired =
// 双击点赞
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTap.numberOfTapsRequired = // 优先检测双击手势
singleTap.require(toFail: doubleTap) // 将手势添加到视图
targetView.addGestureRecognizer(singleTap)
targetView.addGestureRecognizer(doubleTap) // 使用 AVPlayer 播放视频
import AVFoundation
/// 播放器
lazy var avplayLayer: AVPlayerLayer = {
let avplayLayer = AVPlayerLayer(player: nil)
avplayLayer.videoGravity = .resizeAspectFill
return avplayLayer
}() let url = URL(string: "http://pri-video.v.medlinker.net/5595b16d-72bc-4fcb-bef2-c01327abeab3/10.m3u8")
avplayLayer.player = AVPlayer(url: url!)
let scale = CGFloat(video.wide / video.high)
// 通过视频和容器尺寸计算最终展示尺寸
momentContentHeightConstraint?.update(offset: (kScreenWidth - ) / scale)
avplayLayer.frame = CGRect(x: , y: , width: kScreenWidth - , height: (kScreenWidth - ) / scale)
avplayLayer.player?.play() // tableView 设置背景 // 默认拉伸撑满 tableview
self?.tableView.backgroundView = UIImageView.init(image: UIImage(named: "no_feed")) // 图片原始尺寸位于 tableview 中央
let bg = UIImageView.init(image: UIImage(named: "no_feed"))
bg.contentMode = .center
self?.tableView.backgroundView = bg // 子线程执行代码
DispatchQueue.cwl_mainThreadAsync {
let cell = self.cacheCell as? RSSettingAccountCell
cell!.formItemValue.text = String(self.fileSizeOfCache()) + "Mb"
stopLoading()
let feedbackAlert = UIAlertController(title: nil, message: "清除成功!", preferredStyle: .alert)
self.present(feedbackAlert, animated: true, completion: nil)
Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.hideAlert), userInfo: nil, repeats: false)
} // tableview 隐藏滚动条
tableView.showsVerticalScrollIndicator = false

swift开发常用代码片段的更多相关文章

  1. 36个Android开发常用代码片段

    //36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...

  2. Android开发常用代码片段

    拨打电话 public static void call(Context context, String phoneNumber) { context.startActivity( new Inten ...

  3. C#常用代码片段备忘

    以下是从visual studio中整理出来的常用代码片段,以作备忘 快捷键: eh 用途: 类中事件实现函数模板 private void MyMethod(object sender, Event ...

  4. Vue3.0常用代码片段和开发插件

    Vue3 Snippets for Visual Studio Code Vue3 Snippets源码 Vue3 Snippets下载 This extension adds Vue3 Code S ...

  5. 56个PHP开发常用代码

    2016/02/14 6203 4    在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常 ...

  6. iOS开发-代码片段(Code Snippets)提高开发效率

    简介 在 XCode4 引入了一个新特性,那就是“代码片段(Code Snippets)”.对于一些经常用到的代码,抽象成模板放到 Code Snippets 中,使用的时候就只需要键入快捷键就可以了 ...

  7. Jquery学习总结(1)——Jquery常用代码片段汇总

    1. 禁止右键点击 ? 1 2 3 4 5 $(document).ready(function(){     $(document).bind("contextmenu",fun ...

  8. jQuery常用代码片段

    检测IE浏览器 在进行CSS设计时,IE浏览器对开发者及设计师而言无疑是个麻烦.尽管IE6的黑暗时代已经过去,IE浏览器家族的人气亦在不断下滑,但我们仍然有必要对其进行检测.当然,以下片段亦可用于检测 ...

  9. Swift开发常用知识点

    #pragma mark - as/类型转换as? / as! 需要根据前面的返回值决定 有?证明可选,可能为空:需要弱解包 没有?证明一定有值:大胆解包 as? 前面的结果是可选的 if let / ...

随机推荐

  1. TP框架代码学习 学习记录 3.2.3

    文件:think.class.php PHP提供register_shutdown_function()这个函数,能够在脚本终止前回调注册的函数,也就是当 PHP 程序执行完成后执行的函数.regis ...

  2. "Hello World!"团队第四次会议

    Scrum立会 博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 7.燃尽图 一.会议时间: 2017年10月16日  11:44-12:18 ...

  3. 软工1816 · Alpha冲刺(8/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员1(组长):王彬 过去两天完成了哪些任务 推进前后端各个接口的整合 学习jQuery基本语法,为beta阶段的商铺页面做准备 接下 ...

  4. CodeForces 57C Array 组合计数+逆元

    题目链接: http://codeforces.com/problemset/problem/57/C 题意: 给你一个数n,表示有n个数的序列,每个数范围为[1,n],叫你求所有非降和非升序列的个数 ...

  5. Hexo博客搭建全解

    [原创,转载请附网址:http://dongshuyan.top] 欢迎来到莫与的博客,第一篇记录了一下怎么写一篇博客,以方便之后写博客~ #从配置说起下载安装Git与Node.js略过 1.安装he ...

  6. erlang调优方法

    1. 来自Scaling Erlang的方法 内核调优: # Increase the ipv4 port range: sysctl -w net.ipv4.ip_local_port_range= ...

  7. 201621123037 《Java程序设计》第12周学习总结

    作业12-流与文件 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 答: 读取操作 从文件中读取: 1.字节流 InputStr ...

  8. 【Linux 命令】- tar 命令

    语法 tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <区块数目>][-C <目的目录>][-f <备份文件>][-F <Sc ...

  9. LoadRunner脚本增强技巧之手动关联

    个人认为LoadRunner的手动关联技巧可以看做参数化的一种,区别在与获取参数的来源不同. 一般的参数化:把脚本中一个写死的数值或者字符串用一个变量参数来代替,每次迭代的时候,从本地文件或者数据库中 ...

  10. H Hip To Be Square Day5——NWERC2012

    这个题目巨坑啊.调试的时间加起来绝对超过1天整. 不过终于调试出来了,真心感动地尿流满面啊. 题目的意思是给你一个区间[A,B],可以从区间里选出任意多个整数,使得这些整数的积是一个不超过 2^126 ...