Skip to main content
ubuntuask.com

Back to all posts

How to Make Blink Effect In Swiftui?

Published on
4 min read
How to Make Blink Effect In Swiftui? image

Best SwiftUI Animation Guides to Buy in October 2025

1 SwiftUI Cookbook: A guide for building beautiful and interactive SwiftUI apps

SwiftUI Cookbook: A guide for building beautiful and interactive SwiftUI apps

BUY & SAVE
$27.34 $44.99
Save 39%
SwiftUI Cookbook: A guide for building beautiful and interactive SwiftUI apps
2 An iOS Developer's Guide to SwiftUI: Design and build beautiful apps quickly and easily with minimum code

An iOS Developer's Guide to SwiftUI: Design and build beautiful apps quickly and easily with minimum code

BUY & SAVE
$31.80 $44.99
Save 29%
An iOS Developer's Guide to SwiftUI: Design and build beautiful apps quickly and easily with minimum code
3 SwiftUi and Xcode Bible: The Ultimate Guide to Building iOS Apps (Complete Guide to App & Software Development for Beginners)

SwiftUi and Xcode Bible: The Ultimate Guide to Building iOS Apps (Complete Guide to App & Software Development for Beginners)

BUY & SAVE
$25.99
SwiftUi and Xcode Bible: The Ultimate Guide to Building iOS Apps (Complete Guide to App & Software Development for Beginners)
4 SwiftUI Cookbook: A guide to solving the most common problems and learning best practices while building SwiftUI apps, 2nd Edition

SwiftUI Cookbook: A guide to solving the most common problems and learning best practices while building SwiftUI apps, 2nd Edition

BUY & SAVE
$19.99 $41.99
Save 52%
SwiftUI Cookbook: A guide to solving the most common problems and learning best practices while building SwiftUI apps, 2nd Edition
5 Ultimate SwiftUI Handbook for iOS Developers: A complete guide to native app development for iOS, macOS, watchOS, tvOS, and visionOS (English Edition)

Ultimate SwiftUI Handbook for iOS Developers: A complete guide to native app development for iOS, macOS, watchOS, tvOS, and visionOS (English Edition)

BUY & SAVE
$29.95 $37.95
Save 21%
Ultimate SwiftUI Handbook for iOS Developers: A complete guide to native app development for iOS, macOS, watchOS, tvOS, and visionOS (English Edition)
6 iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success

iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success

BUY & SAVE
$26.70 $44.99
Save 41%
iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
+
ONE MORE?

To create a blink effect in SwiftUI, you can use the onReceive modifier along with a Timer.TimerPublisher to toggle a boolean variable that will control the visibility of your view. First, create a @State variable to toggle the visibility of the view. Then, use the onReceive modifier to listen for a Timer.TimerPublisher that will toggle the boolean variable on and off at a specified interval. Finally, use an if statement in your view to conditionally display your view based on the boolean variable. By continuously toggling the boolean variable on and off at a certain interval, you can achieve a blinking effect in SwiftUI.

To test the accessibility of a blink effect in SwiftUI applications, you can follow these steps:

  1. Turn on VoiceOver on your device to simulate how a visually impaired person would interact with your app.
  2. Navigate to the screen or view where the blink effect is present.
  3. Listen to the VoiceOver output and pay attention to any announcements related to the blink effect. VoiceOver should announce that there is a blink effect present on the screen.
  4. Use VoiceOver gestures to interact with the blinking elements and ensure they are accessible and clearly identifiable.
  5. Check the contrast ratio between the blinking element and its background to ensure it meets accessibility guidelines.
  6. Test the blink effect with different screen readers and assistive technologies to ensure compatibility and accessibility.
  7. Consider providing an option in the app settings to disable the blink effect or adjust its speed for users who may find it distracting or uncomfortable.
  8. Gather feedback from users with disabilities or accessibility experts to get insights on how the blink effect can be improved for better accessibility.

The default duration for a blink animation in SwiftUI is 0.4 seconds.

What is the most intuitive way to indicate a blinking element to the user in SwiftUI?

One intuitive way to indicate a blinking element to the user in SwiftUI is to use the .[opacity](https://freelanceshack.com/blog/how-to-change-the-opacity-of-an-iframe-element-with) modifier in combination with a Timer to toggle the opacity of the element on and off at regular intervals. Here is an example code snippet to achieve this effect:

struct BlinkingElement: View { @State private var isBlinking = false

var body: some View {
    Text("Blinking Text")
        .opacity(isBlinking ? 0 : 1)
        .onAppear {
            Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { \_ in
                withAnimation {
                    self.isBlinking.toggle()
                }
            }
        }
}

}

In this code snippet, we create a Text view with the text "Blinking Text" and use the .opacity modifier to toggle the opacity of the text between 0 (invisible) and 1 (visible) based on the isBlinking state. We use a Timer to toggle the isBlinking state at a 0.5 second interval, creating a blinking effect for the text. The withAnimation block is used to animate the opacity change for a smoother transition.

To implement a blink effect on a text view in SwiftUI, you can use a combination of the opacity property and the Animation modifier. Here's an example of how you can achieve this:

import SwiftUI

struct BlinkingText: View { @State private var isBlinking = false

var body: some View {
    Text("Hello, World!")
        .opacity(isBlinking ? 0 : 1)
        .animation(Animation.easeInOut(duration: 0.5).repeatForever())
        .onAppear {
            self.isBlinking = true
        }
}

}

struct ContentView: View { var body: some View { VStack { BlinkingText() } } }

struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }

In this example, we create a BlinkingText view that initially sets the isBlinking variable to false. The Text view has its opacity set to 1 when isBlinking is false, and 0 when isBlinking is true. We then apply an easeInOut animation with a duration of 0.5 seconds that repeats forever. When the view appears, we set isBlinking to true, which starts the blinking effect.

You can customize the duration and other properties of the Animation according to your preferences.