Add - 5 Screen examples (UI Design from dribbble + ContainerController)
This commit is contained in:
1 parent
341ed54032
commit
e87a0a40b6
186 files changed
+11183
-17
No files matched your search
@@ -0,0 +1,64 @@
|
||||
|
||||
|
||||
import UIKit
|
||||
|
||||
class CopyableLabel: UILabel {
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
self.sharedInit()
|
||||
}
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)
|
||||
self.sharedInit()
|
||||
}
|
||||
|
||||
func sharedInit() {
|
||||
self.isUserInteractionEnabled = true
|
||||
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(self.showMenu))
|
||||
self.addGestureRecognizer(gesture)
|
||||
}
|
||||
|
||||
@objc func showMenu(_ recognizer: UILongPressGestureRecognizer) {
|
||||
self.becomeFirstResponder()
|
||||
|
||||
let menu = UIMenuController.shared
|
||||
|
||||
let locationOfTouchInLabel = recognizer.location(in: self)
|
||||
|
||||
if !menu.isMenuVisible {
|
||||
var rect = bounds
|
||||
rect.origin = locationOfTouchInLabel
|
||||
rect.size = CGSize(width: 1, height: 1)
|
||||
|
||||
if #available(iOS 13.0, *) {
|
||||
menu.showMenu(from: self, rect: rect)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override func copy(_ sender: Any?) {
|
||||
let board = UIPasteboard.general
|
||||
|
||||
board.string = text
|
||||
|
||||
let menu = UIMenuController.shared
|
||||
|
||||
if #available(iOS 13.0, *) {
|
||||
menu.showMenu(from: self, rect: bounds)
|
||||
} else {
|
||||
menu.setMenuVisible(false, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
override var canBecomeFirstResponder: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
||||
return action == #selector(UIResponderStandardEditActions.copy)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
|
||||
|
||||
import UIKit
|
||||
|
||||
@IBDesignable
|
||||
class DesignButton: UIButton {
|
||||
|
||||
@IBInspectable var hideAnimation: Bool = true
|
||||
|
||||
//MARK: Fill
|
||||
|
||||
@IBInspectable var fillColor: UIColor = .clear
|
||||
|
||||
//MARK: Gradient
|
||||
|
||||
@IBInspectable var grColor1: UIColor?
|
||||
@IBInspectable var grColor2: UIColor?
|
||||
@IBInspectable var grColor3: UIColor?
|
||||
@IBInspectable var grColor4: UIColor?
|
||||
@IBInspectable var grColor5: UIColor?
|
||||
@IBInspectable var grColor6: UIColor?
|
||||
|
||||
@IBInspectable var grStartPoint: CGPoint = .zero
|
||||
@IBInspectable var grEndPoint: CGPoint = .zero
|
||||
|
||||
//MARK: Inner Shadow
|
||||
|
||||
@IBInspectable var inShColor: UIColor = .clear
|
||||
@IBInspectable var inShRadius: CGFloat = 0.0
|
||||
@IBInspectable var inShOffset: CGSize = .zero
|
||||
|
||||
//MARK: Shadow
|
||||
|
||||
@IBInspectable var shColor: UIColor = .clear
|
||||
@IBInspectable var shRadius: CGFloat = 0.0
|
||||
@IBInspectable var shOffset: CGSize = .zero
|
||||
|
||||
//MARK: Border
|
||||
|
||||
@IBInspectable var brColor: UIColor = .clear
|
||||
@IBInspectable var brWidth: CGFloat = 0.0
|
||||
|
||||
//MARK: Blur
|
||||
|
||||
@IBInspectable var blur: CGFloat = 0.0
|
||||
|
||||
//MARK: Radius
|
||||
|
||||
@IBInspectable var cornerRadius: CGFloat = 0.0
|
||||
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
|
||||
layer.cornerRadius = self.cornerRadius(cornerRadius)
|
||||
layer.backgroundColor = fillColor.cgColor
|
||||
|
||||
addGradient()
|
||||
addInnerShadow()
|
||||
addShadow()
|
||||
addBorder()
|
||||
|
||||
add(blur: blur)
|
||||
}
|
||||
|
||||
private func addGradient() {
|
||||
|
||||
_ = add(gradient: grColor1,
|
||||
color2: grColor2,
|
||||
color3: grColor3,
|
||||
color4: grColor4,
|
||||
color5: grColor5,
|
||||
color6: grColor6,
|
||||
pointStart: grStartPoint,
|
||||
pointEnd: grEndPoint,
|
||||
cornerRadius: cornerRadius)
|
||||
|
||||
}
|
||||
|
||||
private func addInnerShadow() {
|
||||
|
||||
addInnerShadow(color: inShColor,
|
||||
radius: inShRadius,
|
||||
offset: inShOffset,
|
||||
cornerRadius: cornerRadius)
|
||||
|
||||
}
|
||||
|
||||
private func addShadow() {
|
||||
layer.shadowOffset = shOffset
|
||||
layer.shadowOpacity = 1.0
|
||||
layer.shadowRadius = shRadius
|
||||
layer.shadowColor = shColor.cgColor
|
||||
}
|
||||
|
||||
private func addBorder() {
|
||||
layer.borderColor = brColor.cgColor
|
||||
layer.borderWidth = brWidth
|
||||
}
|
||||
|
||||
override var isSelected: Bool {
|
||||
didSet {
|
||||
super.isSelected = isSelected
|
||||
}
|
||||
}
|
||||
|
||||
override var isHighlighted: Bool {
|
||||
didSet {
|
||||
// super.isHighlighted = isHighlighted
|
||||
}
|
||||
}
|
||||
|
||||
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
|
||||
selectedLayer(show: true)
|
||||
return super.beginTracking(touch, with: event)
|
||||
}
|
||||
|
||||
override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
|
||||
let point = touch.location(in: self)
|
||||
let someFrame = bounds
|
||||
let highlighted = someFrame.contains(point)
|
||||
selectedLayer(show: highlighted)
|
||||
return super.continueTracking(touch, with: event)
|
||||
}
|
||||
|
||||
override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
|
||||
selectedLayer(show: false)
|
||||
super.endTracking(touch, with: event)
|
||||
}
|
||||
|
||||
override func cancelTracking(with event: UIEvent?) {
|
||||
selectedLayer(show: false)
|
||||
super.cancelTracking(with: event)
|
||||
}
|
||||
|
||||
func radius() -> CGFloat {
|
||||
let minSize = min(frame.size.width, frame.size.height)
|
||||
let radius = ((cornerRadius < 0) ? (minSize / 2) : cornerRadius)
|
||||
return radius
|
||||
}
|
||||
|
||||
func selectedLayer(show: Bool) {
|
||||
|
||||
func selectAnimationHide(show: Bool) {
|
||||
if show {
|
||||
setAlpha(.halfTransluent)
|
||||
} else {
|
||||
UIView.animate(with: .ms350) {
|
||||
self.setAlpha(.visible)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func selectAnimationShadow(show: Bool) {
|
||||
let tag = 1
|
||||
let view = viewWithTag(tag)
|
||||
if show {
|
||||
if view == nil {
|
||||
let view = UIView(frame: bounds)
|
||||
view.backgroundColor = UIColor.init(white: 0.0, alpha: 0.2)
|
||||
view.tag = tag
|
||||
view.layer.cornerRadius = radius()
|
||||
addSubview(view)
|
||||
}
|
||||
} else {
|
||||
if let view = view {
|
||||
UIView.animate(with: .ms350, animations: {
|
||||
view.setAlpha(.invisible)
|
||||
}, completion: { (fin: Bool) in
|
||||
view.removeFromSuperview()
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hideAnimation {
|
||||
selectAnimationHide(show: show)
|
||||
} else {
|
||||
selectAnimationShadow(show: show)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,716 @@
|
||||
//
|
||||
// DesignTest.swift
|
||||
// FigmaConvertXib
|
||||
//
|
||||
// Created by Рустам Мотыгуллин on 28.07.2020.
|
||||
// Copyright © 2020 mrusta. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
|
||||
@IBDesignable
|
||||
class DesignFigure: UIView {
|
||||
/// Rectange | Ellipse | Polygon | Star
|
||||
@IBInspectable var figureType: Int = 0
|
||||
@IBInspectable var starRadius: CGFloat = 0
|
||||
@IBInspectable var starCount: Int = 5
|
||||
@IBInspectable var cornerRadius: CGFloat = 0.0
|
||||
@IBInspectable var blur: CGFloat = 0.0
|
||||
@IBInspectable var image: UIImage?
|
||||
/// ScaleToFill | Fit | Fill
|
||||
@IBInspectable var imageMode: Int = 1
|
||||
@IBInspectable var fillColor: UIColor?
|
||||
@IBInspectable var grColor1: UIColor?
|
||||
@IBInspectable var grColor2: UIColor?
|
||||
@IBInspectable var grColor3: UIColor?
|
||||
@IBInspectable var grColor4: UIColor?
|
||||
@IBInspectable var grColor5: UIColor?
|
||||
@IBInspectable var grColor6: UIColor?
|
||||
@IBInspectable var grStartPoint: CGPoint = .zero
|
||||
@IBInspectable var grEndPoint: CGPoint = .zero
|
||||
/// Default: Linear
|
||||
@IBInspectable var grRadial: Bool = false
|
||||
@IBInspectable var grDrawsOptions: Bool = true
|
||||
@IBInspectable var grDebug: Bool = false
|
||||
@IBInspectable var grPointPercent: Bool = true
|
||||
@IBInspectable var grBlendMode: Int = 0
|
||||
@IBInspectable var brColor: UIColor = .clear
|
||||
@IBInspectable var brWidth: CGFloat = 0.0
|
||||
@IBInspectable var brDash: Int = 0
|
||||
@IBInspectable var inShColor: UIColor = .clear
|
||||
@IBInspectable var inShRadius: CGFloat = 0.0
|
||||
@IBInspectable var inShOffset: CGSize = .zero
|
||||
@IBInspectable var shColor: UIColor = .clear
|
||||
@IBInspectable var shRadius: CGFloat = 0.0
|
||||
@IBInspectable var shOffset: CGSize = .zero
|
||||
|
||||
func setup() {
|
||||
|
||||
if let foundView = viewWithTag(55) {
|
||||
foundView.removeFromSuperview()
|
||||
}
|
||||
|
||||
let d = DesignFigure_(frame: bounds)
|
||||
d.backgroundColor = .clear
|
||||
d.tag = 55
|
||||
d.figureType = figureType
|
||||
d.starRadius = starRadius
|
||||
d.starCount = starCount
|
||||
d.cornerRadius = cornerRadius
|
||||
d.blur = blur
|
||||
d.image = image
|
||||
d.imageMode = imageMode
|
||||
d.fillColor = fillColor
|
||||
d.grColor1 = grColor1
|
||||
d.grColor2 = grColor2
|
||||
d.grColor3 = grColor3
|
||||
d.grColor4 = grColor4
|
||||
d.grColor5 = grColor5
|
||||
d.grColor6 = grColor6
|
||||
d.grStartPoint = grStartPoint
|
||||
d.grEndPoint = grEndPoint
|
||||
d.grRadial = grRadial
|
||||
d.grDrawsOptions = grDrawsOptions
|
||||
d.grDebug = grDebug
|
||||
d.grPointPercent = grPointPercent
|
||||
d.grBlendMode = grBlendMode
|
||||
d.brWidth = brWidth
|
||||
d.brColor = brColor
|
||||
d.brDash = brDash
|
||||
d.inShColor = inShColor
|
||||
d.inShRadius = inShRadius
|
||||
d.inShOffset = inShOffset
|
||||
d.shColor = shColor
|
||||
d.shRadius = shRadius
|
||||
d.shOffset = shOffset
|
||||
insertSubview(d, at: 0)
|
||||
|
||||
if grDebug {
|
||||
if let foundView = viewWithTag(44) {
|
||||
foundView.removeFromSuperview()
|
||||
}
|
||||
|
||||
let d = DesignFigureDebug(frame: bounds)
|
||||
d.backgroundColor = .clear
|
||||
d.tag = 44
|
||||
d.grStartPoint = grStartPoint
|
||||
d.grEndPoint = grEndPoint
|
||||
d.grPointPercent = grPointPercent
|
||||
|
||||
insertSubview(d, at: 1)
|
||||
}
|
||||
}
|
||||
|
||||
override func layoutSubviews() { setup() }
|
||||
|
||||
}
|
||||
|
||||
@IBDesignable
|
||||
class DesignFigureDebug: UIView {
|
||||
|
||||
@IBInspectable var grStartPoint: CGPoint = .zero
|
||||
@IBInspectable var grEndPoint: CGPoint = .zero
|
||||
@IBInspectable var grPointPercent: Bool = true
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
super.draw(rect)
|
||||
|
||||
addGradientDebugPoints()
|
||||
}
|
||||
|
||||
func addGradientDebugPoints() {
|
||||
|
||||
var start = grStartPoint
|
||||
var end = grStartPoint
|
||||
|
||||
if grPointPercent {
|
||||
end = CGPoint(x: grEndPoint.x * frame.width, y: grEndPoint.y * frame.height)
|
||||
start = CGPoint(x: grStartPoint.x * frame.width, y: grStartPoint.y * frame.height)
|
||||
}
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
let radius: CGFloat = 5
|
||||
|
||||
context.setBlendMode(CGBlendMode.normal)
|
||||
context.setLineWidth(3.0)
|
||||
|
||||
|
||||
context.setFillColor(UIColor.black.cgColor)
|
||||
|
||||
context.setStrokeColor(UIColor.systemRed.cgColor)
|
||||
|
||||
let circleRect = CGRect(x: start.x - radius,
|
||||
y: start.y - radius,
|
||||
width: radius * 2,
|
||||
height: radius * 2)
|
||||
|
||||
context.fillEllipse(in: circleRect)
|
||||
context.strokeEllipse(in: circleRect)
|
||||
|
||||
context.setStrokeColor(UIColor.green.cgColor)
|
||||
|
||||
let circleRect2 = CGRect(x: end.x - radius,
|
||||
y: end.y - radius,
|
||||
width: radius * 2,
|
||||
height: radius * 2)
|
||||
|
||||
context.fillEllipse(in: circleRect2)
|
||||
context.strokeEllipse(in: circleRect2)
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum DesignFigureType: Int {
|
||||
case rectange = 0
|
||||
case ellipse = 1
|
||||
case polygon = 2
|
||||
case star = 3
|
||||
}
|
||||
|
||||
@IBDesignable
|
||||
class DesignFigure_: UIView {
|
||||
|
||||
@IBInspectable var figureType: Int = 0 /// Rectange Ellipse Polygon Star
|
||||
|
||||
@IBInspectable var starRadius: CGFloat = 0
|
||||
@IBInspectable var starCount: Int = 5
|
||||
//MARK: Radius
|
||||
|
||||
@IBInspectable var cornerRadius: CGFloat = 0.0
|
||||
|
||||
//MARK: Blur
|
||||
|
||||
@IBInspectable var blur: CGFloat = 0.0
|
||||
|
||||
//MARK: Image
|
||||
|
||||
@IBInspectable var image: UIImage?
|
||||
@IBInspectable var imageMode: Int = 1 /// Scale to fill / Aspect fit / fill
|
||||
|
||||
//MARK: Fill
|
||||
|
||||
@IBInspectable var fillColor: UIColor?
|
||||
|
||||
//MARK: Gradient
|
||||
|
||||
@IBInspectable var grColor1: UIColor?
|
||||
@IBInspectable var grColor2: UIColor?
|
||||
@IBInspectable var grColor3: UIColor?
|
||||
@IBInspectable var grColor4: UIColor?
|
||||
@IBInspectable var grColor5: UIColor?
|
||||
@IBInspectable var grColor6: UIColor?
|
||||
|
||||
@IBInspectable var grStartPoint: CGPoint = .zero
|
||||
@IBInspectable var grEndPoint: CGPoint = .zero
|
||||
|
||||
@IBInspectable var grRadial: Bool = false /// default: linear
|
||||
@IBInspectable var grDrawsOptions: Bool = true
|
||||
@IBInspectable var grDebug: Bool = false
|
||||
@IBInspectable var grPointPercent: Bool = true
|
||||
@IBInspectable var grBlendMode: Int = 0
|
||||
|
||||
//MARK: Border
|
||||
|
||||
@IBInspectable var brColor: UIColor = .clear
|
||||
@IBInspectable var brWidth: CGFloat = 0.0
|
||||
@IBInspectable var brDash: Int = 0
|
||||
|
||||
//MARK: Inner Shadow
|
||||
|
||||
@IBInspectable var inShColor: UIColor = .clear
|
||||
@IBInspectable var inShRadius: CGFloat = 0.0
|
||||
@IBInspectable var inShOffset: CGSize = .zero
|
||||
|
||||
//MARK: Shadow
|
||||
|
||||
@IBInspectable var shColor: UIColor = .clear
|
||||
@IBInspectable var shRadius: CGFloat = 0.0
|
||||
@IBInspectable var shOffset: CGSize = .zero
|
||||
|
||||
|
||||
|
||||
//MARK: - Draw
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
super.draw(rect)
|
||||
|
||||
guard let bezier = figurePath(bounds) else { return }
|
||||
|
||||
addFill(bezier: bezier)
|
||||
|
||||
addImage(bezier: bezier)
|
||||
|
||||
addGradient(bezier: bezier)
|
||||
|
||||
addInnerShadow(bezier: bezier)
|
||||
addBorder()
|
||||
addShadow()
|
||||
|
||||
addBlur()
|
||||
}
|
||||
|
||||
//MARK: - Figure Type
|
||||
|
||||
private func figurePath(_ rect: CGRect) -> UIBezierPath? {
|
||||
|
||||
let starR = starRadius / 50
|
||||
if starCount < 1 { starCount = 1 }
|
||||
|
||||
switch figureType {
|
||||
case 1: return UIBezierPath(ovalIn: rect)
|
||||
case 2: return star(polygon: true, rect: rect, radius: starR, pointsOnStar: starCount)
|
||||
case 3: return star(rect: rect, radius: starR, pointsOnStar: starCount)
|
||||
default: return rectangle(rect: rect, radius: cornerRadius)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Border / Stroke
|
||||
|
||||
private func addBorder() {
|
||||
|
||||
if brWidth < 0 { brWidth = 0 }
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
// brColor.setStroke()
|
||||
|
||||
guard let bezier = figurePath(bounds) else { return }
|
||||
|
||||
context.saveGState()
|
||||
|
||||
if brDash > 0 {
|
||||
|
||||
UIColor.clear.setFill()
|
||||
bezier.fill()
|
||||
|
||||
brColor.setStroke()
|
||||
bezier.lineWidth = brWidth * 2
|
||||
|
||||
let dashPattern : [CGFloat] = [ CGFloat(brDash), CGFloat(brDash)]
|
||||
bezier.setLineDash(dashPattern, count: 2, phase: 0)
|
||||
bezier.stroke()
|
||||
|
||||
} else {
|
||||
|
||||
brColor.setStroke()
|
||||
|
||||
bezier.lineWidth = brWidth * 2
|
||||
bezier.stroke()
|
||||
}
|
||||
|
||||
|
||||
|
||||
bezier.stroke()
|
||||
bezier.stroke()
|
||||
|
||||
context.restoreGState()
|
||||
context.restoreGState()
|
||||
}
|
||||
|
||||
|
||||
private func addImage(bezier: UIBezierPath) {
|
||||
|
||||
guard let image = image else { return }
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
var f: CGRect = CGRect(x: 0, y: -bounds.height, width: bounds.width, height: bounds.height)
|
||||
|
||||
if imageMode == 1 || imageMode == 2 {
|
||||
|
||||
var w: CGFloat = 0
|
||||
var h: CGFloat = 0
|
||||
|
||||
let percent = bounds.height / image.size.height
|
||||
w = image.size.width * percent
|
||||
h = bounds.height
|
||||
|
||||
if (imageMode == 1) && (w > bounds.width) ||
|
||||
(imageMode == 2) && (bounds.width > w) {
|
||||
|
||||
let percent = bounds.width / image.size.width
|
||||
h = image.size.height * percent
|
||||
w = bounds.width
|
||||
}
|
||||
|
||||
let x = (bounds.width / 2) - (w / 2)
|
||||
let y = (-h + (((bounds.height / 2) - (h / 2)) * (-1)))
|
||||
|
||||
f = CGRect(x: x, y: y, width: w, height: h)
|
||||
}
|
||||
|
||||
context.saveGState()
|
||||
bezier.addClip()
|
||||
context.scaleBy(x: 1, y: -1)
|
||||
context.draw(image.cgImage!, in: f, byTiling: false)
|
||||
context.restoreGState()
|
||||
|
||||
}
|
||||
|
||||
//MARK: - Fill
|
||||
|
||||
private func addFill(bezier: UIBezierPath) {
|
||||
|
||||
guard let fillColor = fillColor else { return }
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
bezier.close()
|
||||
context.saveGState()
|
||||
fillColor.setFill()
|
||||
bezier.fill()
|
||||
context.restoreGState()
|
||||
}
|
||||
|
||||
//MARK: - Gradient
|
||||
|
||||
private func addGradient(bezier: UIBezierPath) {
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
var colors: [CGColor] = []
|
||||
|
||||
if let c = grColor1 { colors.append(c.cgColor) }
|
||||
if let c = grColor2 { colors.append(c.cgColor) }
|
||||
if let c = grColor3 { colors.append(c.cgColor) }
|
||||
if let c = grColor4 { colors.append(c.cgColor) }
|
||||
if let c = grColor5 { colors.append(c.cgColor) }
|
||||
if let c = grColor6 { colors.append(c.cgColor) }
|
||||
|
||||
if colors.count < 2 {
|
||||
colors.append(UIColor.clear.cgColor)
|
||||
colors.append(UIColor.clear.cgColor)
|
||||
} else if colors.count < 1 {
|
||||
if grColor1 != nil {
|
||||
colors.append(UIColor.clear.cgColor)
|
||||
} else {
|
||||
colors.insert(UIColor.clear.cgColor, at: 0)
|
||||
}
|
||||
}
|
||||
|
||||
guard let gradient = CGGradient(colorsSpace: nil,
|
||||
colors: colors as CFArray,
|
||||
locations: nil) else { return }
|
||||
|
||||
let bMode = CGBlendMode(rawValue: CGBlendMode.RawValue(grBlendMode)) ?? CGBlendMode.sourceAtop
|
||||
context.setBlendMode(bMode)
|
||||
|
||||
let options: CGGradientDrawingOptions =
|
||||
grDrawsOptions ? [ .drawsBeforeStartLocation, .drawsAfterEndLocation ] : [ ]
|
||||
|
||||
var grEndPointR: CGPoint = grEndPoint
|
||||
var grStartPointR: CGPoint = grStartPoint
|
||||
if grPointPercent {
|
||||
grEndPointR = CGPoint(x: (grEndPoint.x * frame.width), y: (grEndPoint.y * frame.height))
|
||||
grStartPointR = CGPoint(x: (grStartPoint.x * frame.width), y: (grStartPoint.y * frame.height))
|
||||
}
|
||||
|
||||
bezier.close()
|
||||
context.saveGState()
|
||||
bezier.addClip()
|
||||
|
||||
if grRadial {
|
||||
|
||||
let x: CGFloat = (grEndPointR.x - grStartPointR.x)
|
||||
let y: CGFloat = (grEndPointR.y - grStartPointR.y)
|
||||
let distance: CGFloat = sqrt((x * x) + (y * y))
|
||||
|
||||
context.drawRadialGradient(gradient,
|
||||
startCenter: grStartPointR,
|
||||
startRadius: 0,
|
||||
endCenter: grStartPointR,
|
||||
endRadius: distance,
|
||||
options: options)
|
||||
} else {
|
||||
|
||||
context.drawLinearGradient(gradient,
|
||||
start: grStartPointR,
|
||||
end: grEndPointR,
|
||||
options: options)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Shadow
|
||||
|
||||
private func addShadow() {
|
||||
|
||||
if shRadius < 0 { shRadius = 0 }
|
||||
|
||||
layer.shadowOffset = shOffset
|
||||
layer.shadowOpacity = 1.0
|
||||
layer.shadowRadius = shRadius
|
||||
layer.shadowColor = shColor.cgColor
|
||||
}
|
||||
|
||||
private func addShadowContext(bezier: UIBezierPath) {
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
bezier.close()
|
||||
|
||||
//// Shadow Declarations
|
||||
let shadow = NSShadow()
|
||||
shadow.shadowColor = shColor
|
||||
shadow.shadowOffset = shOffset
|
||||
shadow.shadowBlurRadius = shRadius * 2
|
||||
|
||||
context.saveGState()
|
||||
|
||||
context.setShadow(offset: shadow.shadowOffset,
|
||||
blur: shadow.shadowBlurRadius,
|
||||
color: (shadow.shadowColor as! UIColor).cgColor)
|
||||
|
||||
context.restoreGState()
|
||||
}
|
||||
|
||||
//MARK: - Inner Shadow
|
||||
|
||||
private func addInnerShadow(bezier: UIBezierPath) {
|
||||
|
||||
if inShRadius < 0 { inShRadius = 0 }
|
||||
|
||||
let innerShadow = NSShadow()
|
||||
innerShadow.shadowColor = inShColor
|
||||
innerShadow.shadowOffset = inShOffset
|
||||
innerShadow.shadowBlurRadius = inShRadius * 2
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
////// Star Inner Shadow
|
||||
context.saveGState()
|
||||
context.clip(to: bezier.bounds)
|
||||
context.setShadow(offset: .zero, blur: 0)
|
||||
context.setAlpha((innerShadow.shadowColor as! UIColor).cgColor.alpha)
|
||||
context.beginTransparencyLayer(auxiliaryInfo: nil)
|
||||
|
||||
let starOpaqueShadow = (innerShadow.shadowColor as! UIColor).withAlphaComponent(1)
|
||||
context.setShadow(offset: innerShadow.shadowOffset,
|
||||
blur: innerShadow.shadowBlurRadius,
|
||||
color: starOpaqueShadow.cgColor)
|
||||
|
||||
context.setBlendMode(.sourceOut)
|
||||
context.beginTransparencyLayer(auxiliaryInfo: nil)
|
||||
|
||||
starOpaqueShadow.setFill()
|
||||
|
||||
bezier.fill()
|
||||
|
||||
context.endTransparencyLayer()
|
||||
context.endTransparencyLayer()
|
||||
context.restoreGState()
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Blur
|
||||
|
||||
private func addBlur() {
|
||||
|
||||
guard blur > 0 else { return }
|
||||
|
||||
guard let image = screenShotContext() else { return }
|
||||
|
||||
self.addblur(screen: image, blur: blur)
|
||||
}
|
||||
|
||||
// MARK: Screen Shot Context
|
||||
|
||||
private func screenShotContext() -> UIImage? {
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return nil }
|
||||
guard let cgimage: CGImage = context.makeImage() else { return nil }
|
||||
let image = UIImage(cgImage: cgimage)
|
||||
|
||||
return image
|
||||
}
|
||||
|
||||
// MARK: Clear Context
|
||||
|
||||
private func clearContext() {
|
||||
|
||||
let f = CGRect(x: 0, y: 0,
|
||||
width: frame.width * 2,
|
||||
height: frame.height * 2)
|
||||
|
||||
let context = UIGraphicsGetCurrentContext()
|
||||
context?.clear(f)
|
||||
}
|
||||
|
||||
|
||||
private func addblur(screen: UIImage, blur: CGFloat) {
|
||||
|
||||
clearContext()
|
||||
|
||||
func blurImage(image: UIImage) -> UIImage? {
|
||||
|
||||
guard let ciscreen: CIImage = CIImage(image: screen) else { return nil }
|
||||
|
||||
guard let filter: CIFilter = CIFilter(name: "CIGaussianBlur") else { return nil }
|
||||
filter.setDefaults()
|
||||
filter.setValue(ciscreen, forKey: kCIInputImageKey)
|
||||
filter.setValue(blur, forKey: kCIInputRadiusKey)
|
||||
|
||||
|
||||
let bl2 = ((blur * 3) * -1)
|
||||
let bl4 = (blur * 6)
|
||||
|
||||
let contextFrame = CGRect(
|
||||
x: bl2,
|
||||
y: bl2,
|
||||
width: bl4 + image.size.width,
|
||||
height: bl4 + image.size.height
|
||||
)
|
||||
|
||||
// let bl2 = ((blur * 2) * -1)
|
||||
// let bl4 = (blur * 4)
|
||||
// let contextFrame2 = CGRect(x: bl2,
|
||||
// y: bl2,
|
||||
// width: bl4 + (frame.width * 2),
|
||||
// height: bl4 + (frame.height * 2))
|
||||
|
||||
let ciContext = CIContext(options: nil)
|
||||
guard let ci: CIImage = filter.value(forKey: kCIOutputImageKey) as? CIImage else { return nil }
|
||||
guard let cImg = ciContext.createCGImage(ci, from: contextFrame) else { return nil }
|
||||
|
||||
let finalImage = UIImage(cgImage: cImg)
|
||||
|
||||
return finalImage
|
||||
}
|
||||
|
||||
let p2: CGFloat = 1
|
||||
let p4: CGFloat = p2 * 2
|
||||
|
||||
let bl2 = ((blur * p2) * -1)
|
||||
let bl4 = (blur * p4)
|
||||
|
||||
let x: CGFloat = 0
|
||||
let ofW = (x * 1) * -1
|
||||
let ofH = (x * 1) * -1
|
||||
|
||||
let ofW2 = (x * 2)
|
||||
let ofH2 = (x * 2)
|
||||
|
||||
let contextFrame = CGRect(x: bl2 + ofW,
|
||||
y: bl2 + ofH,
|
||||
width: bl4 + ofW2 + frame.width,
|
||||
height: bl4 + ofH2 + frame.height)
|
||||
|
||||
let blurImageView = UIImageView(frame: contextFrame)
|
||||
blurImageView.image = blurImage(image: screen)
|
||||
blurImageView.contentMode = .scaleAspectFill
|
||||
|
||||
|
||||
addSubview(blurImageView)
|
||||
}
|
||||
|
||||
//MARK: - Rectangle Bezier
|
||||
|
||||
func rectangle(rect: CGRect, radius: CGFloat) -> UIBezierPath {
|
||||
|
||||
let r = self.cornerRadius(radius)
|
||||
let w = rect.width
|
||||
let h = rect.height
|
||||
|
||||
let path = UIBezierPath()
|
||||
path.move(to: CGPoint(x: r, y: 0.0))
|
||||
|
||||
path.addLine(to: CGPoint(x: w - r, y: 0.0))
|
||||
path.addArc(withCenter: CGPoint(x: w - r, y: r),
|
||||
radius: r,
|
||||
startAngle: 3.0 * .pi / 2.0,
|
||||
endAngle: 2 * .pi,
|
||||
clockwise: true)
|
||||
|
||||
path.addLine(to: CGPoint(x: w, y: h - r))
|
||||
path.addArc(withCenter: CGPoint(x: w - r, y: h - r),
|
||||
radius: r,
|
||||
startAngle: 0.0,
|
||||
endAngle: .pi / 2.0,
|
||||
clockwise: true)
|
||||
|
||||
path.addLine(to: CGPoint(x: r, y: h))
|
||||
path.addArc(withCenter: CGPoint(x: r, y: h - r),
|
||||
radius: r,
|
||||
startAngle: .pi / 2.0,
|
||||
endAngle: .pi,
|
||||
clockwise: true)
|
||||
|
||||
path.addLine(to: CGPoint(x: 0.0, y: r))
|
||||
path.addArc(withCenter: CGPoint(x: r, y: r),
|
||||
radius: r,
|
||||
startAngle: .pi,
|
||||
endAngle: 3.0 * .pi / 2.0,
|
||||
clockwise: true)
|
||||
|
||||
path.close()
|
||||
return path
|
||||
}
|
||||
|
||||
//MARK: - Star Bezier
|
||||
|
||||
func star(polygon: Bool = false, rect: CGRect, radius: CGFloat, pointsOnStar: Int) -> UIBezierPath {
|
||||
|
||||
let path = UIBezierPath()
|
||||
|
||||
let minSize = max(rect.width, rect.height)
|
||||
var starExtrusion: CGFloat = minSize * 0.18695652173
|
||||
if radius != 0 {
|
||||
starExtrusion = minSize * radius
|
||||
}
|
||||
|
||||
let center: CGPoint = .zero
|
||||
|
||||
var angle: CGFloat = -CGFloat(.pi / 2.0)
|
||||
let angleIncrement = CGFloat(.pi * 2.0 / Double(pointsOnStar))
|
||||
let radius = minSize / 2.0
|
||||
|
||||
var firstPoint = true
|
||||
|
||||
func pointFrom(angle: CGFloat, radius: CGFloat, offset: CGPoint) -> CGPoint {
|
||||
return CGPoint(x: radius * cos(angle) + offset.x,
|
||||
y: radius * sin(angle) + offset.y)
|
||||
}
|
||||
|
||||
for _ in 1...pointsOnStar {
|
||||
|
||||
let point: CGPoint = pointFrom(angle: angle, radius: radius, offset: center)
|
||||
let nextPoint: CGPoint = pointFrom(angle: angle + angleIncrement, radius: radius, offset: center)
|
||||
let midPoint: CGPoint = pointFrom(angle: angle + angleIncrement / 2.0, radius: starExtrusion, offset: center)
|
||||
|
||||
var perY: CGFloat = 1.0
|
||||
var perX: CGFloat = 1.0
|
||||
|
||||
if rect.height < rect.width {
|
||||
perY = (rect.height / minSize)
|
||||
} else {
|
||||
perX = (rect.width / minSize)
|
||||
}
|
||||
|
||||
let p = CGPoint(x: (point.x * perX) + (rect.width / 2.0),
|
||||
y: (point.y * perY) + (rect.height / 2.0))
|
||||
|
||||
let n = CGPoint(x: (nextPoint.x * perX) + (rect.width / 2.0),
|
||||
y: (nextPoint.y * perY) + (rect.height / 2.0))
|
||||
|
||||
let m = CGPoint(x: (midPoint.x * perX) + (rect.width / 2.0),
|
||||
y: (midPoint.y * perY) + (rect.height / 2.0))
|
||||
|
||||
if firstPoint {
|
||||
firstPoint = false
|
||||
path.move(to: p)
|
||||
}
|
||||
|
||||
if !polygon {
|
||||
path.addLine(to: m)
|
||||
}
|
||||
path.addLine(to: n)
|
||||
|
||||
angle += angleIncrement
|
||||
}
|
||||
|
||||
path.close()
|
||||
|
||||
return path
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
//
|
||||
// DesignImageView.swift
|
||||
// FigmaConvertXib
|
||||
//
|
||||
// Created by Рустам Мотыгуллин on 21.07.2020.
|
||||
// Copyright © 2020 mrusta. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
@IBDesignable
|
||||
class DesignImage: UIImageView {
|
||||
|
||||
@IBInspectable var cornerRadius: CGFloat = 0.0
|
||||
|
||||
/// Shadow
|
||||
@IBInspectable var shColor: UIColor = .clear
|
||||
@IBInspectable var shRadius: CGFloat = 0.0
|
||||
@IBInspectable var shOffset: CGSize = .zero
|
||||
|
||||
/// Border
|
||||
@IBInspectable var brColor: UIColor = .clear
|
||||
@IBInspectable var brWidth: CGFloat = 0.0
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
|
||||
super.draw(rect)
|
||||
|
||||
layer.backgroundColor = UIColor.black.cgColor
|
||||
|
||||
let l = CALayer()
|
||||
l.frame = rect
|
||||
l.borderColor = brColor.cgColor
|
||||
l.borderWidth = brWidth
|
||||
l.cornerRadius = cornerRadius
|
||||
layer.addSublayer(l)
|
||||
|
||||
add(shadow: shRadius, offset: shOffset, color: shColor)
|
||||
add(border: brWidth, color: brColor)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@IBDesignable
|
||||
class DesignImageView: UIView {
|
||||
|
||||
//MARK: Properties
|
||||
|
||||
@IBInspectable var image: UIImage?
|
||||
@IBInspectable var contModeFill: Bool = false
|
||||
|
||||
/// Radius
|
||||
@IBInspectable var cornerRadius: CGFloat = 0.0
|
||||
|
||||
/// Blur
|
||||
@IBInspectable var blur: CGFloat = 0.0
|
||||
|
||||
/// Fill
|
||||
@IBInspectable var fillColor: UIColor = .clear
|
||||
|
||||
/// Gradient
|
||||
@IBInspectable var grColor2: UIColor?
|
||||
@IBInspectable var grColor3: UIColor?
|
||||
@IBInspectable var grColor4: UIColor?
|
||||
@IBInspectable var grColor5: UIColor?
|
||||
@IBInspectable var grColor6: UIColor?
|
||||
|
||||
@IBInspectable var grStartPoint: CGPoint = .zero
|
||||
@IBInspectable var grEndPoint: CGPoint = .zero
|
||||
|
||||
/// Inner Shadow
|
||||
@IBInspectable var inShColor: UIColor = .clear
|
||||
@IBInspectable var inShRadius: CGFloat = 0.0
|
||||
@IBInspectable var inShOffset: CGSize = .zero
|
||||
|
||||
/// Shadow
|
||||
@IBInspectable var shColor: UIColor = .clear
|
||||
@IBInspectable var shRadius: CGFloat = 0.0
|
||||
@IBInspectable var shOffset: CGSize = .zero
|
||||
|
||||
/// Border
|
||||
@IBInspectable var brColor: UIColor = .clear
|
||||
@IBInspectable var brWidth: CGFloat = 0.0
|
||||
|
||||
var imageView: UIImageView!
|
||||
|
||||
|
||||
//MARK: - Draw
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
|
||||
let radius = self.cornerRadius(cornerRadius)
|
||||
|
||||
layer.cornerRadius = radius
|
||||
|
||||
layer.backgroundColor = fillColor.cgColor
|
||||
|
||||
addShadow()
|
||||
addBorder()
|
||||
|
||||
addImage()
|
||||
|
||||
addGradient()
|
||||
addInnerShadow()
|
||||
|
||||
add(blur: blur) { [weak self] in
|
||||
|
||||
guard let _self = self else { return }
|
||||
_self.imageView.setAlpha(.invisible)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func addGradient() {
|
||||
|
||||
guard let glayer = add(gradient: fillColor,
|
||||
color2: grColor2,
|
||||
color3: grColor3,
|
||||
color4: grColor4,
|
||||
color5: grColor5,
|
||||
color6: grColor6,
|
||||
pointStart: grStartPoint,
|
||||
pointEnd: grEndPoint,
|
||||
cornerRadius: cornerRadius) else { return }
|
||||
|
||||
imageView.layer.insertSublayer(glayer, at: 0)
|
||||
|
||||
}
|
||||
|
||||
private func addInnerShadow() {
|
||||
|
||||
addInnerShadow(color: inShColor,
|
||||
radius: inShRadius,
|
||||
offset: inShOffset,
|
||||
cornerRadius: cornerRadius)
|
||||
|
||||
}
|
||||
|
||||
private func addShadow() {
|
||||
layer.shadowOffset = shOffset
|
||||
layer.shadowOpacity = 1.0
|
||||
layer.shadowRadius = shRadius
|
||||
layer.shadowColor = shColor.cgColor
|
||||
}
|
||||
|
||||
private func addBorder() {
|
||||
layer.borderColor = brColor.cgColor
|
||||
layer.borderWidth = brWidth
|
||||
}
|
||||
|
||||
private func addImage() {
|
||||
let f = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
|
||||
imageView = UIImageView(frame: f)
|
||||
imageView.image = image
|
||||
imageView.contentMode = contModeFill ? .scaleAspectFill : .scaleAspectFit
|
||||
imageView.clipsToBounds = true
|
||||
imageView.layer.cornerRadius = self.cornerRadius(cornerRadius)
|
||||
insertSubview(imageView, at: 0)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// DesignInnerShadow.swift
|
||||
// FigmaConvertXib
|
||||
//
|
||||
// Created by Рустам Мотыгуллин on 12.02.2021.
|
||||
// Copyright © 2021 mrusta. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
@IBDesignable
|
||||
class DesignInnerShadow: UIView {
|
||||
|
||||
@IBInspectable var cornerRadius: CGFloat = 0.0
|
||||
@IBInspectable var inShColor: UIColor = .clear
|
||||
@IBInspectable var inShRadius: CGFloat = 0.0
|
||||
@IBInspectable var inShOffset: CGSize = .zero
|
||||
|
||||
@IBInspectable var fillColor: UIColor?
|
||||
|
||||
//MARK: - Draw
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
super.draw(rect)
|
||||
|
||||
addInnerShadow(rect)
|
||||
|
||||
layer.backgroundColor = fillColor?.cgColor
|
||||
layer.cornerRadius = cornerRadius
|
||||
}
|
||||
|
||||
//MARK: - Add Shadow
|
||||
|
||||
func addInnerShadow(_ rect: CGRect) {
|
||||
/// General Declarations
|
||||
|
||||
let context = UIGraphicsGetCurrentContext()!
|
||||
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
|
||||
|
||||
UIColor(white: 1.0, alpha: 0.00001).setFill()
|
||||
|
||||
path.fill()
|
||||
context.saveGState()
|
||||
context.clip(to: path.bounds)
|
||||
context.setAlpha(CGFloat(1.0))
|
||||
context.beginTransparencyLayer(auxiliaryInfo: nil)
|
||||
|
||||
|
||||
let innerShadow = NSShadow()
|
||||
innerShadow.shadowColor = inShColor
|
||||
innerShadow.shadowOffset = inShOffset
|
||||
innerShadow.shadowBlurRadius = inShRadius * 2
|
||||
|
||||
context.setShadow(offset: inShOffset,
|
||||
blur: inShRadius,
|
||||
color: inShColor.cgColor)
|
||||
context.setBlendMode(.sourceOut)
|
||||
context.beginTransparencyLayer(auxiliaryInfo: nil)
|
||||
let ovalOpaqueShadow = UIColor.init(cgColor: inShColor.cgColor)
|
||||
ovalOpaqueShadow.setFill()
|
||||
path.fill()
|
||||
context.endTransparencyLayer()
|
||||
context.endTransparencyLayer()
|
||||
context.restoreGState()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
//
|
||||
// DesignLabel.swift
|
||||
// FigmaConvertXib
|
||||
//
|
||||
// Created by Рустам Мотыгуллин on 25.07.2020.
|
||||
// Copyright © 2020 mrusta. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
@IBDesignable
|
||||
class DesignLabel: UILabel {
|
||||
|
||||
//MARK: Gradient
|
||||
@IBInspectable var grColor1: UIColor?
|
||||
@IBInspectable var grColor2: UIColor?
|
||||
@IBInspectable var grColor3: UIColor?
|
||||
@IBInspectable var grColor4: UIColor?
|
||||
@IBInspectable var grColor5: UIColor?
|
||||
@IBInspectable var grColor6: UIColor?
|
||||
|
||||
@IBInspectable var grStartPoint: CGPoint = .zero
|
||||
@IBInspectable var grEndPoint: CGPoint = .zero
|
||||
|
||||
@IBInspectable var grRadial: Bool = false /// default: linear
|
||||
@IBInspectable var grDrawsOptions: Bool = true
|
||||
@IBInspectable var grDebug: Bool = false
|
||||
@IBInspectable var grPointPercent: Bool = true
|
||||
@IBInspectable var grBlendMode: Int = 20 /// заполнители
|
||||
|
||||
//MARK: - Shadow
|
||||
|
||||
@IBInspectable var shColor: UIColor = .clear
|
||||
@IBInspectable var shRadius: CGFloat = 0.0
|
||||
@IBInspectable var shOffset: CGSize = .zero
|
||||
|
||||
//MARK: Border
|
||||
|
||||
@IBInspectable var brColor: UIColor = .clear
|
||||
@IBInspectable var brWidth: CGFloat = 0.0
|
||||
|
||||
//MARK: - Draw
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
super.draw(rect)
|
||||
shadow()
|
||||
gradient()
|
||||
gradientDebug()
|
||||
border()
|
||||
}
|
||||
|
||||
//MARK: - Add Shadow
|
||||
|
||||
private func shadow() {
|
||||
layer.shadowOpacity = 1.0
|
||||
layer.shadowOffset = shOffset
|
||||
layer.shadowRadius = shRadius
|
||||
layer.shadowColor = shColor.cgColor
|
||||
}
|
||||
|
||||
//MARK: - Add Border
|
||||
|
||||
func border() {
|
||||
|
||||
guard brColor != .clear, brWidth != 0.0 else { return }
|
||||
|
||||
let borderLabel = UILabel(frame: bounds)
|
||||
borderLabel.text = text
|
||||
borderLabel.font = font
|
||||
borderLabel.textColor = .clear
|
||||
borderLabel.numberOfLines = numberOfLines
|
||||
borderLabel.backgroundColor = .clear
|
||||
|
||||
|
||||
let strokeTextAttributes: [NSAttributedString.Key : Any] = [
|
||||
.strokeColor : brColor,
|
||||
.strokeWidth : (-1 * (brWidth * 2)),
|
||||
]
|
||||
|
||||
borderLabel.attributedText = NSAttributedString(string: text ?? "",
|
||||
attributes: strokeTextAttributes)
|
||||
|
||||
addSubview(borderLabel)
|
||||
}
|
||||
|
||||
//MARK: - Add Gradient
|
||||
|
||||
func gradient() {
|
||||
var colors: [CGColor] = []
|
||||
|
||||
if grColor1 != nil || grColor2 != nil || grColor3 != nil || grColor4 != nil || grColor5 != nil {
|
||||
|
||||
if let color1 = grColor1 { colors.append(color1.cgColor) }
|
||||
if let color2 = grColor2 { colors.append(color2.cgColor) }
|
||||
if let color3 = grColor3 { colors.append(color3.cgColor) }
|
||||
if let color4 = grColor4 { colors.append(color4.cgColor) }
|
||||
if let color5 = grColor5 { colors.append(color5.cgColor) }
|
||||
if let color6 = grColor6 { colors.append(color6.cgColor) }
|
||||
|
||||
} else { return }
|
||||
|
||||
guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
|
||||
colors: colors as CFArray,
|
||||
locations: nil) else { return }
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
|
||||
let bMode = CGBlendMode(rawValue: CGBlendMode.RawValue(grBlendMode)) ?? CGBlendMode.sourceAtop
|
||||
context.setBlendMode(bMode)
|
||||
|
||||
let options: CGGradientDrawingOptions =
|
||||
grDrawsOptions ? [ .drawsBeforeStartLocation, .drawsAfterEndLocation ] : [ ]
|
||||
|
||||
|
||||
if grPointPercent {
|
||||
grEndPoint = CGPoint(x: grEndPoint.x * frame.width, y: grEndPoint.y * frame.height)
|
||||
grStartPoint = CGPoint(x: grStartPoint.x * frame.width, y: grStartPoint.y * frame.height)
|
||||
}
|
||||
|
||||
//MARK: Gr. Radial / Linear
|
||||
if grRadial {
|
||||
let x: CGFloat = (grEndPoint.x - grStartPoint.x)
|
||||
let y: CGFloat = (grEndPoint.y - grStartPoint.y)
|
||||
let distance: CGFloat = sqrt((x * x) + (y * y))
|
||||
|
||||
context.drawRadialGradient(gradient,
|
||||
startCenter: grStartPoint,
|
||||
startRadius: 0,
|
||||
endCenter: grStartPoint,
|
||||
endRadius: distance,
|
||||
options: options)
|
||||
} else {
|
||||
context.drawLinearGradient(gradient,
|
||||
start: grStartPoint,
|
||||
end: grEndPoint,
|
||||
options: options)
|
||||
}
|
||||
context.saveGState()
|
||||
|
||||
}
|
||||
|
||||
//MARK: - Gr. Debug
|
||||
|
||||
func gradientDebug() {
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return }
|
||||
guard grDebug else { return }
|
||||
|
||||
context.setBlendMode(CGBlendMode.normal)
|
||||
context.setLineWidth(3.0)
|
||||
context.setFillColor(UIColor.black.cgColor)
|
||||
context.setStrokeColor(UIColor.systemRed.cgColor)
|
||||
|
||||
let ellipseRadius: CGFloat = 5
|
||||
let circleRect = CGRect(x: grStartPoint.x - ellipseRadius,
|
||||
y: grStartPoint.y - ellipseRadius,
|
||||
width: ellipseRadius * 2,
|
||||
height: ellipseRadius * 2)
|
||||
|
||||
context.fillEllipse(in: circleRect)
|
||||
context.strokeEllipse(in: circleRect)
|
||||
context.setStrokeColor(UIColor.green.cgColor)
|
||||
|
||||
let circleRect2 = CGRect(x: grEndPoint.x - ellipseRadius,
|
||||
y: grEndPoint.y - ellipseRadius,
|
||||
width: ellipseRadius * 2,
|
||||
height: ellipseRadius * 2)
|
||||
|
||||
context.fillEllipse(in: circleRect2)
|
||||
context.strokeEllipse(in: circleRect2)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
//
|
||||
// DesignTest.swift
|
||||
// FigmaConvertXib
|
||||
//
|
||||
// Created by Рустам Мотыгуллин on 28.07.2020.
|
||||
// Copyright © 2020 mrusta. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
private extension UIButton {
|
||||
|
||||
func style(selected: Bool) {
|
||||
if selected {
|
||||
self.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
|
||||
} else {
|
||||
self.setTitleColor(#colorLiteral(red: 0.3098039216, green: 0.3098039216, blue: 0.3098039216, alpha: 1), for: .normal)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private extension UIView {
|
||||
|
||||
func selectedView() {
|
||||
|
||||
self.layer.cornerRadius = 8
|
||||
self.layer.backgroundColor = #colorLiteral(red: 0.9960784314, green: 0.3137254902, blue: 0, alpha: 1).cgColor
|
||||
|
||||
self.add(shadow: 6 / 2, offset: CGSize(width: -4, height: -4), color: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))
|
||||
|
||||
self.addInnerShadow(color: #colorLiteral(red: 0.4784313725, green: 0.1960784314, blue: 0.01568627451, alpha: 0.3674445752),
|
||||
radius: 9 / 2,
|
||||
offset: CGSize(width: -4, height: -4),
|
||||
cornerRadius: 8)
|
||||
|
||||
self.addInnerShadow(color: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.3720056395),
|
||||
radius: 8 / 2,
|
||||
offset: CGSize(width: 4, height: 4),
|
||||
cornerRadius: 8)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@IBDesignable
|
||||
class DesignSegmentBar: UIView {
|
||||
|
||||
@IBInspectable var textItems: String = ""
|
||||
@IBInspectable var index: Int = 0
|
||||
|
||||
private var views: [UIView] = []
|
||||
private var buttons: [UIButton] = []
|
||||
private var stackView: UIStackView!
|
||||
private var seView: UIImageView!
|
||||
|
||||
var changeIndex: ((Int) -> ())?
|
||||
|
||||
override func draw(_ rect: CGRect) {
|
||||
super.draw(rect)
|
||||
|
||||
layer.cornerRadius = 8
|
||||
layer.backgroundColor = #colorLiteral(red: 0.9411764706, green: 0.9411764706, blue: 0.9529411765, alpha: 1).cgColor
|
||||
|
||||
for view in buttons {
|
||||
view.removeFromSuperview()
|
||||
}
|
||||
for view in views {
|
||||
view.removeFromSuperview()
|
||||
}
|
||||
for view in subviews {
|
||||
view.removeFromSuperview()
|
||||
}
|
||||
|
||||
let items = textItems.components(separatedBy: ",")
|
||||
let width = (self.frame.width / CGFloat(items.count))
|
||||
let frame = CGRect(x: (width * CGFloat(index)), y: 0, width: width, height: self.frame.height)
|
||||
|
||||
views = []
|
||||
buttons = []
|
||||
|
||||
for i in 0..<items.count {
|
||||
|
||||
let x = ((width * CGFloat(i+1)) - 0.5)
|
||||
|
||||
if (i+1) != items.count {
|
||||
let separatorView = UIView(frame: CGRect(x: x, y: 11, width: 1, height: self.frame.height - 22))
|
||||
separatorView.backgroundColor = #colorLiteral(red: 0.7411764706, green: 0.7411764706, blue: 0.7411764706, alpha: 1)
|
||||
separatorView.layer.cornerRadius = 0.5
|
||||
addSubview(separatorView)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var i = 0
|
||||
for oneText in items {
|
||||
let x = (width * CGFloat(i))
|
||||
|
||||
let view = UIView(frame: frame)
|
||||
view.set(x: x)
|
||||
let oneButton = UIButton(type: .system)
|
||||
oneButton.frame = view.bounds
|
||||
view.addSubview(oneButton)
|
||||
|
||||
/// Btn Size
|
||||
|
||||
view.backgroundColor = .clear
|
||||
oneButton.backgroundColor = .clear
|
||||
oneButton.style(selected: false)
|
||||
|
||||
/// Btn
|
||||
oneButton.setTitle(oneText , for: .normal)
|
||||
oneButton.contentVerticalAlignment = .center
|
||||
|
||||
if let font = UIFont(name: "TTNorms-Regular", size: 18) {
|
||||
oneButton.titleLabel?.font = font
|
||||
}
|
||||
|
||||
if i == index {
|
||||
oneButton.style(selected: true)
|
||||
}
|
||||
|
||||
oneButton.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
|
||||
|
||||
//oneButton.set(width: width)
|
||||
views.append(view)
|
||||
buttons.append(oneButton)
|
||||
//self.addSubview(oneButton)
|
||||
i += 1
|
||||
}
|
||||
|
||||
let stackView = UIStackView()
|
||||
stackView.axis = .horizontal
|
||||
stackView.distribution = .fillEqually
|
||||
|
||||
for btn in views {
|
||||
stackView.addArrangedSubview(btn)
|
||||
}
|
||||
|
||||
|
||||
seView = UIImageView(frame: frame)
|
||||
seView.selectedView()
|
||||
addSubview(seView)
|
||||
|
||||
stackView.translatesAutoresizingMaskIntoConstraints = false
|
||||
self.addSubview(stackView)
|
||||
|
||||
stackView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
|
||||
stackView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
|
||||
stackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
|
||||
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
|
||||
self.stackView = stackView
|
||||
}
|
||||
|
||||
@objc func buttonAction(_ button: UIButton) {
|
||||
|
||||
var i = 0
|
||||
for btn in buttons {
|
||||
btn.style(selected: false)
|
||||
|
||||
if btn == button {
|
||||
self.index = i
|
||||
changeIndex?(i)
|
||||
let x = (btn.frame.width * CGFloat(i))
|
||||
UIView.animate(withDuration: 0.35) {
|
||||
self.seView.set(x: x)
|
||||
}
|
||||
btn.style(selected: true)
|
||||
}
|
||||
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
//
|
||||
// DesignViewExtentions.swift
|
||||
// FigmaConvertXib
|
||||
//
|
||||
// Created by Рустам Мотыгуллин on 20.07.2020.
|
||||
// Copyright © 2020 mrusta. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIView {
|
||||
func add(shadow radius: CGFloat, offset: CGSize, color: UIColor) {
|
||||
layer.shadowOpacity = 1.0
|
||||
layer.shadowColor = color.cgColor
|
||||
layer.shadowOffset = offset
|
||||
layer.shadowRadius = radius
|
||||
}
|
||||
|
||||
func add(border width: CGFloat, color: UIColor) {
|
||||
layer.borderColor = color.cgColor
|
||||
layer.borderWidth = width
|
||||
}
|
||||
|
||||
//MARK: - Blur
|
||||
|
||||
func add(blur: CGFloat, rect: CGRect? = nil, clearCallback: (() -> Void)? = nil) {
|
||||
|
||||
guard blur > 0 else { return }
|
||||
|
||||
layer.shadowOpacity = 0.0
|
||||
var fr = bounds
|
||||
if let rect = rect {
|
||||
fr = CGRect(x: 0, y: 0, width: rect.width, height: rect.height)
|
||||
}
|
||||
|
||||
UIGraphicsBeginImageContext(fr.size)
|
||||
layer.render(in: UIGraphicsGetCurrentContext()!)
|
||||
guard let imgC = UIGraphicsGetImageFromCurrentImageContext() else { return }
|
||||
UIGraphicsEndImageContext()
|
||||
|
||||
self.blur(screen: imgC, blur: blur, clearCallback: clearCallback)
|
||||
}
|
||||
|
||||
func blur(screen: UIImage, blur: CGFloat, clearCallback: (() -> Void)? = nil) {
|
||||
|
||||
guard let ciscreen: CIImage = CIImage(image: screen) else { return }
|
||||
|
||||
guard let filter: CIFilter = CIFilter(name: "CIGaussianBlur") else { return }
|
||||
filter.setDefaults()
|
||||
filter.setValue(ciscreen, forKey: kCIInputImageKey)
|
||||
filter.setValue(blur, forKey: kCIInputRadiusKey)
|
||||
|
||||
let bl2 = ((blur * 2) * -1)
|
||||
let bl4 = (blur * 4)
|
||||
|
||||
let ofW = (layer.shadowOffset.width * 1) * -1
|
||||
let ofH = (layer.shadowOffset.height * 1) * -1
|
||||
|
||||
let ofW2 = (layer.shadowOffset.width * 2)
|
||||
let ofH2 = (layer.shadowOffset.height * 2)
|
||||
|
||||
let contextFrame = CGRect(x: bl2 + ofW,
|
||||
y: bl2 + ofH,
|
||||
width: frame.width + bl4 + ofW2,
|
||||
height: frame.height + bl4 + ofH2)
|
||||
|
||||
let ciContext = CIContext(options: nil)
|
||||
guard let r = filter.value(forKey: kCIOutputImageKey) as? CIImage else { return }
|
||||
guard let cImg = ciContext.createCGImage(r, from: contextFrame) else { return }
|
||||
|
||||
let finalImage = UIImage(cgImage: cImg)
|
||||
|
||||
let imageFrame = CGRect(x: bl2 + ofW,
|
||||
y: bl2 + ofH,
|
||||
width: frame.width + bl4 + ofW2,
|
||||
height: frame.height + bl4 + ofH2)
|
||||
|
||||
let blurImageView = UIImageView(frame: imageFrame)
|
||||
blurImageView.image = finalImage
|
||||
blurImageView.contentMode = .scaleAspectFit
|
||||
|
||||
clearAllFills()
|
||||
|
||||
clearCallback?()
|
||||
|
||||
addSubview(blurImageView)
|
||||
|
||||
}
|
||||
|
||||
//MARK: - Blur Clear All
|
||||
|
||||
func clearAllFills() {
|
||||
|
||||
if let sublayers = layer.sublayers {
|
||||
for layer in sublayers {
|
||||
|
||||
layer.backgroundColor = UIColor.clear.cgColor
|
||||
if let gradientLayer = layer as? CAGradientLayer {
|
||||
gradientLayer.removeFromSuperlayer()
|
||||
} else if layer.name == "InnerShadow" {
|
||||
layer.removeFromSuperlayer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
layer.backgroundColor = UIColor.clear.cgColor
|
||||
|
||||
func clearShadow() {
|
||||
layer.shadowOffset = .zero
|
||||
layer.shadowOpacity = 0.0
|
||||
layer.shadowRadius = 0.0
|
||||
layer.shadowColor = UIColor.clear.cgColor
|
||||
}
|
||||
|
||||
func clearBorder() {
|
||||
layer.borderColor = UIColor.clear.cgColor
|
||||
layer.borderWidth = 0.0
|
||||
}
|
||||
|
||||
clearBorder()
|
||||
}
|
||||
|
||||
//MARK: - Inner Shadow
|
||||
|
||||
func addInnerShadow(color: UIColor,
|
||||
radius: CGFloat,
|
||||
offset: CGSize,
|
||||
cornerRadius: CGFloat,
|
||||
alpha: Float = 1.0) {
|
||||
|
||||
let cornerRadius = self.cornerRadius(cornerRadius)
|
||||
|
||||
let innerShadow = CALayer()
|
||||
innerShadow.name = "InnerShadow"
|
||||
innerShadow.frame = bounds
|
||||
|
||||
let path = UIBezierPath(roundedRect: innerShadow.bounds.insetBy(dx: -bounds.width, dy: -bounds.height), cornerRadius:cornerRadius)
|
||||
let cutout = UIBezierPath(roundedRect: innerShadow.bounds, cornerRadius:cornerRadius).reversing()
|
||||
|
||||
path.append(cutout)
|
||||
innerShadow.shadowPath = path.cgPath
|
||||
innerShadow.masksToBounds = true
|
||||
|
||||
innerShadow.shadowColor = color.cgColor
|
||||
innerShadow.shadowOffset = offset
|
||||
innerShadow.shadowRadius = radius
|
||||
innerShadow.cornerRadius = cornerRadius
|
||||
innerShadow.shadowOpacity = alpha
|
||||
layer.addSublayer(innerShadow)
|
||||
}
|
||||
|
||||
func add(gradient
|
||||
color1: UIColor? = nil,
|
||||
color2: UIColor? = nil,
|
||||
color3: UIColor? = nil,
|
||||
color4: UIColor? = nil,
|
||||
color5: UIColor? = nil,
|
||||
color6: UIColor? = nil,
|
||||
pointStart: CGPoint,
|
||||
pointEnd: CGPoint,
|
||||
cornerRadius: CGFloat) -> CAGradientLayer? {
|
||||
|
||||
if color1 != nil || color2 != nil || color3 != nil || color4 != nil || color5 != nil || color6 != nil {
|
||||
|
||||
var colors: [CGColor] = []
|
||||
// colors.append(fillColor.cgColor)
|
||||
if let color1 = color1 { colors.append(color1.cgColor) }
|
||||
if let color2 = color2 { colors.append(color2.cgColor) }
|
||||
if let color3 = color3 { colors.append(color3.cgColor) }
|
||||
if let color4 = color4 { colors.append(color4.cgColor) }
|
||||
if let color5 = color5 { colors.append(color5.cgColor) }
|
||||
if let color6 = color6 { colors.append(color6.cgColor) }
|
||||
|
||||
let glayer = CAGradientLayer()
|
||||
glayer.name = "Gradient"
|
||||
glayer.frame = bounds
|
||||
glayer.colors = colors
|
||||
glayer.startPoint = pointStart
|
||||
glayer.endPoint = pointEnd
|
||||
glayer.cornerRadius = self.cornerRadius(cornerRadius)
|
||||
layer.insertSublayer(glayer, at: 0)
|
||||
|
||||
layer.backgroundColor = UIColor.clear.cgColor
|
||||
|
||||
return glayer
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
//MARK: - Check CornerRadius
|
||||
|
||||
func cornerRadius(_ cornerRadius: CGFloat) -> CGFloat {
|
||||
let minSize = min(frame.size.width, frame.size.height)
|
||||
let minSizeRadius = (minSize / 2)
|
||||
let radius = ((cornerRadius < 0) || (minSizeRadius < cornerRadius) ? minSizeRadius : cornerRadius)
|
||||
return radius
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user