使用Swift以编程方式闪烁屏幕(在“屏幕截图”上) [英] flashing screen programatically with Swift (on 'screenshot taken')

查看:208
本文介绍了使用Swift以编程方式闪烁屏幕(在“屏幕截图”上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了从这里转换一个Objective C的例子:如何以编程方式刷屏? / $>我写了下面的代码:
$ b $ pre $ func blinkScreen(
var wnd = UIApplication.sharedApplication() .keyWindow;
var v = UIView(frame:CGRectMake(0,0,wnd!.frame.size.width,wnd!.frame.size.height))
wnd!.addSubview(v);
v.backgroundColor = UIColor.whiteColor()
UIView.beginAnimations(nil,context:nil)
UIView.setAnimationDuration(1.0)
v.alpha = 0.0;
UIView.commitAnimations();

$ / code>

但是我不确定在哪里添加 UIView v <删除代码(在动画结束时执行的某个事件,但是如何?)。另外 - 我的转换是否正确?

您接近解决方案。但是你可以使用swift中的completion-blocks更简单:

 如果让wnd = self.view {

var v = UIView(frame:wnd.bounds)
v.backgroundColor = UIColor.redColor()
v.alpha = 1

wnd.addSubview(v )
UIView.animateWithDuration(1,动画:{
v.alpha = 0.0
},完成:{(finished:Bool)in
println(inside)
v.removeFromSuperview()
})
}

,首先我检查是否有一个视图,然后我只是将视图的边界设置为闪存视图。一个重要的步骤是设置背景颜色。否则,你将看不到任何闪光效果。我已经将backgroundColor设置为红色,以便您可以在示例中更轻松地看到它。但是,你当然可以使用任何颜色。然后,乐趣开始于 UIView.animateWithDuration 部分。正如你所看到的,我用块代替了你的 startAnimation 等代码。它是这样读取的:
首先你设置动画持续时间为1秒。之后,通过将alpha设置为0来启动动画。然后,在动画完成后,我从它的超级视图中移除视图。



这就是你需要重现的截图效果。


In an effort to convert an Objective C example from here: How to flash screen programmatically? I wrote the following code:

func blinkScreen(){
    var wnd = UIApplication.sharedApplication().keyWindow;
    var v = UIView(frame: CGRectMake(0, 0, wnd!.frame.size.width, wnd!.frame.size.height))
    wnd!.addSubview(v);
    v.backgroundColor = UIColor.whiteColor()
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1.0)
    v.alpha = 0.0;
    UIView.commitAnimations();
}

But I am not sure where I should add UIView v removal code (on some event that is executed when animation ends ... but how?). Also - is my conversion correct?

解决方案

You were close to the solution. But you can make it even easier with completion-blocks in swift:

if let wnd = self.view{

    var v = UIView(frame: wnd.bounds)
    v.backgroundColor = UIColor.redColor()
    v.alpha = 1

    wnd.addSubview(v)
    UIView.animateWithDuration(1, animations: {
        v.alpha = 0.0
        }, completion: {(finished:Bool) in
            println("inside")
            v.removeFromSuperview()
    })
}

As you see, First I check if there is a view and then I just set the bounds of the view to the flash view. One important step is to set a background color. Otherwise you won't see any flash effect. I've set the backgroundColor to red, so that you can see it easier in the example. But you can of course use any color.

Then, the fun starts with the UIView.animateWithDuration part. As you see, I've replaced your startAnimation etc. code with a block. It reads like that: First you set the animation duration at 1 second. After that you start the animation by setting the alpha to 0. Then, after the animation is done, I remove the view from its superview.

That's all you need to reproduce the screenshot effect.

这篇关于使用Swift以编程方式闪烁屏幕(在“屏幕截图”上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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