Pio*_*otr 3 flash screenshot ios swift
为了从这里转换Objective C示例:如何以编程方式闪屏?我写了以下代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
但我不确定在哪里应该添加UIView v删除代码(在动画结束时执行的某些事件......但是如何?).另外 - 我的转换是否正确?
Chr*_*örz 10
你接近解决方案.但是你可以使用swift中的completion-blocks让它更容易:
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()
})
}
Run Code Online (Sandbox Code Playgroud)
如您所见,首先我检查是否有视图然后我只是将视图的边界设置为flash视图.一个重要的步骤是设置背景颜色.否则你将看不到任何闪光效果.我已将backgroundColor设置为红色,以便您在示例中更容易看到它.但你当然可以使用任何颜色.
然后,乐趣从UIView.animateWithDuration
部分开始.如你所见,我startAnimation
用一个块替换了你的代码.它如下所示:首先,您将动画持续时间设置为1秒.之后,通过将alpha设置为0来启动动画.然后,在动画完成后,我从其超视图中删除视图.
这就是重现屏幕截图效果所需的全部内容.
UIView 提供类方法来设置动画委托,并提供动画何时开始和完成的选择器。
使用方法:
setAnimationDelegate(delegate:)
setAnimationWillStartSelector(selector:)
setAnimationDidStopSelector(selector:)
Run Code Online (Sandbox Code Playgroud)
或者,查看 UIView 动画方法,这些方法允许您提供将在完成时调用的闭包:
animateWithDuration(duration: delay: options: animations: completion:)
Run Code Online (Sandbox Code Playgroud)
在您为 didStopSelector 提供的函数中,您可以删除 UIView。