Search code examples
objective-cios4uibuttonuitouch

Making draggable button in objective C


I'm trying to move to UIImageViews using UITouch. I've 2 imageViews say image1 and image2. I've these declared in .h file as UIIMageView *image1; UIImageView *image2;

NOw in the touchesBegan method, to drag an imageView, i used if condition as follows

 if([touch view] = image1){
    image1.center = location;
 } 
 else if([touch view] = image2){
    image2.center = location;
 }

where location is CGPoint.

With this code when i build n run the program, both the images overlap on each other move together. I want to move them individually.

Some help or any bug please...


Solution

  • Your if statements set [touch view] using =. If you wish to test for equality, then you must use isEqual: which is the NSObject way of saying ==:

     if([[touch view] isEqual:image1]){
        image1.center = location;
     } 
     else if([[touch view] isEqual:image2]){
        image2.center = location;
     }