Skip to content

Instantly share code, notes, and snippets.

/*
更改 navigationBar 字體顏色
方法很多種,
只特別記錄一種萬用的
*/
func setNavigationBar() {
// 完全全黑
/*
Change UIAlert message textColor
*/
extension UIAlertAction {
var titleTextColor: UIColor? {
get {
return self.value(forKey: "titleTextColor") as? UIColor
} set {
@woodycatliu
woodycatliu / imageOrientation.swift
Created March 29, 2021 03:40
在處理相機圖片時(影片),需要先確定鏡頭方向,才能正確做ML分析,或是繪圖。
// return UIImage.Orientation
private func imageOrientation(deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation,
cameraPosition: AVCaptureDevice.Position = .front) -> UIImage.Orientation {
var deviceOrientation = deviceOrientation
if deviceOrientation == .faceDown || deviceOrientation == .faceUp || deviceOrientation == .unknown {
deviceOrientation = currectDeviceOrientation()
}
@woodycatliu
woodycatliu / imageOrientation.swift
Created March 29, 2021 03:41
在處理相機圖片時(影片),需要先確定鏡頭方向,才能正確做ML分析,或是繪圖。以下分別列出iOS原生鏡頭跟UIImage orientation 跟 Firebase ML Kit 的轉換方法
// return UIImage.Orientation
private func imageOrientation(deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation,
cameraPosition: AVCaptureDevice.Position = .front) -> UIImage.Orientation {
var deviceOrientation = deviceOrientation
if deviceOrientation == .faceDown || deviceOrientation == .faceUp || deviceOrientation == .unknown {
deviceOrientation = currectDeviceOrientation()
}
/*
關閉UIView Anitmate小技巧。
有時候設定一系列 View 互動動畫, 突然遇到一個需求,需要關閉特定動畫,但是其他動畫要繼續執行的需求時,
就會特別頭痛。
比如內建 TableView delete/ insert 動畫 + 自定義的其他動畫時,處理起來會特別煩瑣。
而我們可以用簡單的 UIView.performWithoutAnimation 配上 main Therad 處理這件事情
*/
UIView.performWithoutAnimation {
// 動畫被取消,但是自動判斷Difference 不會失效
/*
上一篇取消動畫使用的是 UIView.performWithoutAnimation {}
很好用,但是還是有遺漏的部分,如果原第三方套件把動畫包在 DispathQueue.main 中
動畫就會正常運作。
這一篇介紹的方法將會強制取所任何動畫,概念上就是直接在View底層禁止安裝動畫。
主件~
開關: UIView.setAnimationsEnabled(Bool)
搭配~
CATransaction.begin()
/*
UIKit constraint 設定雖然簡單,但是非常繁瑣,
往往一個View 自少就要設定 4 條 constraint ,
數量一堆也很花時間。
透過 extension UIVIew 將最常用的代碼打包,
可以簡化Constraint,加快刻畫UI的時間
*/
extension UIView {
import UIKit
public struct AnchoredConstraints {
public var top, leading, bottom, trailing, width, height: NSLayoutConstraint?
}
extension UIView {
@discardableResult
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
//
// ViewController.swift
// Practice_TapToOpenZoomInNextBackgroundView
//
// Created by Woody on 2020/12/14.
//
import UIKit
/*
UIImage 染色
String 塞圖片
*/
import UIKit
extension UIImage {
func tinted(color: UIColor) -> UIImage? {
let image = withRenderingMode(.alwaysTemplate)