Skip to content

Instantly share code, notes, and snippets.

@ryugoo
Created October 31, 2012 02:06
Show Gist options
  • Save ryugoo/3984385 to your computer and use it in GitHub Desktop.
Save ryugoo/3984385 to your computer and use it in GitHub Desktop.
iOS TableViewRow longpress event emulation (Titanium Mobile)
(function () {
// Base window
var base = Ti.UI.createWindow({
backgroundColor: "#FFFFFF",
title: "Long Press"
});
// TableView
var tableView = Ti.UI.createTableView({
data: [],
backgroundColor: "#FFFFFF"
});
base.add(tableView);
// HTTP client
var http = Ti.Network.createHTTPClient();
http.open("GET", "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=appcelerator");
http.onload = function (response) {
var json = JSON.parse(http.responseText);
tableView.data = json.map(function (tweet) {
var row = Ti.UI.createTableViewRow({
title: tweet.text,
backgroundColor: "#FFFFFF"
});
// Event listener
var touched = false;
row.addEventListener("click", function (clickEvt) {
touched = false;
alert("Click: " + tweet.text);
});
row.addEventListener("touchmove", function (tmoveEvt) {
touched = false;
});
row.addEventListener("touchend", function (tendEvt) {
touched = false;
});
// Event listener (iOS デバイスで longpress をエミュレートする)
// JavaScript の UI 書き換えスレッドのブロッキングでイベントキャンセルさせるのかなぁ?? (勝手な推測・謎)
row.addEventListener("touchstart", function (tstartEvt) {
touched = true;
if (touched) {
setTimeout(function () {
row.backgroundColor = "#FFFFFF"; // この行が無いと指を放したときに click イベントが発火してしまう
console.log("Long press: " + tweet.text); // alert や dialog ならば click イベントは発火しない
}, 500); // 500ms の長押し
}
});
return row;
});
};
http.onerror = function (response) {
alert("HTTP client error.");
};
http.send(null);
// Navigation group
var wrapper = Ti.UI.createWindow({
backgroundColor: "#FFFFFF",
title: "Long Press"
});
var navGroup = Ti.UI.iPhone.createNavigationGroup({
window: base,
navBarHidden: false
});
wrapper.add(navGroup);
wrapper.open();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment