iOS Swift 相机模式-如何表示已拍摄照片
iOS Swift camera mode- how to indicate photo is captured
在相机模式 (AVCaptureVideoPreviewLayer) 下,我成功地拍摄了一张照片。我想向用户表明这一事实 - 意思是向他展示突然的黑色闪光和咔哒声 - 类似于他自己拍照时的体验。
我该怎么做?是否有一些内置功能可以做到这一点?
谢谢
没有内置功能可以执行此操作,但您可以通过在相机视图层次结构中添加一个 alpha 设置为零的黑色 UIView,然后播放系统声音并为 "flash" 拍摄照片时视图的 alpha。
在 viewDidLoad
、loadView
或您 assemble 视图层次结构中的任何地方
// Assuming cameraView contains your previewLayer...
flashView = UIView(frame: <set your frame>)
flashView.alpha = 0
flashView.backgroundColor = UIColor.blackColor()
cameraView.addSubview(flashView)
然后,在您的捕获完成块中
// Animate the "flash"
UIView.animateWithDuration(0.1, delay: 0, options: .Autoreverse, animations: { () -> Void in
flashView.alpha = 1
}, completion: nil)
// Play the camera shutter system sound
AudioServicesPlayAlertSound(1108)
有关系统声音的更多信息,请参阅此问题:Playing system sound without importing your own。
对于Swift 4:
@IBOutlet weak var lightView: UIView! //First set lightView hidden in the storyboard
//MARK: - Take Screenshot Animation
func flashAnimation(image: UIImage, rect: CGRect) {
self.view.bringSubview(toFront: lightView)
lightView.alpha = 0
lightView.isHidden = false
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseOut], animations: {() -> Void in
self.lightView.alpha = 1.0
}, completion: {(finished: Bool) -> Void in
self.hideFlashView()
})
}
func hideFlashView() {
UIView.animate(withDuration: 0.1, delay: 0.0, animations: {() -> Void in
self.lightView.alpha = 0.0
})
}
在相机模式 (AVCaptureVideoPreviewLayer) 下,我成功地拍摄了一张照片。我想向用户表明这一事实 - 意思是向他展示突然的黑色闪光和咔哒声 - 类似于他自己拍照时的体验。
我该怎么做?是否有一些内置功能可以做到这一点?
谢谢
没有内置功能可以执行此操作,但您可以通过在相机视图层次结构中添加一个 alpha 设置为零的黑色 UIView,然后播放系统声音并为 "flash" 拍摄照片时视图的 alpha。
在 viewDidLoad
、loadView
或您 assemble 视图层次结构中的任何地方
// Assuming cameraView contains your previewLayer...
flashView = UIView(frame: <set your frame>)
flashView.alpha = 0
flashView.backgroundColor = UIColor.blackColor()
cameraView.addSubview(flashView)
然后,在您的捕获完成块中
// Animate the "flash"
UIView.animateWithDuration(0.1, delay: 0, options: .Autoreverse, animations: { () -> Void in
flashView.alpha = 1
}, completion: nil)
// Play the camera shutter system sound
AudioServicesPlayAlertSound(1108)
有关系统声音的更多信息,请参阅此问题:Playing system sound without importing your own。
对于Swift 4:
@IBOutlet weak var lightView: UIView! //First set lightView hidden in the storyboard
//MARK: - Take Screenshot Animation
func flashAnimation(image: UIImage, rect: CGRect) {
self.view.bringSubview(toFront: lightView)
lightView.alpha = 0
lightView.isHidden = false
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseOut], animations: {() -> Void in
self.lightView.alpha = 1.0
}, completion: {(finished: Bool) -> Void in
self.hideFlashView()
})
}
func hideFlashView() {
UIView.animate(withDuration: 0.1, delay: 0.0, animations: {() -> Void in
self.lightView.alpha = 0.0
})
}