草庐IT

ios - (Swift) UIPanGestureRecognizer 在 View 上工作正常,直到将 subview 添加到它

coder 2023-09-06 原文

我有以下 View (它是整体 View Controller 的 subview ):

lazy var superView: UIView = {
    let cv = UIView()

    cv.backgroundColor = .gray
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.layer.cornerRadius = 5     
    cv.layer.masksToBounds = true     
    cv.isUserInteractionEnabled = true
    cv.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan)))

    return cv
}()

这里是我为其设置约束的地方:

// constraints for super view
func setupSuperView() {
    superView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    superView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

    superView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
    superView.heightAnchor.constraint(equalTo: view.heightAnchor, constant: -200).isActive = true

    // ISSUE: For some reason, adding this subview with or without constraints screws up the pan functionality of the superview
    superView.addSubview(subView)
}

这是我添加到 superView 内部的 subview (我将其视为容器 View ):

lazy var subview: UIImageView = {
    let sv = UIImageView()

    sv.translatesAutoresizingMaskIntoConstraints = false
    sv.contentMode = .scaleAspectFill        

    return sv
}()

及其约束:

// constraints for yes/no view within superview
func setupSubView() {
    subView.centerXAnchor.constraint(equalTo: superView.centerXAnchor).isActive = true
    subView.centerYAnchor.constraint(equalTo: superView.centerYAnchor).isActive = true
}

最后,我在这里建立平移手势的功能:

 // pan functionality for swiping on superview
func handlePan(gesture: UIPanGestureRecognizer) {

    guard let superview = gesture.view else {
        return
    }

    // point you are tapped on
    let point = gesture.translation(in: view)

    // reference for how far left or right of the center of the superview your pan is
    let xFromCenter = superview.center.x - view.center.x

    // allows the superview to move with your finger
    superview.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)

    // dragged right
    if xFromCenter > 0 {
        subView.image = #imageLiteral(resourceName: "yes")
        subView.tintColor = UIColor.green

    // else dragged left
    } else {
        subView.image = #imageLiteral(resourceName: "no")
        subView.tintColor = UIColor.red
    }

    // when you let go
    if gesture.state == UIGestureRecognizerState.ended {
        // animate superview back to center where it originally was
        UIView.animate(withDuration: 0.2) {

            superview.center = self.view.center
        }
    }

}

在我添加 subview 之前(以及当我将其注释掉时),平移手势在父 View 上运行得非常好。但是,当我将 subview 限制在 super View 的中心并尝试在 View Controller 周围移动 super View 时,它的行为很奇怪(只正确地向左移动,向右移动时不断地猛拉回中心)。

非常感谢任何帮助。

最佳答案

在您的 subview 上禁用userinteraction,我认为它会起作用!

你的代码应该是这样的,

  subView.isUserInteractionEnabled = false

关于ios - (Swift) UIPanGestureRecognizer 在 View 上工作正常,直到将 subview 添加到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067996/

有关ios - (Swift) UIPanGestureRecognizer 在 View 上工作正常,直到将 subview 添加到它的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  6. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  9. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  10. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

随机推荐