Skip to content

A pressable view that responds to presses by shrinking in size; alternative to a UIButton

License

Notifications You must be signed in to change notification settings

LinusGeffarth/PressableView

Repository files navigation

PressableView

Platform Version CI Status License

Table of Contents:

  1. Requirements
  2. Installation
  3. Usage
  4. Credits
  5. License

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 11.0
  • Swift 4.0
  • Xcode 9

Installation

PressableView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'PressableView'

Usage

To use a PressableView, simply drag a regular UIView into your view inside the storyboard. Then assign the class PressableView to it:

assign class

Then, you may assign a custom value for how much the view should shrink when pressed. The unit for this is %. Hence, setting it to 0.5 will make the view shrink to half of its original size. Setting it to 1 will make it not shrink at all.

The default value is 0.96.

assign value

Now, hook up any actions, you'd like it to perform. You may use any of the following:

  • primaryActionTriggered (acts like touchUpInside)
  • touchUpInside
  • touchDown
  • touchDragInside
  • touchDragOutside

You can either hook up the actions directly through the interface builder, or by assigning a target in code:

assign actions

However, you can also make use of the PressableViewDelegate.
It holds the following method:

@objc optional func pressableViewDidTrigger(_ pressableView: PressableView)

Note, though, that this delegate method will only be called when primaryActionTriggered / touchUpInside is called - so like a regular button.

To make use of the delegate, make your view controller conform to the protocol first:

class ViewController: UIViewController, PressableViewDelegate

Then, assign the view controller to the _pressableDelegate property of your PressableVeiw:

pressableView._pressableDelegate = self

Note, that you can also do this in interface builder.

Collaboration

If you find any bugs, or want to propose enhancements, please make sure to open an issue, or submit a pull request if you have made any changes you'd like for me to merge.

Credits

Linus Geffarth, linus@geffarth.com

License

PressableView is available under the MIT license. See the LICENSE file for more info.

About

A pressable view that responds to presses by shrinking in size; alternative to a UIButton

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published