0
我在每個單元格和多個圖像中都有一個帶有ImageView的UITableView。我想用的PageControl指標,類似於下面的設計圖像之間的刷卡 -UITableViewCell中的UIImageView上的UISwipeGesture
我已經實現了的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)
您是否嘗試將手勢識別器添加到單元格的contentView中或細胞的觀點? –
問題在於,我只想在imageView上滑動手勢,而不是在整個單元格上。 – rohan23
當然,但是當你這樣做時,你能否驗證它是否正常工作?它至少可以對問題提供一些見解。 –