【问题标题】:UIImage wont recognize tap gesture SwiftUIImage 无法识别轻按手势 Swift
【发布时间】:2016-07-14 21:02:11
【问题描述】:

我正在尝试将点击手势识别器添加到侧滚动视图中的一些 UIImages,但图像完全无法识别点击,我看不出哪里出错了。我已经尝试过“scrollView.bringSubViewToFront(imgView)”,因为我认为它们可能会被其他视图层掩埋,但这也没有奏效。 “contentView”是有问题的 UIImageView,我的 scrollView 只是这些的集合。在这里的任何帮助将不胜感激,谢谢。

//function to create contentScrollView for MiniMatches
func setupMiniContentScroll(contentScroll: UIScrollView) {
    let scalar:Double = 4/19
    let contentViewDimension = contentScroll.frame.width * CGFloat(scalar)
    let contentScrollWidth = CGFloat(LocalUser.matches.count) * (contentViewDimension + CGFloat(12)) - CGFloat(12)
    let matchManager = MatchesManager()

    for index in 0..<LocalUser.matches.count {
        let match = LocalUser.matches[index]
        matchManager.retrieveMatchThumbnail(match) { img, error in


            if let img = img {

                //create the mini matches views
                let xOrigin = index == 0 ? 12 : CGFloat(index) * contentViewDimension + (CGFloat(12) * CGFloat(index) + CGFloat(12))
                let contentFrame = CGRectMake(xOrigin, 10, contentViewDimension, contentViewDimension)
                let contentView = self.makeMiniContentView(contentFrame, image: img, matchedPrice: match.matchedPrice)

                let tap = UITapGestureRecognizer(target: self, action: #selector(BrowseViewController.toggleItemInfo(_:)))
                contentView.addGestureRecognizer(tap)
                self.miniMatchContainer.append(contentView)

                //update the contentScrollView
                dispatch_async(dispatch_get_main_queue()) {

                    let contentLabelFrame = CGRect(x: xOrigin, y: contentFrame.height + 15, width: contentFrame.width, height: 20)
                    let contentLabel = self.makeMiniContentLabel(contentLabelFrame, itemName: match.itemName)
                    let priceLabel = self.makeMiniPriceLabel(contentFrame, matchedPrice: match.matchedPrice)

                    contentScroll.addSubview(contentView)
                    contentScroll.addSubview(contentLabel)
                    contentScroll.addSubview(priceLabel)
                    contentScroll.contentSize = CGSizeMake(contentScrollWidth + CGFloat(16), contentScroll.frame.height)
                }
            }

        }
    }
}

【问题讨论】:

  • 尝试使用其中的点击手势创建一个单独的函数,例如 func imageTapped() { let tap = UITapGestureRecognizer(target: self, action: #selector(BrowseViewController.toggleItemInfo(_:))) contentView.addGestureRecognizer(tap) self.miniMatchContainer.append(contentView) } 在你的 if 语句中调用函数 imageTapped()

标签: ios swift uiimage uitapgesturerecognizer


【解决方案1】:

您是否将UIImage 属性userInteractionEnabled 设置为true

【讨论】:

    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多