This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
上一篇取消動畫使用的是 UIView.performWithoutAnimation {} | |
很好用,但是還是有遺漏的部分,如果原第三方套件把動畫包在 DispathQueue.main 中 | |
動畫就會正常運作。 | |
這一篇介紹的方法將會強制取所任何動畫,概念上就是直接在View底層禁止安裝動畫。 | |
主件~ | |
開關: UIView.setAnimationsEnabled(Bool) | |
搭配~ | |
CATransaction.begin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
UIKit constraint 設定雖然簡單,但是非常繁瑣, | |
往往一個View 自少就要設定 4 條 constraint , | |
數量一堆也很花時間。 | |
透過 extension UIVIew 將最常用的代碼打包, | |
可以簡化Constraint,加快刻畫UI的時間 | |
*/ | |
extension UIView { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Practice_TapToOpenZoomInNextBackgroundView | |
// | |
// Created by Woody on 2020/12/14. | |
// | |
import UIKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
UIImage 染色 | |
String 塞圖片 | |
*/ | |
import UIKit | |
extension UIImage { | |
func tinted(color: UIColor) -> UIImage? { | |
let image = withRenderingMode(.alwaysTemplate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
compressImgMid: 二分壓縮法,指定預期的 MB | |
imgWithNewSize: 更改 img 尺寸,可依照預期 imgView 的尺寸去縮放(一般縮到跟 imgView 預期最大即可) | |
*/ | |
extension UIImage { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIColor { | |
class var mainXXX: UIColor { | |
let color = UIColor(XXX) | |
return color | |
} | |
static let main: UIColor = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 設定Label 文字漸層色 | |
有兩三種做法,這個作法是我最喜歡的,不需要考慮到 view 的生命週期。 | |
原理: 在label 繪製text之前。建立一個漸層色image,再將它轉成顏色給textColor | |
*/ | |
extension UIImage { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} |