Swift绘制托马斯小火车的脸

2023-12-12  本文已影响0人  大成小栈
绘制托马斯

代码逻辑,如下:

class ViewController: UIViewController { 

override func viewDidLoad() {
        super.viewDidLoad()
        
        // 画脸
        let headView = CartoonHeadView(frame: CGRect(x: 40, y: 0, width: 300, height: 300))
        view.addSubview(headView)
        
        // 画个圈
        if let image = createRingImage() {
            let imageView = UIImageView(image: image)
            imageView.frame = CGRect(x: 40, y: 0, width: 300, height: 300)
            imageView.contentMode = .scaleAspectFit
             view.addSubview(imageView)
        }
    }
    
    func createRingImage() -> UIImage? {
        let size = CGSize(width: 300, height: 300)
        let radius: CGFloat = 100
        let strokeWidth: CGFloat = 20

        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }

        // 绘制透明背景
        context.setFillColor(UIColor.clear.cgColor)
        context.fill(CGRect(origin: .zero, size: size))

        // 绘制白色圆环
        let circleRect = CGRect(x: (size.width - 2 * radius) / 2, y: (size.height - 2 * radius) / 2, width: 2 * radius, height: 2 * radius)
        context.setStrokeColor(UIColor.white.cgColor)
        context.setLineWidth(strokeWidth)
        context.strokeEllipse(in: circleRect)

        // 从绘图上下文获取图像
        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image
    }

}


extension ViewController {
    class CartoonHeadView: UIView {

        override func draw(_ rect: CGRect) {
            super.draw(rect)

            guard let context = UIGraphicsGetCurrentContext() else { return }

            // 画头部
            let headPath = UIBezierPath(ovalIn: CGRect(x: 50, y: 50, width: 200, height: 200))
            context.setFillColor(UIColor.brown.cgColor)
            context.addPath(headPath.cgPath)
            context.fillPath()

            // 画眼睛
            let leftEyePath = UIBezierPath(ovalIn: CGRect(x: 100, y: 100, width: 40, height: 40))
            context.setFillColor(UIColor.white.cgColor)
            context.addPath(leftEyePath.cgPath)
            context.fillPath()

            let rightEyePath = UIBezierPath(ovalIn: CGRect(x: 160, y: 100, width: 40, height: 40))
            context.setFillColor(UIColor.white.cgColor)
            context.addPath(rightEyePath.cgPath)
            context.fillPath()

            // 画眼珠
            let leftPupilPath = UIBezierPath(ovalIn: CGRect(x: 120, y: 120, width: 20, height: 20))
            context.setFillColor(UIColor.black.cgColor)
            context.addPath(leftPupilPath.cgPath)
            context.fillPath()

            let rightPupilPath = UIBezierPath(ovalIn: CGRect(x: 180, y: 120, width: 20, height: 20))
            context.setFillColor(UIColor.black.cgColor)
            context.addPath(rightPupilPath.cgPath)
            context.fillPath()

            // 画鼻子
            let nosePath = UIBezierPath()
            nosePath.move(to: CGPoint(x: 150, y: 150))
            nosePath.addLine(to: CGPoint(x: 130, y: 170))
            nosePath.addLine(to: CGPoint(x: 150, y: 170))
            nosePath.close()
            context.setFillColor(UIColor.black.cgColor)
            context.addPath(nosePath.cgPath)
            context.fillPath()

            // 画嘴巴
            let mouthPath = UIBezierPath()
            mouthPath.move(to: CGPoint(x: 120, y: 190))
            mouthPath.addCurve(to: CGPoint(x: 180, y: 190), controlPoint1: CGPoint(x: 120, y: 200), controlPoint2: CGPoint(x: 180, y: 200))
            context.setLineWidth(5)
            context.setStrokeColor(UIColor.black.cgColor)
            context.addPath(mouthPath.cgPath)
            context.strokePath()
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读