在UITableViewCell的imageView中添加UIPagerControl

时间:2016-06-12 16:17:11

标签: ios uitableview uiscrollview swift2 uipagecontrol

在我的tableView的Cell中我有一个ImageView,我想插入一个UIPageControll,这样我就可以在我的ImageView中添加多个图像并像这样滚动它:

enter image description here

这是我试过的(并且卡住了):

tableViewCell:

class EventsTableViewCell: UITableViewCell {
     @IBOutlet var coverView: UIView!
    @IBOutlet var pageController: UIPageControl!
    @IBOutlet var imgView: UIImageView!

    let swipeGestureLeft = UISwipeGestureRecognizer()
    let swipeGestureRight = UISwipeGestureRecognizer()
...................... ........... 
...........
}

TableView:

 ///// 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,我也可以调用这个功能,但现在怎么改变图像?任何线索或指导对我都有帮助

如果我的问题不够清楚,请告诉我,我会添加一些细节

1 个答案:

答案 0 :(得分:3)

在你的cellForRowAt索引上添加此代码

cell.imgView.userInteractionEnable = true
cell.imgView.addGestureRecognizer(self.swipeGestureLeft)
cell.imgView.addGestureRecognizer(self.swipeGestureRight)

希望这会对你有所帮助。