在UITableViewCell中滑动手势UIImageView

时间:2016-05-28 02:36:08

标签: ios swift uiimageview uigesturerecognizer

我似乎无法使用我的滑动手势识别器来处理表格视图单元格内的图像视图。

我已确认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")
            }

swipeLeft和swipeRight

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 个答案:

答案 0 :(得分:0)

// imageview默认将userinteractionenable属性设置为false,因此你需要将其设置为userinteractionenable = true

imageCell.cellImageView.userInteractionEnabled =true