ios - UITableViewCell 中 UIImageView 上的 UISwipeGesture

标签 ios objective-c xcode swift uitableview

我有一个 UITableView,每个单元格中都有一个 ImageView 和多个图像。我想使用 pageControl 指示器在图像之间滑动,类似于下面的设计:-

enter image description here

我已经在带有页面和滑动手势的单个 viewController 中实现了 imageView,但是,每当我尝试将其添加到 UITableViewCell 时,它每 10 次只能识别 1 次滑动。

这是滑动的代码,它在单个 View Controller 中工作:-

    override func viewDidLoad() {
    super.viewDidLoad()

    pageControl.currentPage = 0
    pageControl.numberOfPages = maxImages

    let swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.imageView.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.imageView.addGestureRecognizer(swipeLeft)

    imageView.image = UIImage(named:"sample_spring3")

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func swiped(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Right :
            print("User swiped right" , imageIndex)

            // decrease index first
            imageIndex--
            pageControl.currentPage = imageIndex
            // check if index is in range

            if imageIndex < 0 {

                imageIndex = maxImages - 1
                pageControl.currentPage = imageIndex
            }

            imageView.image = UIImage(named: imageList[imageIndex])

        case UISwipeGestureRecognizerDirection.Left:
            print("User swiped Left", imageIndex)

            // increase index first
            imageIndex++
            pageControl.currentPage = imageIndex
            // check if index is in range

            if imageIndex >= maxImages {

                imageIndex = 0
                pageControl.currentPage = imageIndex

            }
            imageView.image = UIImage(named: imageList[imageIndex])

        default:
            break //stops the code/codes nothing.

        }
    }
}

这无法识别滑动:-

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! FeedTableViewCell

    let product = products[indexPath.section]

    let screenRect: CGRect = UIScreen.mainScreen().bounds



    var maxImages = product.productImage.count
    var imageIndex: NSInteger = 0

    // PRODUCT IMAGE VIEW WITH SWIPES

    cell.productImageViewPageControl.numberOfPages = product.productImage.count
    cell.productImageViewPageControl.currentPage = 0

    var imageFile : PFFile = product.productImage[0]
    //cell.productImageView.addSubview(activityIndicator)
    //activityIndicator.startAnimating()

    imageFile.getDataInBackgroundWithBlock { (data, error) -> Void in
        if let downloadedImage = UIImage(data: data!)
        {
            cell.productImageView.image = downloadedImage
            self.activityIndicator.stopAnimating()
        }
    }

    cell.productImageView.userInteractionEnabled = true
    cell.productImageView.tag = indexPath.row

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    cell.productImageView.addGestureRecognizer(swipeRight)

    var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down
    cell.productImageView.addGestureRecognizer(swipeDown)

    var tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
    tapped.numberOfTapsRequired = 1
    cell.productImageView.addGestureRecognizer(tapped)

最佳答案

UISwipeGestureRecognizer 实现一个处理程序,如下所示:

func handleSwipe(gesture: UISwipeGestureRecognizer) {
    gesture.direction = .Right

    let imageCount = productImageArr.count
    if imageCount != i {
        myCell.pageViewCon.currentPage = i
        myCell.imageViewPhoto = gesture.view as! UIImageView
    let url = NSURL(string:(imageURL + (productImageArr[i]["images"] as? String)!))
    myCell.imageViewPhoto?.setImageWithURL(url!, placeholderImage: UIImage(named:"Kloudrac-Logo"))
    i += 1
    } else {
        i = 0
        myCell.pageViewCon.currentPage = i
        let url = NSURL(string:(imageURL + (productImageArr[i]["images"] as? String)!))
        myCell.imageViewPhoto?.setImageWithURL(url!, placeholderImage: UIImage(named:"Kloudrac-Logo"))
    }
}

关于ios - UITableViewCell 中 UIImageView 上的 UISwipeGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756520/

相关文章:

ios - 如何从Firebase Storage下载并声明大量图像?

ios - HTML 输入 jQuery UI 日期选择器与 iOS 原生日期输入

ios - 为什么 ARKit 应用程序在几天后停止工作?

ios - 使用 xib 子类化 UICollectionViewController

ios - 方法参数需要类型和协议(protocol)

ios - Objective-C:如何获取 UIScrollView 的缩放内容

objective-c - 在 Mac OS X 上创建类型为 'Web Form Password' 的钥匙串(keychain)项

objective-c - 在 block 内使用指向 block 外声明的对象的指针

swift - 我可以将多个 Playground 文件放入一个 Xcode 工作簿吗?

ios - 带部分的动态 TableView