I’m trying to animate a bell icon with the rotation effect but it only animates with the specified angle one way:
struct ContentView: View {
@State var alarmSet = false
var body: some View {
ZStack {
Color.black
VStack {
Image(systemName: "bell.fill")
.font(.system(size: 250))
.foregroundColor(.white)
.rotationEffect(.degrees(reminderSet ? 10 : 0), anchor: .top)
.animation(.interpolatingSpring(mass: 0.5, stiffness: 170, damping: 2.5, initialVelocity: 0), value: alarmSet)
Button(action: {
alarmSet.toggle()
}, label: {
Text(alarmSet ? "Alarm Set" : "Wake Me")
.font(.largeTitle)
.foregroundStyle(.white)
})
.padding(.top, 20)
}
.background(.black)
}
.ignoresSafeArea()
}
}
With the code above, the bell animates between left side & center, I’d like to get it to (1.) swing with the same angle towards the right as well & (2.) swing this way thrice in total and settle to resting state. The interpolating spring animation causes the bell to swing more than thrice as it comes to its resting state. Any help is appreciated.
2
Answers
You can do something like:
You can use a
keyframeAnimator
(iOS 17+)Also consider using some of the built-in springs.