2016-06-18 36 views
0

我的收藏视图效果很好。它显示一张照片的网格,并列出其中数百个。您可以垂直滑动来浏览所有内容。生活很好。但是,我现在有一个新的要求。我需要能够检测用户何时向左或向右滑动。我需要能够拦截此手势,以便我可以将行为附加到左右滑动,同时保持我的收藏视图的垂直滚动功能不变。有任何想法吗?UicollectionView在swift ios中的视图之间滚动?

在swift?

如果它有助于参考继承人链接到我的Github项目。

https://github.com/StarShowsStudios/GodCards

如果您打开该项目在Xcode中就可以看到详细视图控制器。它根据选定的收集单元控制器从名为卡的plist文件中获取信息。

回答

0

您可以添加左右滑动手势识别器以检测左右滑动。

override func awakeFromNib() { 
     super.awakeFromNib() 

// Add Left Swipe Gesture 
     let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:))) 
     swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
     self.addGestureRecognizer(swipeLeft) 

     // Add Right Swipe Gesture 
     let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:))) 
     swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
     self.addGestureRecognizer(swipeRight) 
    } 

    // This function detects Swipe direction and perform action 
    func respondToSwipeGesture(gesture: UIGestureRecognizer) { 

     if let swipeGesture = gesture as? UISwipeGestureRecognizer { 
      switch swipeGesture.direction { 
      case UISwipeGestureRecognizerDirection.Right: 
       print("Swiped right") 
       rightSwipeAction() 

      case UISwipeGestureRecognizerDirection.Left: 
       print("Swiped left") 
       leftSwipeAction() 

      default: 
       break 
      } 
     } 
    } 
相关问题