【问题标题】:control multiple buttons with one swipe gesture swift一键手势控制多个按钮
【发布时间】:2016-01-31 13:18:35
【问题描述】:

我有一组UIImageView 打印在矩阵中的主视图(作为子视图)上。

UIImageViews 在我点击它们时进行交互(当我触摸其中一个时它会像像素一样工作(从黑色变为绿色)

但我想用滑动手势来做到这一点,所以我可以用一次滑动触发多个“像素”(UIImageView)

我为安卓找到了这个triggering-multiple-buttonsonclick-event-with-one-swipe-gesture 我想知道在 ios 中是否有类似的东西可以识别一般触摸(不是点击或滑动),所以我可以寻找它。

所有这些的主要目的是用一个滑动手势在像素矩阵上绘制“形状”。

如果您认为有其他方法对您有帮助,我将很乐意在这里讨论。

非常感谢

【问题讨论】:

    标签: ios swift uiimageview ontouch


    【解决方案1】:

    您正在寻找UIGestureRecognizer。 有了它,您可以添加多种手势,如滑动、触摸等。

    您还可以获得位置、持续时间以及基本上所有有关它的信息。

    您可以在此链接中查看分步教程。 http://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial

    在苹果文档中也是如此。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/

    【讨论】:

      【解决方案2】:

      我设法使用 touchesMoved 和 touchesEnded 进行滑动操作

      虽然主要思想是使用触摸的坐标调用 UIIMageViews,并将其与 touchesMoved 函数中的 UIImageViews 坐标进行比较

      使用标志禁用已编辑的 UIIMageViews(当我在同一个触摸会话中时,手指仍在屏幕上)并刷新 UIImageViews 以在 touchesEnded 中再次可编辑

          func swipeTouches(touches: NSSet!) {
                  // Get the first touch and its location in this view controller's view coordinate system
                  let touch = touches.allObjects[0] as! UITouch
                  let touchLocation = touch.locationInView(self.view)
      
                  for pixel in pixelArrays {
                      // Convert the location of the obstacle view to this view controller's view coordinate system
                      let pixelViewFrame = self.view.convertRect(pixel.pixelImage.frame, fromView: pixel.pixelImage.superview)
      
                      // Check if the touch is inside the obstacle view
                      if CGRectContainsPoint(pixelViewFrame, touchLocation) {
                          // check if the pixel is Editable
                          if(!pixel.isEditable){
                              let index = pixel.index
                              pixelArrays.insert(updatePixel(index) , atIndex: index)
                          }
                      }
                  }
              }
      

      我现在唯一的问题是,如果在其中一个 UIImageViews 上开始滑动,touchesMoved 函数将其视为要查找坐标的视图,而其他 UIImageViews 不受影响

      我解决这个问题的想法是在所有 UIImageViews 顶部添加图层并禁用他们已经拥有的点击识别并使用坐标方式实现点击。

      我很高兴知道是否有其他方法可以做到这一点

      更新: 我设法通过我写的东西来解决上面的问题,但我没有添加另一层,而是禁用了所有 UIImageViews 上的触摸,并使用触摸坐标和它们调用它们

      非常感谢

      【讨论】:

        猜你喜欢
        • 2020-08-17
        • 2012-11-10
        • 1970-01-01
        • 2021-08-09
        • 1970-01-01
        • 1970-01-01
        • 2016-04-05
        • 1970-01-01
        • 2011-01-12
        相关资源
        最近更新 更多