Create a SwiftUI animation that uses the dashPhase of a rectangle to create marching ants effect. Repeat the animation forever
// MARK: Create a SwiftUI animation that uses the dashPhase of a rectangle to create marching ants effect. Repeat the animation forever//importSwiftUIstructMarchingAntsRectangle: View {
@Stateprivatevar dashPhase =100.0var body: some View {
Rectangle()
.stroke(style: StrokeStyle(lineWidth: 6, dash: [10, 5], dashPhase: dashPhase))
.animation(.linear(duration: 1).repeatForever(autoreverses: false), value: dashPhase)
.frame(width: 300, height: 200)
.onAppear {
dashPhase =10.0
}
}
}
ChatGPT Prompt
Preview
Create a SwiftUI animation that makes the tip of the SF Symbol pencil moves around a stroked circle forever
// MARK: Create a SwiftUI animation that makes the tip of the SF Symbol pencil moves around a stroked circle foreverimportSwiftUIstructPencilCircleView: View {
@Stateprivatevar angle: Angle = .degrees(0)
var body: some View {
ZStack {
Circle()
.stroke(Color.red, lineWidth: 2)
.frame(width: 200, height: 200)
Image(systemName: "pencil.tip")
.font(.system(size: 64))
.offset(x: 100)
.rotationEffect(angle)
.animation(.linear(duration: 2).repeatForever(autoreverses: false), value: angle)
.onAppear {
self.angle= .degrees(360)
}
}
}
}
1. Create a SwiftUI app animation that is triggered by tapping the SF Symbol button hand.thumbsup.fill. Use a ZStack for the animated views which consist of a splash at the bottom, a green circle in the middle, and a number on the top. When the button is tapped, the opacity of the splash and the green circle change from 0 to 1 with animation, and the number increments by 1 with animation. Use 12 small circles of different colors for the splash and rotate them around a central point by placing their offset above rotation. 2. Increment the number anytime the button is tapped. Change the opacity of the green circle and the splash back to 0 with animation after they animate to an opacity of 1, anytime the button is tapped
importSwiftUIstructMediumClap3: View {
@Stateprivatevar showAnimation =false@Stateprivatevar count =0var body: some View {
VStack(spacing: 64) {
ZStack {
// Splash viewForEach(0..<12, id: \.self) { i inCircle()
.fill(getCircleColor(i))
.frame(width: 12, height: 12)
.opacity(showAnimation ?1:0)
.animation(.easeInOut(duration: 1.5), value: showAnimation)
.offset(x: 0, y: -130)
.rotationEffect(.degrees(Double(i *30)))
}
// Green circleCircle()
.fill(.green)
.frame(width: 200, height: 200)
//.scaleEffect(showAnimation ? 1.3 : 1)
.opacity(showAnimation ?1:0)
.animation(.easeInOut(duration: 1.5), value: showAnimation)
// Number labelText("\(count)")
.font(.largeTitle)
.animation(.easeInOut(duration: 0.5), value: count)
}
// ButtonButton(action: {
showAnimation =true
count +=1// Change the opacity of the green circle and the splash back to 0 with animationwithAnimation(.default.delay(1)) {
showAnimation =false
}
}) {
Image(systemName: "hand.thumbsup.fill")
.font(.largeTitle)
}
}
}
// Helper function to get the color for each circle in the splashfuncgetCircleColor(_index: Int) -> Color {
switch index %4 {
case0:return .redcase1:return .yellowcase2:return .bluecase3:return .purpledefault:return .black
}
}
}
ChatGPT Prompt
Preview
Soon...
importSwiftUIstructTwitterLikeAnimation: View {
@Stateprivatevar isLiked =false@Stateprivatevar isSplashing =falsevar body: some View {
ZStack {
if isLiked {
// Pink-filled SF symbol heart iconImage(systemName: "heart.fill")
.font(.largeTitle)
.foregroundColor(.pink)
.animation(.spring(), value: isLiked)
// Circle with stroke border of 0Circle()
.stroke(lineWidth: 0)
.frame(width: 28, height: 28)
.animation(.easeOut(duration: 0.2), value: isLiked)
if isSplashing {
ForEach(0..<8, id: \.self) { index inCircle()
.foregroundColor(self.color(for: index))
.frame(width: 6, height: 6)
.offset(x: 25)
.rotationEffect(.degrees(45*Double(index)))
}
}
} else {
// Stroked SF symbol heart iconImage(systemName: "heart")
.font(.largeTitle)
.foregroundColor(.pink)
}
} // All views
.scaleEffect(2)
.onTapGesture {
withAnimation(.spring()) {
isLiked.toggle()
}
DispatchQueue.main.asyncAfter(deadline: .now() +0.2) {
withAnimation(Animation.easeOut(duration: 0.5)) {
isSplashing =true
}
}
DispatchQueue.main.asyncAfter(deadline: .now() +0.4) {
withAnimation(Animation.easeOut(duration: 0.5)) {
isSplashing =false
}
}
}
}
funccolor(forindex: Int) -> Color {
let colors: [Color] = [.pink, .red, .orange, .yellow, .green, .blue, .purple, .pink]
return colors[index % colors.count]
}
}