垂直滚动时在未调用的 collectionView 上向左/向右滑动

2024-04-14

我有一个collectionView垂直滚动,覆盖设备上的整个屏幕(即全屏)。

我已经注册了Swipe Left and Right为我的手势collectionView.

//------------right  swipe gestures in collectionView--------------//
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.collectionView.addGestureRecognizer(swipeRight)

    //-----------left swipe gestures in collectionView--------------//
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.collectionView.addGestureRecognizer(swipeLeft)

Problem:左右滑动手势回调不会触发collectionView正在垂直滚动。

有没有任何简单的解决方法。

这是我的全部ViewController Class

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {




@IBOutlet weak var collectionView: UICollectionView!

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    collectionView.dataSource = self
    collectionView.delegate = self


    //------------right  swipe gestures in collectionView--------------//
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.collectionView.addGestureRecognizer(swipeRight)

    //-----------left swipe gestures in collectionView--------------//
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.collectionView.addGestureRecognizer(swipeLeft)


}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.lable.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor.yellowColor()

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.row)
}

func rightSwiped()
{

    print("right swiped ")
}

func leftSwiped()
{

    print("left swiped ")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

这是我的collectionView看起来像

EDIT 1

已解决,解决方案请点击here https://stackoverflow.com/questions/38346679/swipe-left-right-on-collectionview-not-called-while-scrolling-vertically/38369367#38369367


The delegate默认手势识别器UICollcetionView是集合视图对象本身(显然)。

默认实现-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizerreturns YES在 UICollectionView 类中。

因此,为了解决您的问题,您需要将集合视图对象设置为“左”和“右”滑动手势识别器的委托,如下所示。

swipeRight.delegate = collectionView;
swipeLeft.delegate = collectionView;

这应该使您rightSwiped() and leftSwiped()当发生相应的滑动时被触发。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

垂直滚动时在未调用的 collectionView 上向左/向右滑动 的相关文章

  • Android - API 请求

    我开发了一个应用程序 它也在 iPhone 上 问题出在 api 请求上 我为所有请求设置了超时 有时会出现 30 60 秒的中断 看起来这个应用程序执行了几个请求 然后就中断了 一直超时 大约 45 秒后一切正常 不知道是服务器问题还是安
  • 如何在 NSDate 变量中仅设置时间?

    我有 NSDate 变量 只想更改时间 不应更改日期 是否可以 例如 用户在 DatePicker 日期中选择日期间隔 如果是开始日期 我想将时间设置为 00 00 00 如果是结束日期 我将时间设置为 23 59 59 感谢你的帮助 问候
  • 删除具有estimatedItemSize 的项目时 UICollectionView 单元格大小会调整

    我有一个简单的项目 其中的故事板仅包含一个UICollectionViewController 使用 Xcode 7 1 1 为 iOS 9 1 构建 class ViewController UICollectionViewControl
  • 有没有办法阻止 iOS 上的 Safari 在关闭时清除网站的 cookie?

    我的移动网络应用程序的一位用户抱怨说 每次他关闭手机屏幕后使用该应用程序时 他都必须重新登录该应用程序 发生的情况是 当屏幕关闭时 或者当您通过双击主页按钮并滑开 Safari 来完全关闭 Safari 时 Safari 会清除该网站的 C
  • NSOperation 中委托的使用

    我正在尝试利用CLLocationManager in an NSOperation 作为其中的一部分 我需要能够startUpdatingLocation然后等到收到 CLLocation 后才完成操作 目前我已经完成了以下操作 但是委托
  • 在 Swift 中将动态 Int 变量从一个类传递到另一个类

    我是 swift 2 的新手 我陷入了将变量从一个类传递到另一个类的过程中 我有一个类 GameScene 我有一个公共变量score并且在更新功能中不断更新 我想发送score两个节点相互碰撞时的值 一旦它发生碰撞 我就会使用 mainv
  • ShareKit + Facebook 身份验证不起作用

    我正在使用 ShareKit 从我正在开发的 PhoneGap 应用程序发布到 Facebook 我在 Facebook 中创建了该应用程序 并在我的项目中安装了该插件 Twitter 工作正常 但当我尝试在 Facebook 上分享时出现
  • UICollectionReusableView 方法未被调用

    我希望我的部分位于UICollectionView有一个带有图像的标题 我已按照以下步骤操作 在故事板中 分配了一个标题作为我的附件UICollectionView 给它一个标识符 创建了一个子类UICollectionReusableVi
  • 将语音添加到自定义 UIMenuController

    我创建了一个自定义UIMenuController in a UIWebView但它似乎摆脱了 说出选择 选项UIMenuController在那之后 所有测试设备上的 偏好设置 中都打开了发言选择选项 并且它出现在其他应用程序中 包括非
  • iOS NSURLSession,如何在didCompleteWithError中重试

    我想在我的服务器上尝试一次调用 直到成功为止 我想每 30 秒尝试一次 所以我使用 NSURLSession 进行通话 NSURLSessionDownloadTask task self session downloadTaskWithR
  • 使用prepareForSegue传递数据

    我试图将数据从viewController 1传递到viewController2 我有2个按钮和1个segue 因此有一个segue标识符 这2个按钮 按下时每个按钮应显示 1个标签用于显示标题 1个textView用于显示定义 我很难显
  • 如何更改 UINavigationController 后退按钮名称?

    我有一个UIViewController我正在从第一个视图控制器导航到第二个视图控制器 我想更改显示在navigationcontroller为了回去 SecondViewController secondController Second
  • 我如何从 iPhone 设备获取电子邮件历史记录..?

    friends 我想从我的 iPhone 访问电子邮件历史记录 并且还希望在收到新邮件时收到通知 如果可能的话 请向我提供源代码片段 Thanks 简而言之 使用任何已记录的 API 都是不可能的
  • 在 JSQMessagesViewController 中显示 LocationMediaItem

    我刚刚尝试实施LocationMediaItem in my Xamarin iOS应用程序使用JSQMessagesViewController 一切都很顺利 唯一的问题是UICollectionView应该显示位置的单元格永远停留在加载
  • UITextView:内存使用量巨大

    我在 UITextView 中遇到了内存使用过多的问题 我正在将 50Kb ascii 文本文件加载到 NSString 中 并将其分配给应用程序中空 UITextView 组件的 text 属性 这立即使我的内存占用量增加了 100Mb
  • 所有针对 iTunes Connect 的构建,导入此构建时出错 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我将构建上传到应用程序商店 之后它在活动中显示错误 任何人都可以帮帮我 SOLVED 转到构建设置 gt 搜索 bitcode gt 转启
  • 如何在 Swift 中创建 UIAlertView?

    我一直在努力在 Swift 中创建 UIAlertView 但由于某种原因我无法得到正确的语句 因为我收到此错误 找不到接受提供的 init 重载 论点 我是这样写的 let button2Alert UIAlertView UIAlert
  • let/var 如何解决可变性? [复制]

    这个问题在这里已经有答案了 我没有任何问题 我只是想对有关可变性的问题进行一些澄清 在 Objective C 中我们会使用例如NSMutableArray得到一个可变数组和NSArray得到一个不可变的 我对两者的内部运作了解不多 但据我
  • 由于 2.23 导致 iOS 应用程序被拒绝 - iOS 数据存储指南

    以下是 Apple 关于拒绝的消息 2 23 应用程序必须遵循 iOS 数据存储指南 否则将被拒绝 2 23 详情 在启动和内容下载时 您的应用程序会存储 6 5 MB 这并不意味着 遵守 iOS 数据存储指南 下一步 请验证只有用户使用您
  • Google Cloud Messaging 显示成功消息但未发送 iOS

    所以我在使用 Google Cloud Messaging 时遇到了一个非常奇怪的问题 我遇到的问题是它正在成功注册设备 并且当发送消息时我会收到来自 Google 的成功消息 但设备永远不会收到任何消息 我从 GCM 得到的消息是 res

随机推荐