在 UITableViewCell 的 imageView 中添加 UIPagerControl
add UIPagerControl inside a UITableViewCell's imageView
在我的 tableView 的单元格中,我有一个 ImageView,我想在其中插入一个 UIPageControll,这样我就可以在我的 ImageView 中添加多个图像并像这样滚动它:
这是我尝试过的(但卡住了):
表格视图单元格:
class EventsTableViewCell: UITableViewCell {
@IBOutlet var coverView: UIView!
@IBOutlet var pageController: UIPageControl!
@IBOutlet var imgView: UIImageView!
let swipeGestureLeft = UISwipeGestureRecognizer()
let swipeGestureRight = UISwipeGestureRecognizer()
...................... ...........
...........
}
表格视图:
///// inside cellforRowAtIndexPath
........
PagerController = cell.pageController
swipeGestureLeft = cell.swipeGestureLeft
swipeGestureRight = cell.swipeGestureRight
coverView = cell.coverView
// set gesture direction
self.swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.Left
self.swipeGestureRight.direction = UISwipeGestureRecognizerDirection.Right
// add gesture in to view
coverView.addGestureRecognizer(self.swipeGestureLeft)
coverView.addGestureRecognizer(self.swipeGestureRight)
// add gesture target
self.swipeGestureLeft.addTarget(self, action: #selector(self.handleSwipeLeft(_:)))
self.swipeGestureRight.addTarget(self, action: #selector(self.handleSwipeRight(_:)))
.
.
.
.
.
.
// functions for recognising the Gestures
func handleSwipeLeft(gesture: UISwipeGestureRecognizer){
if self.PagerController.currentPage != 2 {
print("left :\(self.PagerController.currentPage) ")
self.PagerController.currentPage += 1
imgView.image = UIImage(named: "cover\(self.PagerController.currentPage)")
}
}
//
func handleSwipeRight(gesture: UISwipeGestureRecognizer){
if self.PagerController.currentPage != 0 {
print("right :\(self.PagerController.currentPage)")
imgView.image = UIImage(named: "cover\(self.PagerController.currentPage)")
self.PagerController.currentPage -= 1
}
}
你可以看到我能够识别我的 coverView
上的手势,这是一个覆盖 ImageView 的 UIView,我也能够调用这个函数,但现在我要如何改变图像?任何线索或指导都会对我很有帮助
P.s 如果我的问题不够清楚,请告诉我,我会添加一些细节
在您的 cellForRowAt 索引中添加此代码
cell.imgView.userInteractionEnable = true
cell.imgView.addGestureRecognizer(self.swipeGestureLeft)
cell.imgView.addGestureRecognizer(self.swipeGestureRight)
希望对您有所帮助。
在我的 tableView 的单元格中,我有一个 ImageView,我想在其中插入一个 UIPageControll,这样我就可以在我的 ImageView 中添加多个图像并像这样滚动它:
这是我尝试过的(但卡住了):
表格视图单元格:
class EventsTableViewCell: UITableViewCell {
@IBOutlet var coverView: UIView!
@IBOutlet var pageController: UIPageControl!
@IBOutlet var imgView: UIImageView!
let swipeGestureLeft = UISwipeGestureRecognizer()
let swipeGestureRight = UISwipeGestureRecognizer()
...................... ...........
...........
}
表格视图:
///// inside cellforRowAtIndexPath
........
PagerController = cell.pageController
swipeGestureLeft = cell.swipeGestureLeft
swipeGestureRight = cell.swipeGestureRight
coverView = cell.coverView
// set gesture direction
self.swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.Left
self.swipeGestureRight.direction = UISwipeGestureRecognizerDirection.Right
// add gesture in to view
coverView.addGestureRecognizer(self.swipeGestureLeft)
coverView.addGestureRecognizer(self.swipeGestureRight)
// add gesture target
self.swipeGestureLeft.addTarget(self, action: #selector(self.handleSwipeLeft(_:)))
self.swipeGestureRight.addTarget(self, action: #selector(self.handleSwipeRight(_:)))
.
.
.
.
.
.
// functions for recognising the Gestures
func handleSwipeLeft(gesture: UISwipeGestureRecognizer){
if self.PagerController.currentPage != 2 {
print("left :\(self.PagerController.currentPage) ")
self.PagerController.currentPage += 1
imgView.image = UIImage(named: "cover\(self.PagerController.currentPage)")
}
}
//
func handleSwipeRight(gesture: UISwipeGestureRecognizer){
if self.PagerController.currentPage != 0 {
print("right :\(self.PagerController.currentPage)")
imgView.image = UIImage(named: "cover\(self.PagerController.currentPage)")
self.PagerController.currentPage -= 1
}
}
你可以看到我能够识别我的 coverView
上的手势,这是一个覆盖 ImageView 的 UIView,我也能够调用这个函数,但现在我要如何改变图像?任何线索或指导都会对我很有帮助
P.s 如果我的问题不够清楚,请告诉我,我会添加一些细节
在您的 cellForRowAt 索引中添加此代码
cell.imgView.userInteractionEnable = true
cell.imgView.addGestureRecognizer(self.swipeGestureLeft)
cell.imgView.addGestureRecognizer(self.swipeGestureRight)
希望对您有所帮助。