【问题标题】:flashing screen programatically with Swift (on 'screenshot taken')使用 Swift 以编程方式闪烁屏幕(在“截屏”上)
【发布时间】:2015-02-24 06:44:51
【问题描述】:

为了从这里转换一个 Objective C 示例:How to flash screen programmatically? 我编写了以下代码:

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

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

【问题讨论】:

    标签: ios flash swift screenshot


    【解决方案1】:

    您已接近解决方案。但是您可以使用 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()
        })
    }
    

    如您所见,首先我检查是否有视图,然后将视图的边界设置为 Flash 视图。一个重要的步骤是设置背景颜色。否则您将看不到任何闪光效果。我已将 backgroundColor 设置为红色,以便您在示例中更容易看到它。但是你当然可以使用任何颜色。

    然后,乐趣从UIView.animateWithDuration 部分开始。如您所见,我已经用块替换了您的startAnimation 等代码。它是这样写的: 首先,您将动画持续时间设置为 1 秒。之后,您通过将 alpha 设置为 0 来启动动画。然后,在动画完成后,我将视图从其父视图中移除。

    这就是重现截图效果所需的全部内容。

    【讨论】:

    • 手机的亮度如何?这个闪屏会受它影响,有没有办法让hr屏闪白,闪的时候自动把屏幕亮度调到最全,然后回到自动亮灯模式?
    【解决方案2】:

    UIView 提供类方法来设置动画委托,并提供动画何时开始和结束的选择器。

    使用方法:

    setAnimationDelegate(delegate:)
    setAnimationWillStartSelector(selector:)
    setAnimationDidStopSelector(selector:)
    

    或者,查看允许您提供将在完成时调用的闭包的 UIView 动画方法:

    animateWithDuration(duration: delay: options: animations: completion:)
    

    在您为 didStopSelector 提供的函数中,您可以删除 UIView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多