2

I'm implementing a drag and drop system in an iphone app. So far I managed to implement the draggable button and it is working fine. This is the code that I have:

First, I assign the action to the button:

[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];

then, I create the drag:

- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
    NSLog(@"moved");
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
}

However, what I want to achieve is that the button being dragged doesn't gets removed from where it is. So I thought of one solution that is to create a new button similar to the first and move the this newly created button. But I having trouble in implementing this last part. Any help? Thanks very much!

2 Answers 2

1
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event {
    NSLog(@"moved");          

    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];

    if (![self.view viewWithTag:11]) {

        NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:(UIButton*)sender];                

        UIButton *anotherButton =(UIButton*) [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];

        anotherButton.tag = 11;    


        UIImage *senderImage=[(UIButton *)sender imageForState:UIControlStateNormal];




        CGImageRef cgImage = [senderImage CGImage];


        UIImage *copyOfImage = [[UIImage alloc] initWithCGImage:cgImage];



        [anotherButton setImage:copyOfImage forState:UIControlStateNormal];

        [self.view addSubview:anotherButton];
    }

    [self.view viewWithTag:11].center = point;
}
8
  • Thanks so much for the response. It seems to be right what I need but somehow it is throwing the exception: -[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x6323500 when it reaches the line: NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:sender];. Can you see what is throwing the exception? Sep 7, 2011 at 13:55
  • u asked to move UIButton but u have used it to uiimageview Sep 7, 2011 at 14:08
  • again i changed the code above just copy and paste again and reply Sep 7, 2011 at 14:22
  • still throws the same exception. Could it be because I'm using UIButtonTypeCustom? self.button = [UIButton buttonWithType:UIButtonTypeCustom]; Sep 7, 2011 at 14:30
  • for testing purpose remove image from button and give some name to button and run and reply Sep 7, 2011 at 14:31
0

To remove the restriction you can add two events on the origin button like so:

[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragOutside];

This way the event will fire too when you exit the bounds of the current view (your button) and you'll be able to move the new button all arround the superview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.