Update text opacity in SwiftUI ScrollView based on scrollTransition

How can I fade in & out the opacity of the Text elements based on the phase? If phase is identity then the text opacity should be 1. When a view at the edge of the scrollview is 1/3rd visible, the text opacity should gradually increase from 0 to 1 as it scrolls into view. Similarly, it should decrease with opacity 1-0 as the view goes out of view. Please note that the RoundedRectangle with overlay Text is just for demonstration, it’ll eventually be replaced by a custom view with several elements including a Text view.

struct ColorScroll: View {
    private let colors = [Color.red, Color.blue, Color.green, Color.yellow, Color.brown, Color.indigo, Color.purple]
    private let titles = ["Color.red", "Color.blue", "Color.green", "Color.yellow", "Color.brown", "Color.indigo", "Color.purple"]

    var body: some View {
            let itemWidth = 150.0
            VStack(spacing: 20) {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 10) {
                        ForEach(colors.indices, id: .self) { index in
                            ZStack {
                                RoundedRectangle(cornerRadius: 25.0)
                                    .fill(colors[index])
                                    .frame(width: itemWidth, height: 200)
                                    .overlay(alignment: .leading) {
                                        Text(titles[index])
                                            .font(.title2)
                                    }
                                    .scrollTransition(.interactive, axis: .horizontal) { content, phase in
                                        content.offset(x: phase.isIdentity ? 0 : phase.value * -itemWidth/2)
                                    }
                            }
                            .clipShape(RoundedRectangle(cornerRadius: 25))
                        }
                    }
                    .scrollTargetLayout()
                    .padding(.horizontal, 20)
                }
                .frame(height: 200)
                Spacer()
        }
    }
}

Any help is appreciated.

2

You can have multiple scrollTransitions! Just put one on the Text:

.overlay(alignment: .leading) {
    Text(titles[index])
        .font(.title2)
        .scrollTransition { content, phase in
            content.opacity(1 - abs(phase.value))
        }
}

Notice the formula 1 - abs(phase.value). You can graph this with respect to phase.value and see that this produces the effect you want. When phase.value is at 1 or -1, the opacity is 0, and when it is 0, the opacity is 1.


The caveat is that the rounded rectangle also has an offset that comes from the other scroll transition. On the trailing edge, this offset, combined with the clipShape, makes the text physically visible much later than what scrollTransition considers to be “visible”.

Consider the case where the text is just about to become physically visible. The text is shifted to the left at this point due to the offset scroll transition. The text’s frame and intersects with the frame of the scroll view (but is not physically visible because of clipShape). As a result, the opacity scroll transition will think the text is visible, and give you a phase value above 0.

Effectively, this means the text will not fade in from 0 opacity. The change in opacity can even be unperceivable if the opacity starts from too high a value. Since the text is extremely close to the leading edge, the latter is exactly what happens.

“red” is fading out, but since the frame of “yellow” is almost entirely within the scroll view’s frame (it might help to imagine what it looks like if it is not clipped by clipShape), it has a high opacity despite being barely visible.

If the text is close to the trailing edge instead, then the same problem would occur on the leading edge.

You can somewhat make it fade in “more” by insetting the scroll transition threshold:

.scrollTransition(.interactive.threshold(.visible.inset(by: itemWidth / 2))) { content, phase in
    content.opacity(1 - abs(phase.value))
}

Note that insetting too much would cause the leading edge to fade out too quickly. That is just the asymmetric nature of the problem here.

To “properly” solve this problem, I think you can’t solely rely on scrollTransition. This is no longer solely about scrolling, but also “how much of the text is clipped by the clip shape”.

Recognized by Mobile Development Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật