Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Add a reactive button target type and an initial isHighlighted stream.
Browse files Browse the repository at this point in the history
Reviewers: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, markwei

Reviewed By: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, markwei

Tags: #material_motion

Differential Revision: http://codereview.cc/D3212
  • Loading branch information
Jeff Verkoeyen committed May 12, 2017
1 parent 6c9d84d commit af75058
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions src/reactivetypes/Reactive+UIButton.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
//
// Reactive+UIButton.swift
// Pods
//
// Created by Jeff Verkoeyen on 5/11/17.
//
//
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Foundation
import UIKit

/**
A reactive button target exposes streams for certain button events.
*/
public final class ReactiveButtonTarget: NSObject {
public init(_ button: UIButton) {
super.init()

button.addTarget(self, action: #selector(didEnterEvent),
for: [.touchDown, .touchDragInside])
button.addTarget(self, action: #selector(didExitEvent),
for: [.touchUpInside, .touchUpOutside, .touchDragOutside])
}

// MARK: Streams

/**
Emits true when the button should be highlighted and false when it should not.
*/
public var didHighlight: MotionObservable<Bool> {
return MotionObservable { observer in
self.didHighlightObservers.append(observer)
return {
if let index = self.didHighlightObservers.index(where: { $0 === observer }) {
self.didHighlightObservers.remove(at: index)
}
}
}
}

func didEnterEvent(_ button: UIButton) {
didHighlightObservers.forEach { $0.next(true) }
}
func didExitEvent(_ button: UIButton) {
didHighlightObservers.forEach { $0.next(false) }
}
private var didHighlightObservers: [MotionObserver<Bool>] = []

public let metadata = Metadata("Button target")
}

0 comments on commit af75058

Please sign in to comment.