ScreenshotSharer
ScreenshotSharer 是一个小小的 Swift 4.2 pod,允许用户在截图时分享视图的特定部分或整个屏幕。它在 Asos 应用程序中受到很大启发,并且高度可定制。
预览
安装
CocoaPods
pod 'ScreenshotSharer'
手动操作
只需下载或克隆仓库,然后将 Classes 文件夹移动到您的项目中。
使用方法
您可以使用 ScreeshotSharer 实例来注册要捕获的视图或窗口。
捕获视图
import UIKit
import ScreenshotSharer
class ViewController: UIViewController {
let sssharer = ScreenshotSharer()
@IBOutlet weak var captureView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
sssharer.registerViewCapturer(view: self.captureView, cropRect: CGRect.zero, captureBlock: { (image, screenshotSharerViewController) in
//this block is called when the user takes a screenshot
//image is the image of given view and it can be cropped according to cropRect.
//sharerViewController is the presented view controller
}) { (isSuccess) in
//this block is called when sharerViewController is dismissed
//isSuccess indicates if sharing is completed successfully.
}
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
sssharer.isEnabled = true
}
override func viewWillDisappear(_ animated: Bool)
{
super.viewWillAppear(animated)
sssharer.isEnabled = false
}
}
捕获全屏
sssharer.registerScreenCapturer(cropStatusBar: true, cropRect: CGRect.zero, captureBlock: { (image, screenshotSharerViewController) in
//this block is called when the user takes a screenshot
//image is the image of given view and it can be cropped according to cropRect.
//sharerViewController is the presented view controller
}) { (isSuccess) in
//this block is called when sharerViewController is dismissed
//isSuccess indicates if sharing is completed successfully.
}
自定义默认共享视图控制器
默认情况下,ScreenshotSharer 使用 ScreenshotSharerMinimal 作为显示的共享视图控制器。您可以在 captureBlock 中自定义它。
sssharer.registerScreenCapturer(cropStatusBar: true, cropRect: CGRect.zero, captureBlock: { (image, screenshotSharerViewController) in
if let sharerViewController = sharerViewController
{
sharerViewController.view.backgroundColor = UIColor.red
sharerViewController.setShareTitleText(_ text: String)
sharerViewController.setShareTitleFont(_ font: UIFont)
sharerViewController.setShareTitleColor(_ color: UIColor)
}
}) { (isSuccess) in
}
以下是您可以用于自定义默认共享视图控制器的所有方法。
func setShareTitleText(_ text:String)
func setShareDescriptionText(_ text:String)
func setShareButtonTitleText(_ text:String)
func setShareTitleFont(_ font:UIFont)
func setShareDescriptionFont(_ font:UIFont)
func setShareButtonTitleFont(_ font:UIFont)
func setShareTitleTextColor(_ color:UIColor)
func setShareDescriptionTextColor(_ color:UIColor)
func setShareButtonTitleColor(_ color:UIColor)
func setShareButtonBackgroundColor(_ color:UIColor)
设计您自己的共享视图控制器
在某些情况下,您可能想要从头开始设计整个共享视图控制器。为此,您的共享视图控制器应扩展ScreenshotSharerViewController类,并将其注册到ScreenshotSharer实例中。默认共享视图控制器使用UIActivityViewController,但您可以实现自己的共享逻辑。
let customSharer = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CustomSharerViewController") as! CustomSharerViewController
sssharer.registerScreenCapturer(cropStatusBar: true, cropRect: CGRect.zero, sharerViewController: customSharer, captureBlock: { (image, customScreenshotSharerViewController) in
}) { (isSuccess) in
}
您可以使用ScreenshotSharerMinimal.swift和ScreenshotSharerMinimal.xib文件作为您的基础设计。ScreenshotSharer将展示您的视图控制器,并调用此方法
func setScreenshotImage(_ image:UIImage)
因此,您应该实现setScreenshotImage(_ image:UIImage)
方法。当您想要关闭共享视图控制器时,您应该在您的共享视图控制器中调用此方法
self.screenshotSharer()?.dismissSharerViewController(isSuccess)
isSuccess
表示共享已成功完成。
许可证
MIT
自由软件,太棒了!