在UITableViewCell中的UIImageView上的UISwipeGesture

时间:2015-11-17 12:07:14

标签: ios objective-c xcode swift uitableview

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

enter image description here

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

以下是滑动的代码,这适用于单个视图控制器: -

    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)

1 个答案:

答案 0 :(得分:0)

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"))
    }
}