14.4 C
Canberra
Tuesday, July 22, 2025

ios – Drawing a number of gradients and internal shadow in SwiftUI


How can I create the metallic rim gradient in addition to internal shadow as seen within the picture under?

sample metallic dish

Here is my code:

import SwiftUI

struct GradientDishView: View {
    non-public let containerSize: CGFloat = 300

    var physique: some View {
        VStack {
            Spacer()
            
            ZStack {
                
                Canvas { context, dimension in
                    
                    let currentCenter = CGPoint(x: containerSize / 2, y: containerSize / 2)
                    
                    let baseRadius = containerSize / 2
                    let innerRimRadius = baseRadius * 0.90
                    let fillRadius = innerRimRadius
                    let rimWidth = baseRadius - innerRimRadius
                    
                    // internal fill
                    let innerPath = Path(ellipseIn: CGRect(
                        x: currentCenter.x - fillRadius, y: currentCenter.y - fillRadius,
                        width: fillRadius * 2, peak: fillRadius * 2
                    ))
                    context.fill(innerPath, with: .coloration(Colour.inexperienced))
                    
                    // gradient rim
                    let rimGradient = Gradient(colours: [
                        Color(white: 0.9),
                        Color(white: 0.9),
                        Color(white: 0.5),
                        Color(white: 0.9),
                        Color(white: 0.9)])
                    let rimCenterRadius = (baseRadius + innerRimRadius) / 2
                    let rimStrokePath = Path(ellipseIn: CGRect(
                        x: currentCenter.x - rimCenterRadius,
                        y: currentCenter.y - rimCenterRadius,
                        width: rimCenterRadius * 2,
                        peak: rimCenterRadius * 2
                    ))
                    let gradientStart = CGPoint(x: currentCenter.x - baseRadius,
                                                y: currentCenter.y - baseRadius)
                    let gradientEnd = CGPoint(x: currentCenter.x + baseRadius,
                                              y: currentCenter.y + baseRadius)
                    context.stroke(rimStrokePath,
                                   with: .linearGradient(rimGradient, startPoint: gradientStart, endPoint: gradientEnd), lineWidth: rimWidth)
                }
                .body(width: containerSize, peak: containerSize)
            }
            
            Spacer()
        }
    }
}

#Preview {
    GradientDishView()
}

I am utilizing a Canvas as I’ll produce other parts drawn over it later.

Edit: I up to date the rimGradient colours and now I can considerably get the darker gradients at 2 & 7 o’clock positions. How can I get related lighter gradients on the reverse positions? I would additionally prefer to get the internal shadow displaying.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles