I want to apply ContainerRelativeShape only to specific corners [SwiftUI]

This article wants to use the automatic rounded ContainerRelativeShape that appeared in iOS 14 only for specific corners.

What is ContainerRelativeShape?

The number of UI with rounded corners unique to each part is increasing, such as the iPhone 11 terminal itself and the widget that appeared in iOS14. The ContainerRelativeShape displays the appropriate rounded radius at each location.

Purpose

Place it in the upper left Apply ContainerRelativeShape Expand and round the upper left corner!

--I want to round only specific corners --And I want to use the automatically calculated ContainerRelativeShape instead of just rounded corners.

I will explain the implementation of the image on the far right that realized this.

code

Custom Shape

Using the Shape Protocol There is a good ʻUIRectCornerOptionSet that handles corner information, so use it Define your ownContainerRelativeShapeSpecificCorner` Struct.

struct ContainerRelativeShapeSpecificCorner: Shape {
    
    private let corners: [UIRectCorner]
    
    init(corner: UIRectCorner...) {
        self.corners = corner
    }
    
    func path(in rect: CGRect) -> Path {
        var p = ContainerRelativeShape().path(in: rect)
        
        if corners.contains(.allCorners) {
            return p
        }
        
        if !corners.contains(.topLeft) {
            p.addPath(Rectangle().path(in: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.width / 2, height: rect.height / 2)))
        }
        if !corners.contains(.topRight) {
            p.addPath(Rectangle().path(in: CGRect(x: rect.origin.x + rect.width / 2, y: rect.origin.y, width: rect.width / 2, height: rect.height / 2)))
        }
        if !corners.contains(.bottomLeft) {
            p.addPath(Rectangle().path(in: CGRect(x: rect.origin.x, y: rect.origin.y + rect.height / 2, width: rect.width / 2, height: rect.height / 2)))
        }
        if !corners.contains(.bottomRight) {
            p.addPath(Rectangle().path(in: CGRect(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height / 2, width: rect.width / 2, height: rect.height / 2)))
        }
        return p
    }
}

Usage example

//Originally
Image("camera")
    .clipShape(ContainerRelativeShape())

//This guy(Rounded corners only for specific corners)
Image("camera")
    .clipShape(ContainerRelativeShapeSpecificCorner(corner: .topLeft, .topRight))

//All code samples
struct SampleView: View {
    var body: some View {
        Group {
            Image("camera")
                .resizable()
                .scaledToFill()
                .frame(width: 80, height: 80)
                .clipShape(ContainerRelativeShapeSpecificCorner(corner: .topLeft))
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
        .padding(8)
    }
}

Recommended Posts

I want to apply ContainerRelativeShape only to specific corners [SwiftUI]
I want to monitor a specific file with WatchService
I want to convert characters ...
I want to make a specific model of ActiveRecord ReadOnly
Swift: I want to chain arrays
I want to recursively search for files under a specific directory
I want to give edit and delete permissions only to the poster
I want to use FormObject well
Only this I want to remember, 4 designs for writing unit tests
I want to convert InputStream to String
I want to docker-compose up Next.js!
[Ruby] I want to output only the odd-numbered characters in the character string
When you want Rails to disable a session for a specific controller only
[Ruby] I want to extract only the value of the hash and only the key
I want to write a nice build.gradle
I want to eliminate duplicate error messages
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
Development memo ~ I want to display only the first image posted multiple times ~
I want to get only the time from Time type data ...! [Strftime] * Additional notes
I always want to show what I posted only to myself who is logged in
Add class only to specific elements with V-for
I want to test Action Cable with RSpec test
I want to sort by tab delimited by ruby
I want to output the day of the week
Run R from Java I want to run rJava
[Swift] I want to draw grid lines (squares)
I want to send an email in Java.
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to reduce simple mistakes. To command yourself.
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to delete files managed by Git
I want to get the value in Ruby
I want to go back to a specific VC by tapping the back button on the NavigationBar!
I want to see only the latest because the Docker log has become too large