小编Cal*_*cki的帖子

AVCaptureMetadataOutput().rectOfInterest 不工作

我正在构建一个UIView具有覆盖框的盒子,最终目标是让 QR 码阅读器仅在 QR 落在盒子内时才触发。我知道我需要将 设置.rectOfInterest()为与黄色框相同,但是在当前实现中(下面的代码),读取器不起作用。

public override init(frame: CGRect) {
    super.init(frame: frame)

    if let captureDevice = AVCaptureDevice.default(for: .video) {
        do {
            let input = try AVCaptureDeviceInput(device: captureDevice)
            session.addInput(input)
        } catch {
            print("Error")
        }

        let scannerRect = CGRect(x: self.center.x - (self.frame.width * 0.667 / 2), y: self.frame.width * 0.667 / 4, width: self.frame.width * 0.667, height: self.frame.width * 0.667)

        let output = AVCaptureMetadataOutput()
        output.rectOfInterest = scannerRect
        session.addOutput(output)

        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        output.metadataObjectTypes = [.qr]

        video = AVCaptureVideoPreviewLayer(session: …
Run Code Online (Sandbox Code Playgroud)

qr-code avcapturedevice cgrect swift

6
推荐指数
1
解决办法
2185
查看次数

在 Swift 中将 IF LET 与 OR 结合起来

有没有一种优雅的方法可以通过 or 运算符组合两个 if let 语句。例如,我需要检查字符串“pass”、“true”或整数 1。下面的函数就是这样做的......

func test(content: Any) -> String {
    if let stringValue = (content as? String)?.lowercased(),
        ["pass", "true"].contains(stringValue) {
        return "You Passed"
    }
    if let numValue = (content as? Int),
        1 == numValue {
        return "YOU PASSED"
    }
    return "You Failed"
}

test(content: "Pass") //"You Passed"
test(content: 1) //"YOU PASSED"
Run Code Online (Sandbox Code Playgroud)

组合这两个 if let 语句来处理传入的数据的最简单方法是什么?

if-statement simplify any swift

5
推荐指数
1
解决办法
6338
查看次数

带有 UIView 的 SwiftUI 按钮

我正在从一个名为 的自定义 UIButton 类制作一个 SwiftUI 按钮UIPillButton。以下是创建 SwiftUI 按钮的代码:

Button(action: {
    print("Button tapped")
}) {
    PillButton()
        .padding(.top)
}
Run Code Online (Sandbox Code Playgroud)

这是我的类,用于创建一个名为 SwiftUI 视图的类,PillButton该视图将我的自定义 UIButton 转换为:

struct PillButton: UIViewRepresentable {
    var ntPillButton = NTPillButton(type: .filled, title: "Start Test")
    
    func makeCoordinator() -> Coordinator { Coordinator(self) }
    
    class Coordinator: NSObject {
        var parent: PillButton
        
        init(_ pillButton: PillButton) {
            self.parent = pillButton
            super.init()
        }
    }

    func makeUIView(context: UIViewRepresentableContext<PillButton>) -> UIView {
        let view = UIView()
        view.addSubview(ntPillButton)
        
        NSLayoutConstraint.activate([
            ntPillButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            ntPillButton.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
        
        return …
Run Code Online (Sandbox Code Playgroud)

button uibutton ios swiftui uiviewrepresentable

5
推荐指数
1
解决办法
5870
查看次数

向多个 FCM 令牌发送通知

我有一个班级(PushNotificationSender)。该类拥有一个名为 的函数,sendPushNotification该函数接受FCM TokenNotification title、 和Notification body。鉴于我知道他们的FCM Token.

我的目标:使用此功能向多个用户发送相同的通知。

我尝试为每个用户调用它,FCM Token并且还尝试更改函数的参数以获取一组令牌,而不仅仅是一个,但还没有任何效果。关于如何完成升级有什么想法吗?

PushNotificationSender:

class PushNotificationSender {

    init() {}

    // MARK: Public Functions

    public func sendPushNotification(to token: String, title: String, body: String, completion: @escaping () -> Void) {
        let urlString = "https://fcm.googleapis.com/fcm/send"
        let url = NSURL(string: urlString)!
        let paramString: [String : Any] = ["to" : token,
                                           "notification" : ["title" : title, "body" : body],
                                           "data" : ["user" : …
Run Code Online (Sandbox Code Playgroud)

push-notification apple-push-notifications nsjsonserialization swift firebase-cloud-messaging

2
推荐指数
1
解决办法
7544
查看次数