在 UITableViewCell 中滑动手势 UIImageView

Posted

技术标签:

【中文标题】在 UITableViewCell 中滑动手势 UIImageView【英文标题】:Swipe Gesture UIImageView in UITableViewCell 【发布时间】:2016-05-28 02:36:07 【问题描述】:

我似乎无法让我的滑动手势识别器为表格视图单元格内的图像视图工作。

我已确认 images.count > 1。

if images.count > 0 
                let imageUrlString = images[0] as String
                let imageUrl = NSURL(string: imageUrlString)
                imageCell.cellImageView.sd_setImageWithURL(imageUrl, placeholderImage: UIImage(named: "placeholder"))

                if images.count > 1 
                    let swipeRightGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
                    swipeRightGestureRecognizer.direction = .Right
                    imageCell.cellImageView.addGestureRecognizer(swipeRightGestureRecognizer)

                    let swipeLeftGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
                    swipeLeftGestureRecognizer.direction = .Left
                    imageCell.cellImageView.addGestureRecognizer(swipeLeftGestureRecognizer)
                
             else 
                imageCell.cellImageView.image = UIImage(named: "placeholder")
            

向左滑动和向右滑动

func swipeLeft(sender: UISwipeGestureRecognizer) 
        imageView = sender.view as! UIImageView

        transition.subtype = kCATransitionFromRight
        imageView.layer.addAnimation(transition, forKey: nil)

        if imageIndex == images.count - 1 
            let urlString = images[0] as String
            let url = NSURL(string: urlString)
            imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "placeholder"))

            imageIndex = 0

         else 
            let urlString = images[imageIndex + 1] as String
            let url = NSURL(string: urlString)
            imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "placeholder"))

            imageIndex += 1
        
    

    func swipeRight(sender: UISwipeGestureRecognizer) 
        imageView = sender.view as! UIImageView

        transition.subtype = kCATransitionFromLeft
        imageView.layer.addAnimation(transition, forKey: nil)

        if imageIndex == 0 
            let urlString = images.last! as String
            let url = NSURL(string: urlString)
            imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "placeholder"))

            imageIndex = images.count - 1

         else 
            let urlString = images[imageIndex - 1] as String
            let url = NSURL(string: urlString)
            imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "placeholder"))

            imageIndex -= 1
        
    

【问题讨论】:

【参考方案1】:

// imageview默认userinteractionenable属性设置为false,所以需要设置为userinteractionenable = true

imageCell.cellImageView.userInteractionEnabled =true

【讨论】:

以上是关于在 UITableViewCell 中滑动手势 UIImageView的主要内容,如果未能解决你的问题,请参考以下文章

滑动手势取消编辑模式后 UITableViewCell 刷新不良

REFrostedViewController 平移手势禁用 tableview 滑动

UITableViewcell 和 UITableVIew 上的手势识别器

UITableView 中的 UISlider 没有响应滑动手势

添加了 UITableViewCell GestureRecognizer 委托不工作

UITableViewCell GestureReognizer 的性能问题