Skip to content

Commit

Permalink
add TapticEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldDownTown committed Apr 9, 2017
1 parent 483aacb commit 236eb15
Show file tree
Hide file tree
Showing 15 changed files with 1,111 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## mac
.DS_Store

## Build generated
build/
DerivedData/
Expand Down
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1
26 changes: 26 additions & 0 deletions Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,75 @@
# TapticEngine
This library generates vibrations by Taptic Engine.

[![License](https://img.shields.io/:license-mit-blue.svg)](https://doge.mit-license.org)
[![Language](https://img.shields.io/badge/language-swift-orange.svg?style=flat)](https://developer.apple.com/swift)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![CocoaPods compatible](https://img.shields.io/cocoapods/v/TapticEngine.svg?style=flat)](http://cocoadocs.org/docsets/TapticEngine/)
[![Downloads with CocoaPods](https://img.shields.io/cocoapods/dt/TapticEngine.svg)](http://cocoadocs.org/docsets/TapticEngine/)

## Overview
"TapticEngine" generates iOS Device vibrations.
This library wrapps on [UIImpactFeedbackGenerator](https://developer.apple.com/reference/uikit/uiimpactfeedbackgenerator), [UISelectionFeedbackGenerator](https://develo per.apple.com/reference/uikit/uiselectionfeedbackgenerator), [UINotificationFeedbackGenerator](https://developer.apple.com/reference/uikit/uinotificationfeedbackgenerator).

<img src="https://dl2.pushbulletusercontent.com/TKSfPYAu8pl5NARwARj2E3j87llwnXcs/taptic_engine.png" alt="demo_screenshot" width="375px" />

## Demo
Build Xcode project.

1. Open TapticEngine.xcodeproj.
2. Change Scheme to `TapticEngineDemo`
3. Run

## Usage

```swift
// Triggers a impact feedback between small, light user interface elements. (`UIImpactFeedbackStyle.light`)
TapticEngine.feedback(.light)

// Triggers a impact feedback between moderately sized user interface elements. (`UIImpactFeedbackStyle.medium`)
TapticEngine.feedback(.medium)

// Triggers a impact feedback between large, heavy user interface elements. (`UIImpactFeedbackStyle.heavy`)
TapticEngine.feedback(.heavy)

// Triggers a selection feedback to communicate movement through a series of discrete values.
TapticEngine.feedback(.selection)

// Triggers a notification feedback, indicating that a task has completed successfully. (`UINotificationFeedbackType.success`)
TapticEngine.feedback(.success)

// Triggers a notification feedback, indicating that a task has produced a warning. (`UINotificationFeedbackType.warning`)
TapticEngine.feedback(.warning)

// Triggers a notification feedback, indicating that a task has failed. (`UINotificationFeedbackType.error`)
TapticEngine.feedback(.error)
```

## Requirements
- Swift 3.0+
- iOS 9.0+ (**But it works on iOS 10 or later. On iOS 9, it does nothing.**)

## Installation

### Carthage
TapticEngine is available through [Carthage](https://github.com/Carthage/Carthage). To install it, simply add the following line to your Cartfile:

```
github "WorldDownTown/TapticEngine"
```

### CocoaPods
TapticEngine is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:

```ruby
pod 'TapticEngine'
```

### Manually
Download and drop `TapticEngine/Sources` folder in your project.

## Author
WorldDownTown, WorldDownTown@gmail.com

## License
TapticEngine is available under the MIT license. See the LICENSE file for more info.

88 changes: 88 additions & 0 deletions Sources/TapticEngine/TapticEngine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// TapticEngine.swift
// TapticEngine
//
// Created by Keisuke Shoji on 2017/04/09.
// Copyright © 2017年 Keisuke Shoji. All rights reserved.
//

import UIKit

/// Generates iOS Device vibrations by UIFeedbackGenerator.
public struct TapticEngine {

/// Feedback vibration types
///
/// - light: A impact feedback between small, light user interface elements.
/// - medium: A impact feedback between moderately sized user interface elements.
/// - heavy: A impact feedback between large, heavy user interface elements.
/// - selection: A selection feedback to communicate movement through a series of discrete values.
/// - success: A notification feedback, indicating that a task has completed successfully.
/// - warning: A notification feedback, indicating that a task has produced a warning.
/// - error: A notification feedback, indicating that a task has failed.
public enum FeedbackType {
case light, medium, heavy, selection, success, warning, error
}

/// Triggers feedback.
///
/// - Parameter type: Feedback style
public static func feedback(_ type: FeedbackType = .light) {
switch type {
case .light, .medium, .heavy:
feedbackImpact(type)
case .selection:
feedbackSelection()
case .success, .warning, .error:
feedbackNotification(type)
}
}

private static func feedbackImpact(_ type: FeedbackType) {
guard #available(iOS 10.0, *) else { return }

let feedbackStyle: UIImpactFeedbackStyle
switch type {
case .light:
feedbackStyle = .light
case .medium:
feedbackStyle = .medium
case .heavy:
feedbackStyle = .heavy
default:
return
}

let feedbackGenerator: UIImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: feedbackStyle)
feedbackGenerator.prepare()
feedbackGenerator.impactOccurred()
}

private static func feedbackSelection() {
guard #available(iOS 10.0, *) else { return }

let feedbackGenerator: UISelectionFeedbackGenerator = UISelectionFeedbackGenerator()
feedbackGenerator.prepare()
feedbackGenerator.selectionChanged()
}

private static func feedbackNotification(_ type: FeedbackType) {
guard #available(iOS 10.0, *) else { return }

let feedbackType: UINotificationFeedbackType
switch type {
case .success:
feedbackType = .success
case .warning:
feedbackType = .warning
case .error:
feedbackType = .error
default:
return
}

let feedbackGenerator: UINotificationFeedbackGenerator = UINotificationFeedbackGenerator()
feedbackGenerator.prepare()
feedbackGenerator.notificationOccurred(feedbackType)
}
}
18 changes: 18 additions & 0 deletions TapticEngine.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Pod::Spec.new do |s|
s.name = 'TapticEngine'
s.version = '1.0.0'
s.summary = '"TapticEngine" generates iOS Device vibrations.'
s.description = <<-DESC
"TapticEngine" generates iOS Device vibrations.
This library wrapps on [UIImpactFeedbackGenerator](https://developer.apple.com/reference/uikit/uiimpactfeedbackgenerator), [UISelectionFeedbackGenerator](https://developer.apple.com/reference/uikit/uiselectionfeedbackgenerator), [UINotificationFeedbackGenerator](https://developer.apple.com/reference/uikit/uinotificationfeedbackgenerator).
DESC
s.homepage = 'https://github.com/WorldDownTown/TapticEngine'
s.screenshots = 'https://dl2.pushbulletusercontent.com/TKSfPYAu8pl5NARwARj2E3j87llwnXcs/taptic_engine.png'
s.license = { type: 'MIT', file: 'LICENSE' }
s.author = { WorldDownTown: 'WorldDownTown@gmail.com' }
s.source = { git: 'https://github.com/WorldDownTown/TapticEngine.git', tag: s.version.to_s }
s.social_media_url = 'https://twitter.com/WorldDownTown'
s.ios.deployment_target = '9.0'
s.source_files = 'Sources/**/*.swift'
s.frameworks = ['UIKit']
end
Loading

0 comments on commit 236eb15

Please sign in to comment.