对拖动视图做出反应

时间:2020-03-29 18:36:04

标签: swift swiftui

我有一种看法,只要在其上长按或拖动,便要更改。它可以从视图外部开始,也可以从内部开始。

struct ContentView: View {
    @State var active: Bool = false

    var body: some View {
        Rectangle()
        .fill(self.active ? Color.red : Color.secondary)
    }
}

这种行为的一个例子是iPhone键盘:长按一个键时,它会弹出(active = true)。当您将其移到外部时,它会弹出(active = false),但是下一个键处于活动状态。

我尝试使用LongPressGesture,但无法弄清楚如何使其表现出我想要的效果。

1 个答案:

答案 0 :(得分:0)

我在操场上为您举例说明了如何使用LongPressGestureRecognizer

您将手势识别器添加到要长按的视图中,target是处理手势识别的父控制器(在您的情况下为ContentView),而action是会发生什么情况长按。

在我的实现中,长按视图bodyView可以将其颜色从透明更改为红色。这发生在didSet属性的showBody内部,该属性调用showBodyToggled()。我正在检查手势的state,因为手势识别器将针对每种状态发送消息(我仅在状态为.began时执行操作)。

如果您有任何疑问,请告诉我:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {

    var bodyView: UIView!

    var showBody = false {
        didSet {
            showBodyToggled()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        configureSubviews()
        configureLongPressRecognizer()
    }

    func configureSubviews() {
        bodyView = UIView()
        bodyView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bodyView)

        NSLayoutConstraint.activate([
            bodyView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            bodyView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            bodyView.heightAnchor.constraint(equalToConstant: 200),
            bodyView.widthAnchor.constraint(equalToConstant: 300)
        ])
    }

    func configureLongPressRecognizer() {
        bodyView.addGestureRecognizer(
            UILongPressGestureRecognizer(
                target: self,
                action: #selector(longPressed(_:))
            )
        )
    }

    @objc func longPressed(_ sender: UILongPressGestureRecognizer) {
        // the way I'm doing it: only acts when the long press first happens.
        // delete this state check if you'd prefer the default long press implementation
        switch sender.state {
        case .began:
            showBody = !showBody
        default:
            break
        }
    }

    func showBodyToggled() {
        UIView.animate(withDuration: 0.4) { [weak self] in
            guard let self = self else { return }
            self.bodyView.backgroundColor = self.showBody ? .red : .clear
        }

    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

编辑:

这是SwiftUI中的一个示例,其中长按0.5秒可在红色和黑色之间切换圆圈的颜色

struct ContentView: View {
    @State var active = false

    var longPress: some Gesture {
        LongPressGesture()
            .onEnded { _ in
                withAnimation(.easeIn(duration: 0.4)) {
                    self.active = !self.active
                }
        }
    }

    var body: some View {
        Circle()
            .fill(self.active ? Color.red : Color.black)
            .frame(width: 100, height: 100, alignment: .center)
            .gesture(longPress)
    }
}