【问题标题】:Swipe Gesture UIImageView in UITableViewCell在 UITableViewCell 中滑动手势 UIImageView
【发布时间】: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
        }
    }

【问题讨论】:

    标签: ios swift uiimageview uigesturerecognizer


    【解决方案1】:

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

    imageCell.cellImageView.userInteractionEnabled =true
    

    【讨论】:

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