Skip to content

Commit

Permalink
add support didTapAt
Browse files Browse the repository at this point in the history
  • Loading branch information
travel committed Oct 18, 2016
1 parent b5ae30f commit 7290115
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Carousel/CarouselViewControllerDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ class CarouselViewControllerDemo: CarouselViewController, CarouselViewController
func carousel(_ carousel: CarouselViewController, scrollFrom: Int, to: Int, progress: CGFloat) {
print("CarouselViewController scrollFrom \(scrollFrom) \(to) \(progress)")
}

func carousel(_ carousel: CarouselViewController, didTapAt cell: Int) {
print("CarouselViewController didTapAt \(cell)")
}
}
4 changes: 4 additions & 0 deletions Carousel/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,9 @@ class ViewController: UIViewController, CarouselViewDataSourse, CarouselViewDele
func carousel(_ carousel: CarouselView, scrollFrom: Int, to: Int, progress: CGFloat) {
print("CarouselView scrollFrom \(scrollFrom) \(to) \(progress)")
}

func carousel(_ carousel: CarouselView, didTapAt cell: Int) {
print("CarouselView didTapAt \(cell)")
}
}

2 changes: 1 addition & 1 deletion CarouselSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Pod::Spec.new do |s|

s.name = "CarouselSwift"
s.version = "1.0"
s.version = "1.1"
s.summary = "An reusable carousel support both Horizontal and Vertical direction, and multi page as well"

s.description = <<-DESC
Expand Down
55 changes: 55 additions & 0 deletions Sources/Carousel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ protocol CarouselDelegate:class {
func carouselDidEndDecelerating()

func carouselDidEndScrollingAnimation()


func carouselDidTap(cell:CarouselCell)
}

private func formatedInex(_ index:Int, ofCount count:Int) -> Int {
Expand Down Expand Up @@ -336,6 +339,8 @@ open class CarouselScrollView: UIScrollView {
fileprivate var autoScrollTimer:Timer?
fileprivate var autoScrollIncrease = true

fileprivate var tapGestureRecognizer:UITapGestureRecognizer?


required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Expand All @@ -353,6 +358,9 @@ open class CarouselScrollView: UIScrollView {
autoScrollTimer = nil
_reusableCells.clear()
_cells = []
if let tap = tapGestureRecognizer {
removeGestureRecognizer(tap)
}
}

fileprivate func baseInit() {
Expand All @@ -373,6 +381,11 @@ open class CarouselScrollView: UIScrollView {

NotificationCenter.default.addObserver(self, selector: #selector(self.handleNotifications(_:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleNotifications(_:)), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)

let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.handeTapGestureRecognizer(tap:)))
isUserInteractionEnabled = true
addGestureRecognizer(tap)
tapGestureRecognizer = tap
}

final func handleNotifications(_ notification:Notification) {
Expand All @@ -388,6 +401,16 @@ open class CarouselScrollView: UIScrollView {
}
}

final func handeTapGestureRecognizer(tap:UITapGestureRecognizer) {
let pos = tap.location(in: self)
for cell in _cells {
if cell.view.frame.contains(pos) {
carouselDelegate?.carouselDidTap(cell: cell)
break
}
}
}

fileprivate func cellSizeEqualToSize(_ size1: CGSize, _ size2: CGSize) -> Bool {
return abs(size1.width - size2.width) < 0.001 && abs(size1.height - size2.height) < 0.001
}
Expand Down Expand Up @@ -1296,6 +1319,15 @@ public protocol CarouselViewDataSourse:class {
@objc optional func carouselDidEndDecelerating(_ carousel:CarouselView)

@objc optional func carouselDidEndScrollingAnimation(_ carousel:CarouselView)


// MARK: select relate delegate

/// did tap cell
///
/// - parameter carousel: instance of CarouselView
/// - parameter cell: cell index
@objc optional func carousel(_ carousel:CarouselView, didTapAt cell:Int)
}


Expand Down Expand Up @@ -1361,6 +1393,13 @@ class CarouselDelegateForView:CarouselDelegate {
delgate?.carousel?(carousel, didScrollFrom: from, to: to)
}

// select
func carouselDidTap(cell: CarouselCell) {
guard let carousel = carousel else {
return
}
delgate?.carousel?(carousel, didTapAt: cell.index)
}

func carouselDidScroll() {
guard let carousel = carousel else {
Expand Down Expand Up @@ -1590,6 +1629,14 @@ public protocol CarouselViewControllerDataSourse:class {
@objc optional func carouselDidEndDecelerating(_ carousel:CarouselViewController)

@objc optional func carouselDidEndScrollingAnimation(_ carousel:CarouselViewController)

// MARK: select relate delegate

/// did tap cell
///
/// - parameter carousel: instance of CarouselViewController
/// - parameter cell: cell index
@objc optional func carousel(_ carousel:CarouselViewController, didTapAt cell:Int)
}

class CarouselCellForViewController:CarouselCell {
Expand Down Expand Up @@ -1695,6 +1742,14 @@ class CarouselDelegateForViewController:CarouselDelegate {
delgate?.carousel?(carousel, didScrollFrom: from, to: to)
}

// select
func carouselDidTap(cell: CarouselCell) {
guard let carousel = carousel else {
return
}
delgate?.carousel?(carousel, didTapAt: cell.index)
}


func carouselDidScroll() {
guard let carousel = carousel else {
Expand Down

0 comments on commit 7290115

Please sign in to comment.