2017-02-14 95 views
0

有沒有什麼辦法可以獲得像某個NSView按需拍攝屏幕截圖時產生的閃屏效果?我的問題不是flashing screen programatically with Swift (on 'screenshot taken')的重複,因爲我需要osx的解決方案,而不是ios和方法是不同的。如何獲得屏幕捕捉效果之類的屏幕閃光效果?

+1

的可能的複製[閃屏編程與斯威夫特(上「截圖採取」)](http://stackoverflow.com/questions/28684549/flashing-screen-programatically -with-迅速-ON-截圖採用) – Alladinian

回答

1

實現這一目標的方法是創建一個與屏幕尺寸相同的新UIView,並將其添加到視圖的子視圖中,然後將該視圖的alpha設置爲零(使用持續時間來實現動畫效果預期效果)完成後,從超級視圖中移除視圖。

我在許多項目中都使用過這種技術,它的功能就像一個魅力。您可以使用視圖的背景顏色來自定義閃光燈的外觀。

1

這樣的事情可能會奏效

func showScreenshotEffect() { 

let snapshotView = UIView() 
    snapshotView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(snapshotView) 

    let constraints:[NSLayoutConstraint] = [ 
     snapshotView.topAnchor.constraint(equalTo: view.topAnchor), 
     snapshotView.leadingAnchor.constraint(equalTo: view.leadingAnchor), 
     snapshotView.trailingAnchor.constraint(equalTo: view.trailingAnchor), 
     snapshotView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor) 
    ] 
    NSLayoutConstraint.activate(constraints) 

    snapshotView.backgroundColor = UIColor.white 

    UIView.animate(withDuration: 0.2, animations: { 
     snapshotView.alpha = 0 
    }) { _ in 
     snapshotView.removeFromSuperview() 
    } 
}