gpt4 book ai didi

ios - UITableViewCell 中 UIImageView 上的 UISwipeGesture

转载 作者:行者123 更新时间:2023-11-30 13:54:53 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com