Skip to content

Instantly share code, notes, and snippets.

/*
上一篇取消動畫使用的是 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)
extension NSMutableAttributedString {
func insertImage(image: UIImage?, tintColor: UIColor? = nil, bounds: CGRect = .zero, at: Int) {
let imageAttachment = NSTextAttachment()
if let tintColor = tintColor{
// 搭配自己的UIImageExtension
imageAttachment.image = image?.tinted(color: tintColor)
} else {
imageAttachment.image = image
}
/*
compressImgMid: 二分壓縮法,指定預期的 MB
imgWithNewSize: 更改 img 尺寸,可依照預期 imgView 的尺寸去縮放(一般縮到跟 imgView 預期最大即可)
*/
extension UIImage {
extension UIColor {
class var mainXXX: UIColor {
let color = UIColor(XXX)
return color
}
static let main: UIColor = {
/* 設定Label 文字漸層色
有兩三種做法,這個作法是我最喜歡的,不需要考慮到 view 的生命週期。
原理: 在label 繪製text之前。建立一個漸層色image,再將它轉成顏色給textColor
*/
extension UIImage {
@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()
}