【问题标题】:How to use UIGestureRecognizer in UIScrollView如何在 UIScrollView 中使用 UIGestureRecognizer
【发布时间】:2018-11-13 06:14:04
【问题描述】:

所以我想在我的应用程序中使用点击手势识别器,用户不必滚动屏幕我希望手势识别器仅位于应用程序的一侧,例如从左右 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)

        }

    }

}

【问题讨论】:

  • 你想用手势模拟上一个/下一个
  • 最适合用户界面的东西
  • 分别向左和右添加 2 个不可见视图,宽度为 30 并向它们添加手势,然后将它们添加到主视图而不是滚动视图
  • 您现在有什么问题?无需添加不可见的视图。您可以将识别器添加到主视图。

标签: ios iphone swift uiscrollview uigesturerecognizer


【解决方案1】:

你需要 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

【讨论】:

    【解决方案2】:

    有几种方法可以做到这一点。一种简单的方法是在 z-order 顶部添加不可见的视图,并将点击手势识别器附加到相同的视图上。支持动态方向变化的 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")
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多