The draggable Method
- The
draggable
method allows you to convert any view or an array of views into draggable elements. - You can set basic animations when dragging or dropping elements using the
drag:
anddrop:
modifiers. - You can
apply
oranimate
the properties either globally or locally using thedrag-apply
ordrag-animate
classes. - You can also constraint any view using the
horizontal-constraint
orvertical-constraint
classes.
$.draggableAnimation.draggable('A View or an array of Views');
You can create a blank Animation
object or use an existing one to call the draggable
method to convert a view or array of views into 'draggable' objects.
When using an Animation object on an array of views, it will handle the zIndex of each draggable element.
draggable example
<Alloy>
<Window class="keep-screen-on exit-on-close-false">
<Label text="Draggable Example" class="mt-16 text-center" />
<View id="red" class="w-32 h-32 ml-4 bg-red-500 rounded-lg" />
<View id="green" class="w-32 h-32 ml-10 bg-green-500 rounded-lg" />
<View id="blue" class="w-32 h-32 ml-16 bg-blue-500 rounded-lg" />
</Window>
<Animation id="draggableAnimation" module="purgetss.ui" />
</Alloy>
$.index.open();
$.draggableAnimation.draggable([$.red, $.green, $.blue]);
* low framerate gif
drag
and drop
modifiers
- The
drag:
anddrop:
modifiers allow you to set basic animations while dragging and dropping elements. - You can set 'global' modifiers in the
Animation
object or you can set individual modifiers for each view. - Local modifiers will overwrite any global modifier.
To simplify things, we are restricting the types of animations that can be applied while dragging (or dropping).
Mainly, we are not applying any size
, scale
, or anchorPoint
transformation.
drag & drop example
<Alloy>
<Window class="keep-screen-on exit-on-close-false">
<Label text="Global Modifiers:\ndrag:duration-100 drag:opacity-50 drop:opacity-100" class="mt-16 text-center" />
<!-- No local modifiers -->
<Label id="red" class="w-32 h-32 mx-2 ml-4 text-xs text-center text-white bg-red-500 rounded-lg" text="No local modifiers" />
<!-- drag:bg-green-800 drop:bg-green-500 -->
<Label id="green" class="w-32 h-32 ml-10 text-xs text-center text-white bg-green-500 rounded-lg drag:bg-green-800 drop:bg-green-500" text="drag:bg-green-800 drop:bg-green-500" />
<!-- drag:opacity-25 -->
<Label id="blue" class="w-32 h-32 ml-16 text-xs text-center text-white bg-blue-500 rounded-lg drag:opacity-25" text="drag:opacity-25" />
<!-- Global set of modifiers -->
<Animation id="draggableAnimation" module="purgetss.ui" class="drag:duration-100 drag:opacity-50 drop:opacity-100" />
</Window>
</Alloy>
* low framerate gif
draggingType
property
To control how drag:
and drop:
modifiers are applied, you can use either the drag-animate
(default) or drag-apply class. The drag-animate
class will animate the properties, while the drag-apply
class will apply them immediately.
// Component(s): For the Animation Component
// Property(ies): draggingType
'.drag-apply': { draggingType: 'apply' }
'.drag-animate': { draggingType: 'animate' }
draggingType example
In the following example, the Animation
element sets the global dragging type to drag-apply
, but the green square overwrites it to drag-animate
.
<Alloy>
<Window class="keep-screen-on exit-on-close-false">
<Label text="draggingType Example:\ndrag-apply drag:duration-500 drag:opacity-50 drop:opacity-100" class="mt-16 text-center" />
<!-- No local modifiers, will be using the global modifiers -->
<Label id="red" class="w-32 h-32 ml-4 text-xs text-center text-white bg-red-500 rounded-lg" text="No local modifiers" />
<!-- drag-animate drag:bg-green-800 drop:bg-green-500 -->
<Label id="green" class="w-32 h-32 ml-10 text-xs text-center text-white bg-green-500 rounded-lg drag-animate drag:bg-green-800 drop:bg-green-500" text="drag-animate drag:bg-green-800 drop:bg-green-500" />
<!-- drag:opacity-25 -->
<Label id="blue" class="w-32 h-32 ml-16 text-xs text-center text-white bg-blue-500 rounded-lg drag:opacity-25" text="drag:opacity-25" />
<!-- Global set of modifiers -->
<Animation id="draggableAnimation" module="purgetss.ui" class="drag-apply drag:duration-500 drag:opacity-50 drop:opacity-100" />
</Window>
</Alloy>
* low framerate gif
bounds
modifier
- You can set boundaries in which a view can move within its parent view using the horizontal-constraint or vertical-constraint classes.
- You can set global boundaries to the Animation object and/or local boundaries to each individual child view.
- Local values will overwrite any global values.
bounds example 1
The card
view has a boundary of m-4
and a bottom boundary of mb-16
.
<Alloy>
<Window class="keep-screen-on exit-on-close-false bg-green-50">
<View class="w-screen h-screen mx-6 mt-10 mb-6 bg-green-200 rounded-lg">
<View id="card" class="w-64 h-24 mt-8 shadow-lg bounds:m-2 bounds:mb-16">
<View id="cardInside" class="w-screen bg-white border-2 border-purple-700 rounded-lg ">
<ImageView id="theImage" class="w-16 h-16 m-4 ml-4 rounded-16 prevent-default-image bg-gray-50" image="https://randomuser.me/api/portraits/women/17.jpg" />
<View class="w-screen ml-24 vertical">
<Label class="ml-0 text-sm font-bold text-gray-800" text="Ms. Jane Doe" />
<Label class="ml-0 text-xs font-bold text-gray-400" text="Website Designer" />
</View>
</View>
</View>
<Label class="mx-2 mb-2 h-12 rounded-lg text-white text-center w-screen bg-(#80000000)" text="Some Element..." />
</View>
<Animation id="draggableAnimation" module="purgetss.ui" />
</Window>
</Alloy>
$.index.open();
$.draggableAnimation.draggable($.card);
* low framerate gif
bounds example 2
In this example, the boundaries are set globally in the draggableAnimation
view.
Every card view will use these global values.
<Alloy>
<Window class="keep-screen-on exit-on-close-false bg-green-50">
<View class="w-screen h-screen mx-6 mt-10 mb-6 bg-green-200 rounded-lg">
<View id="card" class="w-64 h-24 mt-8 shadow-lg">
<View id="cardInside" class="w-screen bg-white border-2 border-purple-700 rounded-lg ">
<ImageView id="theImage" class="w-16 h-16 m-4 ml-4 rounded-16 prevent-default-image bg-gray-50" image="https://randomuser.me/api/portraits/women/17.jpg" />
<View class="w-screen ml-24 vertical">
<Label class="ml-0 text-sm font-bold text-gray-800" text="Ms. Jane Doe" />
<Label class="ml-0 text-xs font-bold text-gray-400" text="Website Designer" />
</View>
</View>
</View>
<View id="card2" class="w-64 h-24 mt-40 shadow-lg">
<View id="cardInside" class="w-screen bg-white border-2 border-purple-700 rounded-lg ">
<ImageView id="theImage" class="w-16 h-16 m-4 ml-4 rounded-16 prevent-default-image bg-gray-50" image="https://randomuser.me/api/portraits/women/21.jpg" />
<View class="w-screen ml-24 vertical">
<Label class="ml-0 text-sm font-bold text-gray-800" text="Ms. Jane Doe" />
<Label class="ml-0 text-xs font-bold text-gray-400" text="Website Designer" />
</View>
</View>
</View>
<View id="card3" class="w-64 h-24 shadow-lg mt-72">
<View id="cardInside" class="w-screen bg-white border-2 border-purple-700 rounded-lg ">
<ImageView id="theImage" class="w-16 h-16 m-4 ml-4 rounded-16 prevent-default-image bg-gray-50" image="https://randomuser.me/api/portraits/women/25.jpg" />
<View class="w-screen ml-24 vertical">
<Label class="ml-0 text-sm font-bold text-gray-800" text="Ms. Jane Doe" />
<Label class="ml-0 text-xs font-bold text-gray-400" text="Website Designer" />
</View>
</View>
</View>
<Label class="mx-2 mb-2 h-12 rounded-lg text-white text-center w-screen bg-(#80000000)" text="Some Element..." />
</View>
<Animation id="draggableAnimation" module="purgetss.ui" class="bounds:m-2 bounds:mb-16" />
</Window>
</Alloy>
$.index.open();
$.draggableAnimation.draggable([$.card, $.card2, $.card3]);
* low framerate gif
vertical
and horizontal
constraints
To add a vertical and horizontal constraint to any dragging
element, set the vertical-constraint
or horizontal-constraint
classes on the view.
// Component(s): Ti.UI.Animation
// Property(ies): A custom property to use it with the Animation module
'.horizontal-constraint': { constraint: 'horizontal' }
'.vertical-constraint': { constraint: 'vertical' }
Constraint example
In this example the card
View will move only from side to side.
<Alloy>
<Window class="keep-screen-on exit-on-close-false">
<View id="card" class="w-64 h-24 shadow-lg horizontal-constraint">
<View id="cardInside" class="w-screen bg-white border-2 border-purple-700 rounded-lg">
<ImageView id="theImage" class="w-16 h-16 m-4 ml-4 rounded-16" image="https://randomuser.me/api/portraits/women/17.jpg" />
<View class="w-screen ml-24 vertical">
<Label class="ml-0 text-sm font-bold text-gray-800" text="Ms. Jane Doe" />
<Label class="ml-0 text-xs font-bold text-gray-400" text="Website Designer" />
</View>
</View>
</View>
<Animation id="draggableAnimation" module="purgetss.ui" />
</Window>
</Alloy>
$.index.open();
$.draggableAnimation.draggable($.card);
* low framerate gif