如何在 UIScrollView 中使用 UIGestureRecognizer

How to use UIGestureRecognizer in UIScrollView

所以我想在我的应用程序中使用轻击手势识别器,用户不必滚动屏幕我希望手势识别器仅位于应用程序的一侧,例如左右 30 度。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!

    var images = [UIImageView]()
    var contentWidth: CGFloat = 0.0
    var contentAdded = false

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if !contentAdded {
            contentAdded = true

            for x in 0...2 {
                let image = UIImage(named: "icon\(x).png")
                let imageView = UIImageView(image: image)
                images.append(imageView)

                var newX: CGFloat = 0.0
                newX = view.frame.midX + view.frame.size.width * CGFloat(x)

                contentWidth += view.frame.size.width

                scrollView.addSubview(imageView)

                imageView.frame = CGRect(x: newX - 75, y: (view.frame.size.height / 2) - 75, width: 150, height: 150)

            }
            scrollView.contentSize = CGSize(width: contentWidth, height: view.frame.size.height)

        }

    }

}

你需要 UIScreenEdgePanGestureRecognizer

/*! This subclass of UIPanGestureRecognizer only recognizes if the user slides their finger
in from the bezel on the specified edge. */
NS_CLASS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED @interface UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer
@property (readwrite, nonatomic, assign) UIRectEdge edges; //< The edges on which this gesture recognizes, relative to the current interface orientation
@end

有几种方法可以做到这一点。一种简单的方法是在 z 顺序之上添加不可见视图,并将点击手势识别器附加到相同的视图上。支持动态方向更改的 AutoLayout 内容:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Call this at the end to make sure our Left/Right taps are on top of z-index
    setupLeftRightTaps()
}

func setupLeftRightTaps() {
    let leftView = UIView()
    // leftView.backgroundColor = .yellow
    leftView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(leftTap)))
    view.addSubview(leftView)

    let rightView = UIView()
    // rightView.backgroundColor = .red
    rightView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(rightTap)))
    view.addSubview(rightView)

    // Layout
    rightView.translatesAutoresizingMaskIntoConstraints = false
    leftView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        leftView.topAnchor.constraint(equalTo: view.topAnchor),
        leftView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        leftView.widthAnchor.constraint(equalToConstant: 30),

        rightView.topAnchor.constraint(equalTo: view.topAnchor),
        rightView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        rightView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        rightView.widthAnchor.constraint(equalToConstant: 30)
        ])
}

@objc func leftTap(_ sender: UITapGestureRecognizer) {
    print("Left Tapped")
}

@objc func rightTap(_ sender: UITapGestureRecognizer) {
    print("Right Tapped")
}

}