Draw an apple using SwiftUI and path

Hello

I have been trying to draw an apple with SwiftUI using path, but I can't make it good as it looks like a circle. Can someone tell me how to do it?

Thank you

Answered by Jad-T in 740074022

Here is an example I've written a few month ago:

import SwiftUI

struct Apple: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            let width = rect.width
            let height = rect.height
            
            //Draw the apple's body
            path.move(to: CGPoint(x: width * 0.55, y: height * 0.31))
            path.addCurve(
                to: CGPoint(x: width * 0.56, y: height * 0.96),
                control1: CGPoint(x: width * 0.85, y: height * 0.15),
                control2: CGPoint(x: width * 0.7, y: height * 1.1)
            )
            path.addCurve(
                to: CGPoint(x: width * 0.55, y: height * 0.31),
                control1: CGPoint(x: width * 0.36, y: height * 1.15),
                control2: CGPoint(x: width * 0.24, y: height * 0.18)
            )
        }
    }
}

struct AppleView: View {
    var body: some View {
        ZStack {
            Apple()
                .fill(.red)
                .frame(height: 210, alignment: .center)
            
            //Draw the leaf of the apple
            Ellipse()
                .fill(.green)
                .rotationEffect(.degrees(45))
                .offset(x: 30, y: -56)
                .frame(width: 20, height: 50, alignment: .center)
        }
    }
}
Accepted Answer

Here is an example I've written a few month ago:

import SwiftUI

struct Apple: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            let width = rect.width
            let height = rect.height
            
            //Draw the apple's body
            path.move(to: CGPoint(x: width * 0.55, y: height * 0.31))
            path.addCurve(
                to: CGPoint(x: width * 0.56, y: height * 0.96),
                control1: CGPoint(x: width * 0.85, y: height * 0.15),
                control2: CGPoint(x: width * 0.7, y: height * 1.1)
            )
            path.addCurve(
                to: CGPoint(x: width * 0.55, y: height * 0.31),
                control1: CGPoint(x: width * 0.36, y: height * 1.15),
                control2: CGPoint(x: width * 0.24, y: height * 0.18)
            )
        }
    }
}

struct AppleView: View {
    var body: some View {
        ZStack {
            Apple()
                .fill(.red)
                .frame(height: 210, alignment: .center)
            
            //Draw the leaf of the apple
            Ellipse()
                .fill(.green)
                .rotationEffect(.degrees(45))
                .offset(x: 30, y: -56)
                .frame(width: 20, height: 50, alignment: .center)
        }
    }
}
Draw an apple using SwiftUI and path
 
 
Q