天天看點

IOS開發Swift——開發小知識(持續更新)如有錯誤,請指正!謝謝!

如有錯誤,請指正!謝謝!

1.PHAsset擷取本地視訊的url

PHCachingImageManager().requestAVAsset(forVideo: asset, options:nil, resultHandler: { (asset, audioMix, info)in

          let  avAsset = asset as? AVURLAsset
          print(asset?.url)
})
           

2.手勢沖突

moreTap.require(toFail: singleTap)
//優先檢測singleTap,若singleTap檢測不到,或檢測失敗,則檢測moreTap,檢測成功後,觸發方法
singleTap.require(toFail: moreTap)
//優先檢測moreTap,若moreTap檢測不到,或檢測失敗,則檢測singleTap,檢測成功後,觸發方法
           

原文連結:https://blog.csdn.net/a645258072/article/details/72896196

3.UITableViewCell UIButton不響應點選事件

cell.contentView.addSubview:(button)
           

4.ableViewCell取消點選方法與取消點選高亮狀态

//取消觸發點選方法
self.tableView.allowsSelection = false

//取消點選高亮
cell.selectionStyle = .none
           

5.UITableView 禁止滑動

tableView.isScrollEnabled = false
           

6.根據cell裡的子控件來擷取cell,以UIbutton為例

func superUITableViewCell(of : UIButton) -> TableViewCell?{
        for view in sequence(first: of.superview, next: { $0?.superview }) {
            if let cell = view as? TableViewCell {
                return cell
            }
        }
        return nil
    }
           

7.收起鍵盤

//方法1
textField.resignFirstResponder()

//方法2
UIApplication.shared.keyWindow?.endEditing(true)
           

8.管理Placeholder的文本屬性(包括字型、顔色、大小)

textField.attributedPlaceholder = NSAttributedString(string: __("文本内容"),
                                                        attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15),
                                                                     NSAttributedString.Key.foregroundColor: UIColor.init(hex: 0x000000,alpha: 0.5)])
           

9.textfield文本距離邊框的距離設定

let leftView = UIView.init(frame:CGRect.init(x:0, y: 0, width:12, height: 20))
                leftView.backgroundColor = .clear
 let textField = UITextField()
   


textField.leftViewMode = .always    //此句代碼較容易忽略
textField.leftView = leftView
           

10.sqlite更新blob類型資料

let sql = "update \(name) set coverPhoto=cast((?) as blob) where id=\(id)"
let db = SQLiteManager.shareManger().db
db.executeUpdate(sql, withArgumentsIn: [coverPhoto as Data])

//主要是
cast((?) as blob) 與 [coverPhoto as Data] 表明存入資料類型和發過來的資料類型
//cast(xxx as 類型).    convert(xxx,類型)

           

11.擷取字典的key或者value

Array(listArray[num].keys)[0]
 Array(listArray[num].values)[0]