I am trying to make a rectangular button that on tap gesture became a circular button with a scale and smooth animation.
ZStack {
ZStack {
RegularPolygon(sides: started ? 50 : 4)
.scale(started ? 0.75 : 1.0)
.fill(started ? Color(.green) : .red)
}
.frame(width: 350, height: 200)
.padding()
Text(started ? "start" : "Stop")
.font(.system(size: 36, design:.rounded))
.fontWeight(.heavy)
}
.animation(.easeOut(duration: 0.5))
This is my code so fare but I don't like this approach and I think it's overwhelming a simple task.
What is the best approach to transform a rectangular button to a circle one with a smooth scale animation?
CodePudding user response:
Try this code to change rectangular button to round button.
ZStack {
// Rectangle CGFloat(self.isTapped ? 25.0 : 50)
RoundedRectangle(cornerRadius: CGFloat(self.isTapped ? 25.0 : 50))
.foregroundColor(self.isTapped ?.red :.blue)
.frame(width: CGFloat(self.isTapped ? 200 : 100), height: CGFloat(self.isTapped ? 40 : 100), alignment: .center)
Text(self.isTapped ? "Start" : "Stop")
.foregroundColor(Color.white)
}
.animation(.easeOut(duration: 0.5))
.onTapGesture {
withAnimation {
self.isTapped.toggle()
}
}
}