Note that there are some explanatory texts on larger screens.

plurals
  1. PODrag out new UIImageView objects from a stationary UIImageView or button
    text
    copied!<p>I'm a beginner at iOS development and I'm working on a side project for my school. What I would like to do is have a small image where the user can drag out more images from. Essentially I have an app where I'm generating squares and I need to have a set square where the user can touch the square and drag out new squares onto a board. I have achieved this to a certain point but it's not the desired behavior yet. Currently the user has to touch the button and then release before they can interact with the new square to drag it off of the button. What i would like is to have the touch event somehow passed over to the new UIImageView object that is created so it is seamless dragging out a new square. Below is my current code.</p> <pre><code>//UIViewController #import &lt;UIKit/UIKit.h&gt; #import "Tile.h" @interface MenuViewController : UIViewController { Tile *tileObject; } @end #import "MenuViewController.h" @implementation MenuViewController - (IBAction)createTile:(UIButton *)sender { CGFloat tileX = sender.center.x; CGFloat tileY = sender.center.y; tileObject = [[Tile alloc]initWithX:tileX andY:tileY]; [self.view addSubview:tileObject]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } @end //Custom Tile Class #import &lt;UIKit/UIKit.h&gt; @interface Tile : UIImageView { CGPoint startLocation; } - (id)init; //Default Initialization - (id)initWithX:(CGFloat)x andY:(CGFloat)y; //Initialization with coordinate points from single screen tap @end #import "Tile.h" @implementation Tile //Default initialization -(id) init { self = [self initWithX:0 andY:0]; return self; } //Initialization with coordinate points from single screen tap - (id)initWithX:(CGFloat)centerX andY:(CGFloat)centerY { //Creates a UIImageView object from an image file UIImage *image = [UIImage imageNamed:@"redblock.png"]; self = [super initWithImage:image]; //Used to center the image under the single screen tap centerX -= (image.size.height/2); centerY -= (image.size.height/2); //Sets the position of the image self.frame = CGRectMake(centerX, centerY, image.size.width, image.size.height); self.userInteractionEnabled = YES; //required for interacting with UIImageViews return self; } /* Methods from Stack Overflow http://stackoverflow.com/questions/1991899/uiimage-detecting-touch-and-dragging Referenced from http://www.iphoneexamples.com/ */ - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the touch point CGPoint point = [[touches anyObject] locationInView:self]; startLocation = point; [[self superview] bringSubviewToFront:self]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Move relative to the original touch point CGPoint point = [[touches anyObject] locationInView:self]; CGRect frame = [self frame]; frame.origin.x += point.x - startLocation.x; frame.origin.y += point.y - startLocation.y; [self setFrame:frame]; } /* end of methods from Stack Overflow */ @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload