SwiftUI - 如何部分 mask/clip 图像?
SwiftUI - How to partially mask/clip an image?
我希望我的图像对象在移动(通过动画)跨越边界时被部分遮盖。作为参考,我想做这样的事情:
位置 1
位置 2
位置 3
将图像隐藏在另一幅图像后面(并创建剪裁错觉)将不起作用,因为我的背景可能是动态的。任何意见是极大的赞赏。谢谢!
create a clipped illusion) will not work
查看 .clipped()
修饰符。是真的,不是幻觉。
struct ContentView: View {
@State var offset = CGFloat(0)
var body: some View {
Group {
Image(systemName: "circle.fill")
.foregroundColor(.red)
.offset(x: offset, y: 0)
}
.frame(width: 80, height: 80)
.border(Color.green) /// just for clarity, you can remove this
.clipped() /// use this to prevent circle from going past borders
.onAppear {
withAnimation(.linear(duration: 5)) {
offset = 100
}
}
}
}
结果:
我希望我的图像对象在移动(通过动画)跨越边界时被部分遮盖。作为参考,我想做这样的事情:
位置 1 | 位置 2 | 位置 3 |
---|---|---|
将图像隐藏在另一幅图像后面(并创建剪裁错觉)将不起作用,因为我的背景可能是动态的。任何意见是极大的赞赏。谢谢!
create a clipped illusion) will not work
查看 .clipped()
修饰符。是真的,不是幻觉。
struct ContentView: View {
@State var offset = CGFloat(0)
var body: some View {
Group {
Image(systemName: "circle.fill")
.foregroundColor(.red)
.offset(x: offset, y: 0)
}
.frame(width: 80, height: 80)
.border(Color.green) /// just for clarity, you can remove this
.clipped() /// use this to prevent circle from going past borders
.onAppear {
withAnimation(.linear(duration: 5)) {
offset = 100
}
}
}
}
结果: