首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >闪烁一张图片1秒(淡入淡出图片)

闪烁一张图片1秒(淡入淡出图片)
EN

Stack Overflow用户
提问于 2016-02-20 21:34:30
回答 2查看 563关注 0票数 1

我正在尝试制作一个图片显示1秒,然后再次淡出到原始图片。这是我的尝试;

代码语言:javascript
复制
@IBOutlet var button1: UIButton!
var button1flashing = false

override func viewDidLoad() {
    super.viewDidLoad()


if !button1flashing {
            flashbutton1()
        } else {
            stopflashingbutton1()
        }
}


func flashbutton1() {
    button1flashing = true

    UIButton.animateWithDuration(1.0, animations: {

        self.button1.setImage(UIImage(named: "code1"), forState: .Normal)

        }, completion: {Bool in
    })
}


func stopflashingbutton1() {

    UIButton.animateWithDuration(1.0, animations: {

        self.button1.setImage(UIImage(named: "code2"), forState: .Normal)

        }, completion: nil)
}

问题:该按钮在视图加载后立即设置为图像"code1“(没有动画),即使它在故事板中设置为"code2”。此外,它不会淡出到图像"code2“(不执行func stopflashingbutton1)

EN

回答 2

Stack Overflow用户

发布于 2016-02-20 23:23:06

使用此函数(您需要显示和隐藏按钮):

代码语言:javascript
复制
func flashButton(button: UIButton, imageName: String, completion: (finished: Bool) -> Void) {

    UIView.animateWithDuration(1.0, animations: {
        button.alpha = 0
        }, completion: {finished in
            button.setImage(UIImage(named: imageName), forState: .Normal)
            UIButton.animateWithDuration(1.0, animations: {
                button.alpha = 1
                }, completion: {finished in
                    completion(finished: finished)
            })
    })
}

func flashButton(button: UIButton, imageName: String) {

    UIView.animateWithDuration(1.0, animations: {
        button.alpha = 0
        }, completion: {finished in
            button.setImage(UIImage(named: imageName), forState: .Normal)
            UIButton.animateWithDuration(1.0, animations: {
                button.alpha = 1
                })
    })
}

在你的viewDidLoad中

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    flashButton(button1, imageName: "code1") { (finished) -> Void in
            self.flashButton(self.button1, imageName: "code2")
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-03-10 02:52:24

将alpha设置为0.1,然后设置为1.0,两者之间的间隔为秒。这会产生动画效果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35523992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档