UI FeedBack

Vibration

Vibration enables haptics enhance interactions and convey useful information to users through the sense of touch. Haptic effect simulates physical vibrates when an action triggered. On iOS vibrate which has three level as high, medium and low works on only on iPhone7 and above. Android Vibration Effect's time can be managed depending given value.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/page1';
import FlexLayout from 'sf-core/ui/flexlayout';
import Application from 'sf-core/application';
import Button from 'sf-core/ui/button';
import System from 'sf-core/device/system';
import Invocation from 'sf-core/util/iOS/invocation';
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
myButton: Button;
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.myButton = new Button({
text: "Vibrate",
onPress: () => {
//Android
if (System.OS == "Android") {
System.vibrate({ millisecond: 50 });
}
//iOS
if (System.OS == "iOS") {
//@ts-ignore
let feedbackAlloc = Invocation.invokeClassMethod("UIImpactFeedbackGenerator", "alloc", [], "id");
// 0: Light , 1: Medium , 2: Heavy
//@ts-ignore
let argStyle = new Invocation.Argument({
type: "NSInteger",
value: 0
});
//@ts-ignore
let feedbackGenerator = Invocation.invokeInstanceMethod(feedbackAlloc, "initWithStyle:", [argStyle], "NSObject");
//@ts-ignore
Invocation.invokeInstanceMethod(feedbackGenerator, "prepare", []);
//@ts-ignore
Invocation.invokeInstanceMethod(feedbackGenerator, "impactOccurred", []);
feedbackGenerator = undefined;
feedbackAlloc = undefined;
}
}
});
this.layout.addChild(this.myButton, "myButton", ".sf-button", {
width: 100,
height: 50,
textColor: "#000000",
backgroundColor: "#00A1F1"
});
}
JavaScript
const System = require("sf-core/device/system");
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const FlexLayout = require('sf-core/ui/flexlayout');
const Color = require('sf-core/ui/color');
const Button = require('sf-core/ui/button');
const Invocation = require('sf-core/util').Invocation;
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
var myButton = new Button({
width: 100,
height: 50,
text: "Vibrate",
textColor: Color.BLACK,
backgroundColor: Color.create("#00A1F1"),
onPress: function() {
//Android
if (System.OS == "Android") {
System.vibrate({ millisecond: 50 });
}
//iOS
if (System.OS == "iOS") {
var feedbackAlloc = Invocation.invokeClassMethod("UIImpactFeedbackGenerator","alloc",[],"id");
// 0: Light , 1: Medium , 2: Heavy
var argStyle = new Invocation.Argument({
type:"NSInteger",
value: 0
});
var feedbackGenerator = Invocation.invokeInstanceMethod(feedbackAlloc,"initWithStyle:",[argStyle],"NSObject");
Invocation.invokeInstanceMethod(feedbackGenerator,"prepare",[]);
Invocation.invokeInstanceMethod(feedbackGenerator,"impactOccurred",[]);
feedbackGenerator = undefined;
feedbackAlloc = undefined;
}
}
});
this.layout.addChild(myButton);
}
);
module.exports = Page1;

Using Vibrate Effect With Notifications

When a notification is received vibrate effect can be used all the way.

TypeScript
TypeScript
Notifications.onNotificationReceive = function(e): void {
//Android
if (System.OS === "Android") {
System.vibrate({ millisecond: 50 });
}
//iOS
if (System.OS === "iOS") {
var feedbackAlloc = Invocation.invokeClassMethod("UIImpactFeedbackGenerator", "alloc", [], "id");
// 0: Light , 1: Medium , 2: Heavy
var argStyle = new Invocation.Argument({
type: "NSInteger",
value: 0
});
var feedbackGenerator = Invocation.invokeInstanceMethod(feedbackAlloc, "initWithStyle:", [argStyle], "NSObject");
Invocation.invokeInstanceMethod(feedbackGenerator, "prepare", []);
Invocation.invokeInstanceMethod(feedbackGenerator, "impactOccurred", []);
feedbackGenerator = undefined;
feedbackAlloc = undefined;
}
}