将图片闪烁1秒(淡入淡出图片)

时间:2016-02-20 13:34:30

标签: swift animation

我试图将图片显示1秒钟然后再次淡出原始图片。这是我的尝试;

@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"一旦View加载(没有动画),即使它被设置为" code2"在故事板中。此外,它不会淡化回图像" code2" (不会执行func stopflashingbutton1

2 个答案:

答案 0 :(得分:0)

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

<div id="h1" class="header">Hello </div>
<hr>  
<div id="h2" class="header">what about this one </div>

在viewDidLoad中

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
                })
    })
}

答案 1 :(得分:0)

将alpha设置为0.1,然后设置为1.0,其间有一个sec间隙。这给了动画效果。