22.2 C
Canberra
Monday, February 24, 2025

ios – Bizarre challenge whereas exhibiting a picture in rounded rectangle


In my app, I would like to indicate pictures in rounded rectangle form on a horizontal scroll view and after I faucet on the picture, the picture opens in full display. I’ve a number of pictures, however for a similar of retaining it easy, I’ve 2 pictures in my pattern code beneath.

The second picture (Picture B) may be very vast. To clarify this query in a straightforward manner, I’ve chosen first picture (Picture A) with 2 shades (yellow and pink). In the event you faucet on pink coloration of Picture A, the app behaves as if Picture B was tapped and opens Picture B as a substitute of opening Picture A. Tapping on yellow coloration of Picture A, opens Picture A accurately.

That is taking place as a result of Picture B is vast and I’m utilizing picture.resizeable().aspectRatio(**.fill**) to show pictures in a rounded rectangle form. If I exploit picture.resizeable().aspectRatio(**.match**), then faucet habits works effective i.e. if pink coloration of Picture A is tapped, then app opens Picture A itself, nonetheless with aspectRatio(.match), the photographs do not get displayed as rounded rectangle.

Executable pattern code:

import SwiftUI

struct Foo {
    var title: String
    var url: String
    var picture: Picture?
    
    init(title: String, url: String, picture: Picture? = nil) {
        self.title = title
        self.url = url
        self.picture = picture
    }
}

struct ContentViewA: View {
    @State non-public var information = [
        Foo(title: "Image A", url: "https://www.shutterstock.com/image-illustration/two-shades-color-background-mix-260nw-2340299851.jpg", image: nil),
        Foo(title: "Image B", url: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Sydney_Harbour_Bridge_night.jpg/800px-Sydney_Harbour_Bridge_night.jpg", image: nil)

        // Foo(title: "Image B", url: "https://www.shutterstock.com/image-photo/ultra-wide-photo-mountains-river-260nw-1755037052.jpg", image: nil)
        /// There are more images in the array in real code.
    ]
    
    var physique: some View {
        ZStack {
            Colour.black.opacity(0.7).ignoresSafeArea()
            VStack {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(alignment: .heart, spacing: 10) {
                        ForEach(Array(information.enumerated()), id: .offset) { index, merchandise in
                            if let urlObject = URL(string: merchandise.url) {
                                AsyncImage(url: urlObject,
                                           scale: 1.0,
                                           transaction: Transaction(animation: .spring(response: 0.5, dampingFraction: 0.65, blendDuration: 0.025)),
                                           content material: { renderPhoto(part: $0, merchandise: merchandise, index: index) })
                            } else {
                                /// Notice: Reveals placeholder view
                                EmptyView()
                            }
                        }
                    }
                    .padding(.main, 0)
                    .padding(.trailing, 16)
                    .body(maxWidth: .infinity, minHeight: 65, maxHeight: 65, alignment: .topLeading)
                }
            }
            .padding([.top, .bottom], 150.0)
            .padding([.leading, .trailing], 50.0)
        }
    }
    
    @ViewBuilder
    non-public func renderPhoto(part: AsyncImagePhase, merchandise: Foo, index: Int) -> some View {
        swap part {
            case .success(let picture):
                thumbnailView(picture: picture, merchandise: merchandise, index: index)
            case .failure(let error):
                thumbnailView(merchandise: merchandise, index: index, isFailure: true)
            case .empty:
                thumbnailView(merchandise: merchandise, index: index, isFailure: true)
            @unknown default:
                EmptyView()
        }
    }
    
    non-public func thumbnailView(picture: Picture? = nil, merchandise: Foo, index: Int, isFailure: Bool = false) -> some View {
        VStack {
            Rectangle()
            .foregroundColor(.clear)
            .body(width: 72, top: 55)
            .background(
                VStack {
                    if let picture = picture {
                        picture.resizable()
                            .aspectRatio(contentMode: .fill)
                            // .aspectRatio(contentMode: .match) /// Setting facet ratio to suit avoids the issue, however would not give rounded rectangle look.
                            .body(width: 72, top: 55)
                            .disabled(false)
                            .clipped()
                    } else {
                        /// present error picture
                        EmptyView()
                    }
                }
            )
            .cornerRadius(8)
            .padding([.top, .bottom], 10.0)
            .onTapGesture {
                print("%%%%% Tapped picture title: (merchandise.title) and index is: (index) %%%%%%")
            }
        }
    }
}

Sharing screenshots of the app utilizing pattern code:

Rounded rectangle pictures with aspectRatio(.fill), however tapping on pink coloration of 1st picture, opens 2nd picture, as a result of 2nd picture is vast. That is the look of the photographs I need to have.

enter image description here

Pictures with aspectRatio(.match), tapping on 1st picture, opens 1st picture and tapping on 2nd picture, opens 2nd picture. That is the faucet habits I need to have.

enter image description here

How can I’ve rounded rectangular trying pictures and likewise open appropriate picture when tapped i.e. tapping anyplace on Picture A ought to solely open Picture A?

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