首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在UIScrollView中使用UIGestureRecognizer

如何在UIScrollView中使用UIGestureRecognizer
EN

Stack Overflow用户
提问于 2018-06-04 06:39:52
回答 2查看 191关注 0票数 1

因此,我希望在我的应用程序中有轻击手势识别器,用户不必滚动屏幕,我希望手势识别器只在应用程序的一侧,如30左右。

代码语言:javascript
复制
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)

        }

    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-04 19:01:34

有几种方法可以做到这一点。其中一种简单的方法是在z顺序的顶部添加不可见的视图,并将轻击手势识别器附加到该视图上。支持动态方向更改的AutoLayout组件:

代码语言:javascript
复制
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")
}

}

票数 0
EN

Stack Overflow用户

发布于 2018-06-04 09:43:00

您需要UIScreenEdgePanGestureRecognizer

代码语言:javascript
复制
/*! 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50671615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档