Created
August 16, 2011 19:28
-
-
Save jonalter/1149937 to your computer and use it in GitHub Desktop.
iOS: simple drag and drop example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var win = Ti.UI.createWindow({ | |
| backgroundColor: 'white', | |
| fullscreen: true, | |
| orientationModes: [1,2,3,4] | |
| }); | |
| var image = Ti.UI.createImageView({ | |
| width:50, | |
| height:100, | |
| image:"http://images.allocine.fr/r_160_214/b_1_cfd7e1/medias/nmedia/18/35/94/63/18458444.jpg", | |
| top:10, | |
| left:10, | |
| right:10, | |
| bottom:10, | |
| }); | |
| var imagecontainer = Ti.UI.createView({ | |
| width:image.width + 20, | |
| height:image.height + 20, | |
| backgroundColor:'red', | |
| top:20, | |
| left:20, | |
| }); | |
| imagecontainer.add(image); | |
| // add a transparent image so we can move image and imagecontainer together | |
| var transpImage = Ti.UI.createImageView({ | |
| backgroundColor: "transparent", | |
| movable: true, | |
| parent: imagecontainer | |
| }); | |
| imagecontainer.add(transpImage); | |
| Ti.Gesture.addEventListener('orientationchange', function(e){ | |
| Ti.API.info(e); | |
| }); | |
| win.addEventListener("touchmove", function(e){ | |
| // we check if movable is set to true so we don't grab the window | |
| if(e.source.movable){ | |
| // find the middle of the view so it will center on the point of touch | |
| var halfHeight = Math.round( e.source.parent.height/2 ); | |
| var halfWidth = Math.round( e.source.parent.width/2 ); | |
| // use globalPoint to layout where the image is | |
| // Portrait only | |
| // e.source.parent.top = e.globalPoint.y - halfHeight; | |
| // e.source.parent.left = e.globalPoint.x - halfWidth; | |
| // Support different orientations | |
| // If you move it to the side in landscape and rotate to portrait | |
| // the view will be displayed outside of the viewable area. | |
| // do some math to make it pop back in. | |
| switch (Titanium.Gesture.orientation) | |
| { | |
| case Titanium.UI.PORTRAIT: | |
| e.source.parent.top = e.globalPoint.y - halfHeight; | |
| e.source.parent.left = e.globalPoint.x - halfWidth; | |
| break; | |
| case Titanium.UI.UPSIDE_PORTRAIT: | |
| e.source.parent.top = win.height - e.globalPoint.y - halfHeight; | |
| e.source.parent.left = win.width - e.globalPoint.x - halfWidth; | |
| break; | |
| case Titanium.UI.LANDSCAPE_LEFT: | |
| e.source.parent.left = win.width - e.globalPoint.y - halfWidth; | |
| e.source.parent.top = e.globalPoint.x - halfHeight; | |
| break; | |
| case Titanium.UI.LANDSCAPE_RIGHT: | |
| e.source.parent.left = e.globalPoint.y - halfWidth; | |
| e.source.parent.top = win.height - e.globalPoint.x - halfHeight; | |
| break; | |
| } | |
| } | |
| }); | |
| win.addEventListener("singletap", function(e){ | |
| Ti.API.info("e: "+ JSON.stringify( e ) ); | |
| }); | |
| win.add(imagecontainer); | |
| win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment