Swiftui 迅捷按钮的光环动画
如何制作以下动画?触摸按钮时会出现光环,整个盒子会因为手指的重量而收缩Swiftui 迅捷按钮的光环动画,swiftui,swiftui-animation,swiftui-button,Swiftui,Swiftui Animation,Swiftui Button,如何制作以下动画?触摸按钮时会出现光环,整个盒子会因为手指的重量而收缩 下面是一个使用ZStack和scaleEffect的解决方案。按下时收缩图标,并在完成时还原。与圆形相反,按下时缩放,完成后恢复 太好了!非常感谢。我打算把它们缩在一起(灵感来自苹果音乐上的“播放按钮”),所以我做了一个小小的改变-.frame(宽度:!点击?全尺寸:0.9*全尺寸,高度:!点击?全尺寸:0.9*全尺寸,对齐:对齐(水平:。中心,垂直:。中心))非常欢迎您,很高兴我能帮忙! struct Content
下面是一个使用ZStack和scaleEffect的解决方案。按下时收缩图标,并在完成时还原。与圆形相反,按下时缩放,完成后恢复
太好了!非常感谢。我打算把它们缩在一起(灵感来自苹果音乐上的“播放按钮”),所以我做了一个小小的改变-
.frame(宽度:!点击?全尺寸:0.9*全尺寸,高度:!点击?全尺寸:0.9*全尺寸,对齐:对齐(水平:。中心,垂直:。中心))
非常欢迎您,很高兴我能帮忙!
struct ContentView: View {
var body: some View {
Button(action: {
}, label: {
Image(systemName: "camera")
.font(.title)
.opacity(1)
})
.padding(.horizontal, 20)
.frame(height: 80)
.background(Color.yellow.opacity(0.2).clipShape(Circle()))
}
}
struct AuraButtonView: View {
// automatically resets gesture when complete
@GestureState private var tapped = false
// max size
@State var fullSize : CGFloat = 72
// size when button pressed
@State var scale : CGFloat = 0.8
// camera icon, shrink when pressed, restore on complete
var icon : some View {
Image(systemName: "camera")
.font(.title)
.scaleEffect(tapped ? CGSize(width: scale, height: scale) : CGSize(width: 1, height: 1))
.animation(.easeInOut)
}
// yellow circle, grow when pressed, restore on complete
var background : some View {
Circle()
.foregroundColor(tapped ? Color.yellow.opacity(0.2) : Color.clear)
.frame(width: tapped ? fullSize : 0, height: tapped ? fullSize : 0, alignment: Alignment(horizontal: .center, vertical: .center))
.animation(.easeInOut)
}
// stack the icon on the circle and use the zstack as the gesture
var body: some View {
ZStack(alignment: Alignment(horizontal: .center, vertical: .center), content: {
background
icon
})
.frame(width: fullSize, height: fullSize)
.gesture(DragGesture(minimumDistance: 0)
.updating($tapped) { (_, tapped, _) in
tapped = true
})
}
}
struct AuraButtonView_Previews: PreviewProvider {
static var previews: some View {
AuraButtonView()
}
}