iOS --- 总结Swift中常用的全局函数和extension(持续更新中)

将iOS开发中常用的Swift全局函数和extension(因Swift中不能使用宏定义)整理如下。
而对于Objective-C,其宏定义总结请参考博客iOS — 总结Objective-C中常用的宏定义(持续更新中)

//
//  Global.swift
//
//  useful global methods and extension for Chris Hu
//

import Foundation

func isIPhone4() -> Bool {
    return UIScreen.mainScreen().currentMode?.size == CGSizeMake(640, 960)
}
func isIPhone5() -> Bool {
    return UIScreen.mainScreen().currentMode?.size == CGSizeMake(640, 1136)
}
func isIPhone6() -> Bool {
    return UIScreen.mainScreen().currentMode?.size == CGSizeMake(750, 1334)
}
func isIPhone6Plus() -> Bool {
    return UIScreen.mainScreen().currentMode?.size == CGSizeMake(1242, 2208)
}
func isIPhone6PlusBigMode() -> Bool {
    return UIScreen.mainScreen().currentMode?.size == CGSizeMake(1125, 2001)
}


func currentLanguage() -> String {
    return NSLocale.preferredLanguages().first!
}

func systemVersion() -> String {
    return UIDevice.currentDevice().systemVersion
}

func appVersion() -> String {
    return String(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]!)
}


extension UIColor {
    public convenience init(hexString: UInt32, alpha: CGFloat = 1.0) {
        let red     = CGFloat((hexString & 0xFF0000) >> 16) / 255.0
        let green   = CGFloat((hexString & 0x00FF00) >> 8 ) / 255.0
        let blue    = CGFloat((hexString & 0x0000FF)      ) / 255.0
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}

extension UITableViewDataSource {
    // return total rows in the tableView
    func totalRows(tableView: UITableView) -> Int {
        let totalSections = self.numberOfSectionsInTableView!(tableView) ?? 1
        var section = 0, total = 0
        while section < totalSections {
            total += self.tableView(tableView, numberOfRowsInSection: section)
            section++
        }
        return total
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS的开发,使用Swift可以实现底部弹出视图。以下是一个简单的实现方式: 首先,需要创建一个底部弹出视图的ViewController。可以在Storyboard创建一个新的ViewController,并设置其底部约束为屏幕底部,高度为底部弹出视图的高度。可以在该ViewController添加需要显示的内容,比如按钮、标签等。 然后,在需要弹出底部视图的地方,可以通过 present 方法来显示这个ViewController。可以在当前的ViewController添加一个按钮,当点击该按钮时,调用如下代码来显示底部弹出视图: ```swift // 创建底部弹出视图的ViewController let bottomViewController = storyboard?.instantiateViewController(withIdentifier: "BottomViewController") as! BottomViewController // 设置底部弹出视图的 ModalPresentationStyle 为 .overCurrentContext,使得弹出视图会覆盖当前视图 bottomViewController.modalPresentationStyle = .overCurrentContext // 设置底部弹出视图的 TransitioningDelegate;通过实现相关代理方法来自定义弹出视图的动画效果 bottomViewController.transitioningDelegate = self // 弹出底部视图 present(bottomViewController, animated: true, completion: nil) ``` 在当前ViewController,需要增加一个扩展来实现底部弹出视图的自定义动画: ```swift extension ViewController: UIViewControllerTransitioningDelegate { // 返回自定义的动画控制器对象 func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return BottomViewAnimator(isPresenting: true) } // 返回自定义的动画控制器对象 func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { return BottomViewAnimator(isPresenting: false) } } ``` 在自定义动画控制器,可以实现底部弹出视图的动画效果。可以通过使用 UIView 的动画方法来实现位移、透明度等动画效果。 最后,需要创建一个自定义的 Animator 类,实现 UIViewControllerAnimatedTransitioning 协议的相关代理方法来控制动画的展示和隐藏。 通过上述步骤,即可完成iOS底部弹出视图的实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值