imageview - Users should be able to drag the images and place it on image view in ios -


i creating app in users can design own stage images present on there. have created buttons images, want code enables user drag , drop images(buttons) in particular image view area.

you can achieve tap gestures (e.g. tap first image view, tap on destination image view), it's not intuitive. doing proper drag-and-drop uipangesturerecognizer far more intuitive end user.

technically, you're not dragging image image view, rather you'll dragging image view itself. (an image, itself, has no visual representation without image view.) , when let go, you'll animate changing of image view frames complete illusion.

if had nsarray of imageviews, add gesture superview:

@property (nonatomic, strong) nsmutablearray *imageviews;  - (void)viewdidload { [super viewdidload];  [self createimageviewarray];  uipangesturerecognizer *gesture = [[uipangesturerecognizer alloc] initwithtarget:self                                                                          action:@selector(handlepan:)]; [self.view addgesturerecognizer:gesture]; }  - (void)createimageviewarray { self.imageviews = [nsmutablearray array]; }  - (void)handlepan:(uipangesturerecognizer *)gesture { static uiimageview *draggedimage = nil; static cgrect draggedimageoriginalframe;  cgpoint location = [gesture locationinview:gesture.view];  if (gesture.state == uigesturerecognizerstatebegan) {      draggedimage = [self determineimageforlocation:location];      if (draggedimage)     {         draggedimageoriginalframe = draggedimage.frame;         [draggedimage.superview bringsubviewtofront:draggedimage];     } } else if (gesture.state == uigesturerecognizerstatechanged && draggedimage != nil) {      cgpoint translation = [gesture translationinview:gesture.view];     cgrect frame = draggedimageoriginalframe;     frame.origin.x += translation.x;     frame.origin.y += translation.y;     draggedimage.frame = frame; } else if (draggedimage != nil && (gesture.state == uigesturerecognizerstateended ||                                  gesture.state == uigesturerecognizerstatecancelled ||                                  gesture.state == uigesturerecognizerstatefailed)) {      uiimageview *droppedover = nil;      if (gesture.state == uigesturerecognizerstateended)         droppedover = [self draggedimageview:draggedimage tolocation:location];      if (droppedover == nil)     {         [uiview animatewithduration:0.25                          animations:^{                              draggedimage.frame = draggedimageoriginalframe;                          }];     }     else     {         [droppedover.superview bringsubviewtofront:droppedover];          [uiview animatewithduration:0.25                          animations:^{                              draggedimage.frame = droppedover.frame;                              droppedover.frame = draggedimageoriginalframe;                          }];     }   }  }  - (uiimageview *)draggedimageview:(uiimageview *)draggedview tolocation:(cgpoint)location {    (uiimageview *imageview in self.imageviews)      if (cgrectcontainspoint(imageview.frame, location) && imageview != draggedview)         return imageview;   return nil; } - (uiimageview *)determineimageforlocation:(cgpoint)location {  return [self draggedimageview:nil tolocation:location]; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -