ios - 具有弯曲中心的 Swift 4 View

标签 ios swift view uibezierpath custom-view

我在创建 UI 时遇到困难。我试图创建自定义 View ,但我什至看不到该 View 的背景。这是我的示例代码,我从另一篇文章中获取了它,但我不知道如何将其更改为我的情况。

enter image description here

我确实将 SnapKit 用于我的 UI 元素。

我的看法:

lazy var greenView: CurvedView = {
    let view = CurvedView()
    view.backgroundColor = #colorLiteral(red: 0.08778516203, green: 0.7643524408, blue: 0.1997725368, alpha: 1)
    DispatchQueue.main.async {
        view.snp.makeConstraints{ (make) -> Void in
            make.top.equalTo(self.shipView.snp.bottom).offset(100)
            make.left.right.equalTo(self.scrollContentView)
            make.height.equalTo(200)
        }
    }
    return view
}()

尝试创建自定义 View :

class CurvedView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        StyleKitName.drawCanvas1(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400), resizing: .aspectFit)
        backgroundColor = .red
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }
}

已更新 带有 PaintCode 的绿色自定义 View

    public class StyleKitName : UIView {

    //// Drawing Methods

    @objc dynamic public class func drawCanvas1(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 240, height: 120), resizing: ResizingBehavior = .aspectFit) {
        //// General Declarations
        let context = UIGraphicsGetCurrentContext()!

        //// Resize to Target Frame
        context.saveGState()
        let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 240, height: 120), target: targetFrame)
        context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY)
        context.scaleBy(x: resizedFrame.width / 240, y: resizedFrame.height / 120)


        //// Color Declarations
        let color = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
        let color2 = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)

        //// Rectangle Drawing
        let rectanglePath = UIBezierPath()
        rectanglePath.move(to: CGPoint(x: 48, y: 103))
        rectanglePath.addLine(to: CGPoint(x: 191, y: 103))
        rectanglePath.addLine(to: CGPoint(x: 191, y: 9))
        rectanglePath.addLine(to: CGPoint(x: 48, y: 9))
        rectanglePath.addLine(to: CGPoint(x: 48, y: 103))
        rectanglePath.close()
        color.setFill()
        rectanglePath.fill()


        //// Bezier Drawing
        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: 96, y: 9))
        bezierPath.addCurve(to: CGPoint(x: 110, y: 15), controlPoint1: CGPoint(x: 104.11, y: 9.42), controlPoint2: CGPoint(x: 107, y: 12))
        bezierPath.addCurve(to: CGPoint(x: 123, y: 23), controlPoint1: CGPoint(x: 114.16, y: 19.16), controlPoint2: CGPoint(x: 115.63, y: 23))
        bezierPath.addCurve(to: CGPoint(x: 136, y: 15), controlPoint1: CGPoint(x: 129.95, y: 23), controlPoint2: CGPoint(x: 132, y: 19))
        bezierPath.addCurve(to: CGPoint(x: 152, y: 9), controlPoint1: CGPoint(x: 139.3, y: 11.7), controlPoint2: CGPoint(x: 145.35, y: 9))
        bezierPath.addCurve(to: CGPoint(x: 96, y: 9), controlPoint1: CGPoint(x: 166.7, y: 9), controlPoint2: CGPoint(x: 96, y: 9))
        bezierPath.addLine(to: CGPoint(x: 148.61, y: 9))
        color2.setFill()
        bezierPath.fill()

        context.restoreGState()

    }




    @objc(StyleKitNameResizingBehavior)
    public enum ResizingBehavior: Int {
        case aspectFit /// The content is proportionally resized to fit into the target rectangle.
        case aspectFill /// The content is proportionally resized to completely fill the target rectangle.
        case stretch /// The content is stretched to match the entire target rectangle.
        case center /// The content is centered in the target rectangle, but it is NOT resized.

        public func apply(rect: CGRect, target: CGRect) -> CGRect {
            if rect == target || target == CGRect.zero {
                return rect
            }

            var scales = CGSize.zero
            scales.width = abs(target.width / rect.width)
            scales.height = abs(target.height / rect.height)

            switch self {
            case .aspectFit:
                scales.width = min(scales.width, scales.height)
                scales.height = scales.width
            case .aspectFill:
                scales.width = max(scales.width, scales.height)
                scales.height = scales.width
            case .stretch:
                break
            case .center:
                scales.width = 1
                scales.height = 1
            }

            var result = rect.standardized
            result.size.width *= scales.width
            result.size.height *= scales.height
            result.origin.x = target.minX + (target.width - result.width) / 2
            result.origin.y = target.minY + (target.height - result.height) / 2
            return result
        }
    }
}

https://d.radikal.ru/d30/1902/c5/c07cac5532d0.png

最佳答案

这是使用 PaintCode 生成的代码的基本方法...

class StyleView: UIView {
    override func draw(_ rect: CGRect) {
        StyleKitName.drawCanvas1()
    }
}
class StyleTestViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        let v = StyleView()
        v.frame = CGRect(x: 0, y: 0, width: 240, height: 120)
        v.center = view.center
        v.backgroundColor = .blue
        view.addSubview(v)

    }

}

结果:

enter image description here

关于ios - 具有弯曲中心的 Swift 4 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54910805/

相关文章:

ios - 什么时候在 iOS 开发中使用多线程?

ios - 为什么显示 "ld: framework not found Bolts"

ios - 是否有必要在 UIView.animateWithDuration(...) 的闭包中使用 [unowned self]?

ios - 我正在尝试在 Swift 中添加 MDCCard 以使用 Material Components 获取 Material Card View。错误 : No such module 'MaterialComponents.MaterialCards'

android - 通过相机使用动态坐标捕获图像

iphone - 将文本添加到现有的 txt 文件

ios - 如何计算 UIView 相对于 iPad 上背景图像的位置

swift - iOS Swift - UITableViewCell 自定义子类不显示内容

android - 我可以在 Android 中缩放 View 吗?

Android:在 View 上应用模糊效果 (BlurMaskFilter)