如何在SwiftUI tvOS中检测``点击''手势

时间:2019-12-05 17:28:12

标签: swift swiftui tvos

使用:

  • SwiftUI
  • 雨燕5
  • tvOS
  • Xcode版本11.2.1

我只想在下面的URLImage上检测到点击手势

JFYI 我对Xcode,Swift和SwiftUI非常陌生(不到3周)。

URLImage(URL(string: channel.thumbnail)!,
                 delay: 0.25,
                 processors: [ Resize(size: CGSize(width:isFocused ?  300.0 : 225.0, height:isFocused ?  300.0 : 225.0), scale: UIScreen.main.scale) ],
                 content:  {
                    $0.image
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .clipped()
        })
            .frame(width: isFocused ?  300.0 : 250.0, height:isFocused ?  300.0 : 250.0)
            .clipShape(Circle())
            .overlay(
                Circle().stroke( isFocused ? Color.white : Color.black, lineWidth: 8))
            .shadow(radius:5)
            .focusable(true, onFocusChange:{ (isFocused) in
                withAnimation(.easeInOut(duration:0.3)){
                    self.isFocused = isFocused
                }
                if(isFocused){
                    self.manager.bannerChannel = self.channel
                    print(self.manager.bannerChannel)
                    self.manager.loadchannelEPG(id: self.channel.id)
                }
            })
            .padding(20)
    }
  • 我发现的唯一解决方法是将其包装在NavigationLink中 或按钮,但无法集中在按钮上。
  • 我发现,如果我向其添加转角半径,则focusable会在Button / NavigationLink上运行,但是默认的点击操作不会运行
  • TapGesture在tvOS中不可用

既然可以使用手势,也许有一种方法我无法弄清楚。

OR

如果有一种方法可以将焦点放到按钮上(尽管这是最不受欢迎的选择,因为这会改变我想要的外观)。

2 个答案:

答案 0 :(得分:2)

如果您想避免 UIKit,您可以使用 Long Press Gesture 通过设置非常小的按下持续时间来实现所需的解决方案。

1.只有Press

如果你只需要处理按下动作,根本不需要长按。

ContentView()
   .onLongPressGesture(minimumDuration: 0.01, pressing: { _ in }) {
       print("pressed")
   }

2. PressLong press

如果您需要同时处理按下和长按。

var longPress: some Gesture {
    LongPressGesture(minimumDuration: 0.5)
       .onEnded { _ in
          print("longpress")
       }
}

ContentView()
   .highPriorityGesture(longPress)
   .onLongPressGesture(minimumDuration: 0.01, pressing: { _ in }) {
       print("press")
   }

答案 1 :(得分:1)

这是可能的,但并非出于胆小的人。我想出了一个可能对您有所帮助的通用解决方案。我希望在下一个swiftUI更新中,Apple可以为tvOS附加单击事件的更好方法,并且该代码可以移交给它所属的垃圾箱。

有关如何执行此操作的高级解释是,使UIView捕获焦点和click事件,然后使UIViewRepresentable以便swiftUI可以使用该视图。然后将视图添加到ZStack的布局中,以便将其隐藏,但是您可以获取焦点并响应单击事件,就好像用户确实在与真正的swiftUI组件进行交互一样。

首先,我需要制作一个捕获事件的UIView。

    class ClickableHackView: UIView {
        weak var delegate: ClickableHackDelegate?

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

        override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
            if event?.allPresses.map({ $0.type }).contains(.select) ?? false {
                delegate?.clicked()
            } else {
                superview?.pressesEnded(presses, with: event)
            }
        }

        override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
            delegate?.focus(focused: isFocused)
        }

        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        override var canBecomeFocused: Bool {
            return true
        }
    }

可点击的委托人:

protocol ClickableHackDelegate: class {
    func focus(focused: Bool)
    func clicked()
}

然后为我的视图添加swiftui扩展名

struct ClickableHack: UIViewRepresentable {
    @Binding var focused: Bool
    let onClick: () -> Void

    func makeUIView(context: UIViewRepresentableContext<ClickableHack>) -> UIView {
        let clickableView = ClickableHackView()
        clickableView.delegate = context.coordinator
        return clickableView
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<ClickableHack>) {
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    class Coordinator: NSObject, ClickableHackDelegate {
        private let control: ClickableHack

        init(_ control: ClickableHack) {
            self.control = control
            super.init()
        }

        func focus(focused: Bool) {
            control.focused = focused
        }

        func clicked() {
            control.onClick()
        }
    }
}

然后,我制作一个友好的swiftui包装器,这样我就可以传入我想使其可聚焦和可单击的任何类型的组件

struct Clickable<Content>: View where Content : View {
    let focused: Binding<Bool>
    let content: () -> Content
    let onClick: () -> Void

    @inlinable public init(focused: Binding<Bool>, onClick: @escaping () -> Void, @ViewBuilder content: @escaping () -> Content) {
        self.content = content
        self.focused = focused
        self.onClick = onClick
    }

    var body: some View {
        ZStack {
            ClickableHack(focused: focused, onClick: onClick)
            content()
        }
    }
}

用法示例:

struct ClickableTest: View {
    @State var focused1: Bool = false
    @State var focused2: Bool = false

    var body: some View {
        HStack {
            Clickable(focused: self.$focused1, onClick: {
                print("clicked 1")
            }) {
                Text("Clickable 1")
                    .foregroundColor(self.focused1 ? Color.red : Color.black)
            }
            Clickable(focused: self.$focused2, onClick: {
                print("clicked 2")
            }) {
                Text("Clickable 2")
                    .foregroundColor(self.focused2 ? Color.red : Color.black)
            }
        }
    }
}