2015-11-17 42 views
0

我在每個單元格和多個圖像中都有一個帶有ImageView的UITableView。我想用的PageControl指標,類似於下面的設計圖像之間的刷卡 -UITableViewCell中的UIImageView上的UISwipeGesture

enter image description here

我已經實現了的ImageView與頁面的單個的viewController和滑動手勢,然而,每當我嘗試將它添加到UITableViewCell中,它會每10次識別一次滑動。

下面是刷卡的代碼,這是工作在一個單一的視圖控制器: -

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) 
+0

您是否嘗試將手勢識別器添加到單元格的contentView中或細胞的觀點? –

+0

問題在於,我只想在imageView上滑動手勢,而不是在整個單元格上。 – rohan23

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

使用此功能通過滑動手勢更改圖像 –

相關問題