swift知识点汇总

swift中NSClassFromString正确的使用方法!

  private lazy var titles: [String] = {

        return ["WTHomeSeController", "WTHomeController", "WTHomeSeController"]

    }()

 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

         //第一步:获取项目名称

        let clsName = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String

        //第二步:拼接参数字符串 项目名+.+需要加载的类的名称

        let vcView = NSClassFromString(clsName! + "." + titles[indexPath.row]) as! UIViewController.Type

        //第三步:调用初始化方法

        let vc = vcView.init()

        //第四步执行跳转

        self.navigationController?.pushViewController(vc, animated: true)    }

Swift手势使用

 

单机手势

let guesture = UITapGestureRecognizer(target: self, action: #selector(singleTap(_:)))

view.addGestureRecognizer(guesture)

func singleTap(_ tapGesture: UITapGestureRecognizer) {

}

双击手势

let doubleGuesture = UITapGestureRecognizer(target: self, action: #selector(doubleTap(_:)))

doubleGuesture.numberOfTapsRequired = 2// 点击次数

doubleGuesture.numberOfTouchesRequired = 1// 手指个数

view.addGestureRecognizer(doubleGuesture)

func doubleTap(_ tapGesture: UITapGestureRecognizer) {

}

长按手势

let longGuesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))

view.addGestureRecognizer(longGuesture)

func longPress(_ longPressGesture: UILongPressGestureRecognizer){

}

滑动手势

left:向左滑动,right:向右滑动,up:向上滑动,down:向下滑动,

let leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeGesture(_:)))

leftSwipeGesture.direction= .left

view.addGestureRecognizer(leftSwipeGesture)

func swipeGesture(_ swipeGesture: UISwipeGestureRecognizer) {

switch swipeGesture.direction {

  case.left, .right, .up, .down:

  print("SwipeGesture")

}}

捏合手势

let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinchGesture(_:)))

view.addGestureRecognizer(pinch)

func pinchGesture(_ pinchGesture: UIPinchGestureRecognizer) {

  print(pinchGesture.scale)// 捏合比例

  print(pinchGesture.velocity)// 捏合速度

}

旋转手势

let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationGesture(_:)))

view.addGestureRecognizer(rotation)

func rotationGesture(_ rotationGesture: UIRotationGestureRecognizer) {

print(rotationGesture.rotation*(180/(CGFloat(Double.pi))))// 旋转的角度

}

拖动手势

@IBOutlet weak var panGestureView: UIView!

let pan = UIPanGestureRecognizer(target: self, action: #selector(panGesture(_:)))

pan.maximumNumberOfTouches=1// 一个手指拖动

panGestureView.addGestureRecognizer(pan)

func panGesture(_ panGesture: UIPanGestureRecognizer) {

let point = panGesture.location(in: panGesture.view?.superview)

panGestureView.center= point

}

iOS CollectionView添加长按手势并识别cell名称

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCollectionViewCell
 
        cell.labelCellName.text = arrayCellName[indexPath.row]
 
        ///添加长按手势//
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(cellLongPress(sender:)))
 
        cell.addGestureRecognizer(longPress)
 
        return cell

}

@objc func cellLongPress(sender:UILongPressGestureRecognizer) {
 
        //获取手势在collectionView中的点
        let touchPoint = sender.location(in: self.collectionView)
        var editCellName = ""
 
        if (sender.state == UIGestureRecognizerState.ended)
        {
 
            let indexPath = self.collectionView.indexPathForItem(at: touchPoint)
 
            if indexPath != nil {
                editCellName = arrayCellName[(indexPath?.row)!]
            }
        }

}

Swift 闭包的使用坑

很多人在使用闭包的时候容易引起循环引用,解决循环引用的问题可以使用 [unowned self] 和 weak var weakSelf = self 来解决具体的使用?如下

使用weak关键字

 

private func loadDataAction(){
        
        QMUITips.showLoading(in: self.view)
        weak var weakSelf = self
        YHLNetworkManger.share.addWorkFlowTableInfoAction {
           (flag, data) in
            let view = weakSelf?.view ?? UIView.init()
            QMUITips.hideAllTips(in: view)
            if flag {
                weakSelf?.congigerDataAction(data: data)
            }else{
                QMUITips.showError("表单加载失败", in: view, hideAfterDelay: 1.5)
            }
        }
        
    }

使用[unowned self]

private func loadDataAction(){
        
        QMUITips.showLoading(in: self.view)
        
        YHLNetworkManger.share.addWorkFlowTableInfoAction {
           [unowned self] (flag, data) in
            QMUITips.hideAllTips(in: self.view)
            if flag {
                weakSelf?.congigerDataAction(data: data)
            }else{
                QMUITips.showError("表单加载失败", in: self.view, hideAfterDelay: 1.5)
            }
        }
        
    }

直接说结果吧,第一种效果好,因为在网络回调之前页面如果退出,不会崩溃。

https://www.jianshu.com/p/b75cf0214724

 

swift弹窗

let alertNormal = UIAlertController(title: "系统提示", message: "无上的神伟大的小明", preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: "取消", style: .cancel){(UIAlertAction) in

            print("点击了取消")

            

        }

       let sureAction = UIAlertAction(title: "确定", style: .destructive) { (UIAlertAction) in

           print("点击了确定")

       }

       alertNormal.addAction(cancelAction);

       alertNormal.addAction(sureAction);

       self.present(alertNormal, animated: true) {

 

               }

 

  let alertVc = UIAlertController(title: "系统提示", message: "无上的神伟大的小明", preferredStyle: .alert)

                   let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler:nil)

                   alertVc.addAction(cancelAction)

 

                   let okAction = UIAlertAction(title: "确定", style: .default, handler:{ (action) in

                   

                    

                    print(action.title!)

                    

                    })

                    alertVc.addAction(okAction)

 

        self.present(alertVc, animated:true, completion:{()

            

            print("弹窗显示完毕")

        })

 第三个

  let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复",

                                                preferredStyle: .actionSheet)

        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: {

            (action) in

            

            print("取消")

            

        })

        let deleteAction = UIAlertAction(title: "删除", style: .destructive, handler: {

            (action) in

            

            print("删除")

            

        })

        let archiveAction = UIAlertAction(title: "保存", style: .default, handler: nil)

        alertController.addAction(cancelAction)

        alertController.addAction(deleteAction)

        alertController.addAction(archiveAction)

        self.present(alertController, animated: true, completion: nil)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值