【问题标题】:Tap gesture on multiple imageview inside scrollview not firing event , only first imageview tap gesture working?在滚动视图内的多个图像视图上点击手势不触发事件,只有第一个图像视图点击手势有效?
【发布时间】:2016-03-30 21:04:38
【问题描述】:

场景是我在滚动视图中添加了图像视图,然后在每个图像视图上添加了点击手势,但只有第一个图像视图触发事件。

试过以下:

1.创建了 imageview 的扩展

extension UIImageView {
  public func openMenu() {
    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("handleTapGesture:"))
    //mImg.addGestureRecognizer()
    self.addGestureRecognizer(tapGestureRecognizer)
    print("open")
  }
}

2。在循环中添加了点击手势(在滚动视图上添加图像视图的方式与添加点击手势的方式相同)

//the loop go till all images will load
for var index = 0; index < imagesName.count; index++ {
  let mImg: UIImageView = UIImageView()          
  let pDict = homePageProductArr[index] as NSDictionary

  //Create a UIVIEW 
  let containerView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 100))
  let nameLabel:UILabel = UILabel(frame:CGRect(x: xOffset+20, y: 85, width: 100, height: 20))
  nameLabel.text = (pDict.objectForKey("CategoryName") as! String)
  nameLabel.font = UIFont(name: "Helvetica Neue", size: 10.0)
  containerView.addSubview(nameLabel)

  let tapMenuCategoryImageTGR = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
  containerView.addGestureRecognizer(tapMenuCategoryImageTGR)

  // make the imageview object because in scrollview we need image
  mImg.frame = CGRectMake(5 + xOffset, 0, 80, 80)
  // the offset represent the values, used so that topleft for each image will
  // change with(5+xOffset, 0)and the bottomright(160, 110)
  //mImg.image = UIImage(named: imagesName[index])
  mImg.imageFromUrl(imagesName[index])
  //mImg.imageFromServerURL(imagesName[index])
  //Creating Circular Image
  //mImg.layer.borderWidth = 1
  mImg.layer.masksToBounds = true
  mImg.layer.borderColor = UIColor.whiteColor().CGColor
  mImg.layer.cornerRadius = mImg.frame.height/2
  mImg.clipsToBounds = false
  mImg.openMenu()
  mImg.userInteractionEnabled = true
  mImg.addGestureRecognizer = tapMenuCategoryImageTGR

  // The image will put on the img object
  images.insert(mImg, atIndex: index)
  //Put the img object at the images array which is mutable array
  scrollview.contentSize = CGSizeMake(120 + xOffset, 110)
  //scroll view size show after 125 width the scroll view enabled 
  scrollview.addSubview(images[index] as! UIView)
  //set images on scroll view
  xOffset += 100
}

欢迎分享你的建议。

【问题讨论】:

  • 您是否尝试在 for 之前初始化 tapMenuCategoryImageTGR ?

标签: ios swift uiimageview uitapgesturerecognizer


【解决方案1】:

这不是 UIScrollView 的问题。我们可以使用 Gesture 来进行单个视图。 下面给出了您的问题的另一种解决方案-

  1. 您必须全局声明点击手势。

    var  singleTap:UITapGestureRecognizer!
    
  2. 在这一步中,我们创建一个函数,该函数接受 UIimageView 并在其顶部添加手势。

      func setGesture(img_View:UIImageView)  {
          singleTap = UITapGestureRecognizer.init(target: self, action: Selector("resignResponder"))
          singleTap.numberOfTapsRequired = 1
        view.addGestureRecognizer(singleTap)
    

    }

  3. 现在创建手势动作。

    func resignResponder()  {
    
    }
    

这些是解决问题的三个步骤。称呼 每个 imageView 的“setGesture”。您可以成功识别 点击每个 imageView。

【讨论】:

    【解决方案2】:

    最终通过在 imageview 上添加按钮解决,并使其背景颜色清晰,标题为“”:

    这里是代码片段:

     //print(mDict.objectForKey("ImageURL"))
        //var scrollWidth: Int = 120
        scrollview.contentSize = CGSizeMake(120.0, 80.0)
        var xOffset: CGFloat = 5
        //the loop go till all images will load
        for index in 0 ..< imagesName.count {
    
            let mImg: UIImageView = UIImageView()
            //set placeholder image
            mImg.image = UIImage(named: "lazyload_icon.png")
            let pDict = homePageProductArr[index] as NSDictionary
    
            //Create a UIVIEW 
            let containerView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 120))
            let nameLabel:UILabel    = UILabel(frame:CGRect(x: xOffset+20, y: 95, width: 100, height: 20))
            nameLabel.text = (pDict.objectForKey("CategoryName") as! String)
            nameLabel.font = UIFont(name: "Helvetica Neue", size: Constant().HomeLabelFontSize)
            nameLabel.textColor = UIColor.colorWithHexString(Constant().HomeLabelHexColorCode)
            //set line and wrap text
            nameLabel.numberOfLines = 0
            nameLabel.sizeToFit()
            containerView.addSubview(nameLabel)
            containerView.userInteractionEnabled = false
    
            // make the imageview object because in scrollview we need image
            mImg.frame = CGRectMake(xOffset , 0, 90, 90)
            // the offset represent the values, used so that topleft for each image will
            // change with(5+xOffset, 0)and the bottomright(160, 110)
            //mImg.image = UIImage(named: imagesName[index])
            mImg.imageFromUrl(imagesName[index])
            //mImg.imageFromServerURL(imagesName[index])
            //Creating Circular Image
            mImg.layer.masksToBounds = true
            mImg.layer.borderColor = UIColor.whiteColor().CGColor
            mImg.layer.cornerRadius = mImg.frame.height/2
            mImg.clipsToBounds = false
            mImg.userInteractionEnabled = false
    
    
            // The image will put on the img object
            images.insert(mImg, atIndex: index)
            // Put the img object at the images array which is mutable array
            scrollview.contentSize = CGSizeMake(120 + xOffset, 110)
            //scroll view size show after 125 width the scroll view enabled
            containerView.addSubview(images[index] as! UIView)
            scrollview.addSubview(containerView)//images[index] as! UIView)
            // set images on scroll view
            xOffset += 100
    
            //Adding button to fire event
            let touchButton:UIButton = UIButton()
            touchButton.frame = mImg.frame
            touchButton.backgroundColor = UIColor.clearColor()
            touchButton.tag = index
            touchButton.addTarget(self, action: Selector("touchCategoryAction:"), forControlEvents: UIControlEvents.TouchUpInside)
            touchButton.setTitle("", forState: UIControlState.Normal)
            scrollview.addSubview(touchButton)
            scrollview.bringSubviewToFront(touchButton)
    

    【讨论】:

      【解决方案3】:

      您可以使用 UIButton 并设置 背景图像到按钮并设置标题“”。那你不 需要同时添加 UIImageView 和 UIButton。 UIButton 可以轻松搞定 UIScrollView 上的点击事件。

      【讨论】:

      • 单一问题有多种解决方案。我们必须选择最好的1。 @ShobhakarTiwari
      • 你应该理解完整的问题,它不是关于单个图像视图的点击,它已经在问题本身中完成,而是多个图像视图水平滚动,所以这是另一个问题。
      • 是的,你是对的,它是多个图像视图水平滚动。您可以轻松设置多个按钮的标签。并检查它们是从哪里得到水龙头的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2016-09-13
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多