You may have noticed that each of our ImageViews in the previous recipe had a click
event listener attached to them, calling an event handler named setChosenImage
. This event handler is going to handle setting our chosen "funny face" image to the imageViewFace
control. It will then animate all four "funny face" ImageView objects on our screen area using a number of different 2D and 3D matrix transforms.
Replace the existing setChosenImage
function, which currently stands empty, with the following source code:
//this function sets the chosen image and removes the 4 //funny faces from the screen function setChosenImage(e){ imageViewFace.image = e.source.backgroundImage; imageViewMe.visible = true; //create the first transform var transform1 = Titanium.UI.create2DMatrix(); transform1 = transform1.rotate(-180); var animation1 = Titanium.UI.createAnimation({ transform: transform1, duration: 500, curve: Titanium.UI.ANIMATION_CURVE_EASE_IN_OUT }); image1.animate(animation1); animation1.addEventListener('complete',function(e){ //remove our image selection from win1 win1.remove(image1); }); //create the second transform var transform2 = Titanium.UI.create2DMatrix(); transform2 = transform2.scale(0); var animation2 = Titanium.UI.createAnimation({ transform: transform2, duration: 500, curve: Titanium.UI.ANIMATION_CURVE_EASE_IN_OUT }); image2.animate(animation2); animation2.addEventListener('complete',function(e){ //remove our image selection from win1 win1.remove(image2); }); //create the third transform var transform3 = Titanium.UI.create2DMatrix(); transform3 = transform3.rotate(180); transform3 = transform3.scale(0); var animation3 = Titanium.UI.createAnimation({ transform: transform3, duration: 1000, curve: Titanium.UI.ANIMATION_CURVE_EASE_IN_OUT }); image3.animate(animation3); animation3.addEventListener('complete',function(e){ //remove our image selection from win1 win1.remove(image3); }); //create the fourth and final transform var transform4 = Titanium.UI.create3DMatrix(); transform4 = transform4.rotate(200,0,1,1); transform4 = transform4.scale(2); transform4 = transform4.translate(20,50,170); //the m34 property controls the perspective of the 3D view transform4.m34 = 1.0/-3000; //m34 is the position at [3,4] //in the matrix var animation4 = Titanium.UI.createAnimation({ transform: transform4, duration: 1500, curve: Titanium.UI.ANIMATION_CURVE_EASE_IN_OUT }); image4.animate(animation4); animation4.addEventListener('complete',function(e){ //remove our image selection from win1 win1.remove(image4); }); //change the status of the imageSelected variable imageSelected = true; }
Again, we are creating animations for each of the four ImageViews, but this time in a slightly different way. Instead of using the built-in animate
method, we are creating a separate animation object for each ImageView, before calling the ImageView's animate
method and passing this animation object to it. This method of creating animations allows you to have finer control over them, including the use of transforms.
Transforms have a couple of shortcuts to help you perform some of the most common animation types quickly and easily. The image1
and image2
transforms, as shown in the previous code, use the rotate
and scale
methods respectively. Scale and rotate in this case are 2D matrix transforms, meaning they only transform the object in two-dimensional space along its X-axis and Y-axis. Each of these transformation types takes a single integer parameter; for scale, it is 0-100 percent and for rotate, the number of it is 0-360 degrees.
Another advantage of using transforms for your animations is that you can easily chain them together to perform a more complex animation style. In the previous code, you can see that both a scale
and a rotate
transform are transforming the image3
component. When you run the application in the emulator or on your device, you should notice that both of these transform animations are applied to the image3
control!
Finally, the image4
control also has a transform animation applied to it, but this time we are using a 3D matrix transform instead of the 2D matrix transforms used for the other three ImageViews. These work the same way as regular 2D matrix transforms, except that you can also animate your control in 3D space, along the Z-axis.
It's important to note that animations have two event listeners: start
and complete
. These event handlers allow you to perform actions based on the beginning or ending of your animation's life cycle. As an example, you could chain animations together by using the complete
event to add a new animation or transform to an object after the previous animation has finished. In our previous example, we are using this complete
event to remove our ImageView from the Window once its animation has finished.
3.14.245.167