Skip to content

add BetterSegmentedControlDelegate #162

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Pod/Classes/BetterSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

import UIKit

@objc public protocol BetterSegmentedControlDelegate {
@objc optional func tappedSegmentedControl(index: Int)
@objc optional func pannedSegmentedControl(index: Int)
}

@IBDesignable open class BetterSegmentedControl: UIControl {
private struct Constants {
static let minimumIntrinsicContentSizeHeight: CGFloat = 32.0
Expand All @@ -18,6 +23,9 @@ import UIKit
// MARK: Properties

// Public
/// BetterSegmentedControlDelegate
public var delegate: BetterSegmentedControlDelegate?

/// Indicates a no-segment-selected state.
public static let noSegment = -1

Expand Down Expand Up @@ -473,7 +481,9 @@ import UIKit
// MARK: Action handlers
@objc private func tapped(_ gestureRecognizer: UITapGestureRecognizer!) {
let location = gestureRecognizer.location(in: self)
setIndex(closestIndex(toPoint: location), shouldSendValueChangedEvent: true)
let index = closestIndex(toPoint: location)
delegate?.tappedSegmentedControl?(index: index)
setIndex(index, shouldSendValueChangedEvent: true)
}

@objc private func panned(_ gestureRecognizer: UIPanGestureRecognizer!) {
Expand All @@ -486,7 +496,9 @@ import UIKit
frame.origin.x = max(min(frame.origin.x, bounds.width - indicatorViewInset - frame.width), indicatorViewInset)
indicatorView.frame = frame
case .ended, .failed, .cancelled:
setIndex(closestIndex(toPoint: indicatorView.center), shouldSendValueChangedEvent: true)
let index = closestIndex(toPoint: indicatorView.center)
delegate?.pannedSegmentedControl?(index: index)
setIndex(index, shouldSendValueChangedEvent: true)
default: break
}
}
Expand Down