I have an UIImage, and I want to give it a Random Position everytime I Click
at it.
Once you Click at the Image it should pick a random spot and so on.
I have tried and failed, so need some help!
*myDraggableImage.cs*
using System;
using MonoTouch.UIKit;
using System.Drawing;
namespace MyDragbleImageApp
{
public class myDraggableImage : UIImageView {
//Store locations for remembering the last positions, and
counting the
future ones.
PointF Location;
PointF StartLocation;
bool haveBeenTouchedOnce = false;
public myDraggableImage ( RectangleF frame ){
//Set the position of the frame with
RectangleF (Replacement of
CGRectangle)
this.Frame = frame;
StartLocation = this.Frame.Location;
}
//This event occurs when you just touch the object
public override void TouchesBegan (MonoTouch.Foundation.NSSet
touches,
MonoTouch.UIKit.UIEvent e)
{
Console.WriteLine("Touched the object");
Location = this.Frame.Location;
var touch = (UITouch) e.TouchesForView (this).AnyObject;
var bounds = Bounds;
StartLocation = touch.LocationInView(this);
this.Frame = new RectangleF(Location,bounds.Size);
}
//This event occurs when you drag it around
public override void TouchesMoved (MonoTouch.Foundation.NSSet
touches,
MonoTouch.UIKit.UIEvent e)
{
Console.WriteLine("Dragged the object");
var bounds = Bounds;
var touch = (UITouch) e.TouchesForView (this).AnyObject;
//Always refer to the StartLocation of the object that
you've been
dragging.
Location.X += touch.LocationInView(this).X -
StartLocation.X;
Location.Y += touch.LocationInView(this).Y -
StartLocation.Y;
this.Frame = new RectangleF(Location,bounds.Size);
haveBeenTouchedOnce = true;
}
public override void TouchesEnded (MonoTouch.Foundation.NSSet
touches,
MonoTouch.UIKit.UIEvent e)
{
StartLocation = Location;
}
}
};
*APPDELEGATE*
public override bool FinishedLaunching (UIApplication app, NSDictionary
options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
myDraggableImage img = new myDraggableImage(new
RectangleF(64,64,64,64));
img.UserInteractionEnabled = true;
img.Image = UIImage.FromFile("Wood.png");
img.Hidden = false;
window.AddSubview(img);
window.MakeKeyAndVisible ();
return true;
}
Thanks in advance
Regards, Nader
--
View this message in context:
http://monotouch.2284126.n4.nabble.com/How-To-change-a-UIImages-position-randomly-tp4388418p4388418.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch