SwiftUI-如何脉动图像不透明度? [英] SwiftUI - how to pulsate image opacity?

查看:52
本文介绍了SwiftUI-如何脉动图像不透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SwiftUI中有一个图片,希望永久脉动"(每秒来一次).

I have an Image in SwiftUI that I would like to "pulsate" (come & go every second or so) forever.

我尝试了很多事情,但似乎无法达到我想要的效果.我尝试过的一件事是下面的代码(似乎什么都不做!:)).

I tried a number of things but I can't seem to get the effect I want. One of the things I tried is the code below (which seems to do nothing! :) ).

Image(systemName: "dot.radiowaves.left.and.right" )
.foregroundColor(.blue)
.transition(.opacity)
.animation(Animation.easeInOut(duration: 1)
    .repeatForever(autoreverses: true))

有什么想法吗?

推荐答案

这里是一种方法.使用Xcode 11.4/iOS 13.4进行了测试

Here is an approach. Tested with Xcode 11.4 / iOS 13.4

struct DemoImagePulsate: View {
    @State private var value = 1.0
    var body: some View {
        Image(systemName: "dot.radiowaves.left.and.right" )
            .foregroundColor(.blue)
            .opacity(value)
            .animation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true))
        .onAppear { self.value = 0.3 }
    }
}

这篇关于SwiftUI-如何脉动图像不透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆