Prerna chavan
Prerna chavan

Reputation: 3249

Differentiate between two ImageViews

I have two imageViews imageView1 and imageView2. I have given gestureRecognizer to move these both images. Now the problem is when i move first imageView near second imageView only second image view is displayed. But when i put first imageView on another image the first imageView should be displayed on the second imageView which is not happening.Can any body please tell me how can the first imageView gets view on the top of another imageView.

Upvotes: 1

Views: 142

Answers (2)

Totumus Maximus
Totumus Maximus

Reputation: 7573

Why dont you use something similar like this to drag your UIImageViews?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor yellowColor];

    test1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    test1.backgroundColor = [UIColor whiteColor];
    test1.userInteractionEnabled = YES;
    test1.clipToBounds = YES;
    [self.view addSubview:test1];

    test2 = [[UIImageView alloc] initWithFrame:CGRectMake(400, 400, 100, 100)];
    test2.backgroundColor = [UIColor whiteColor];
    test2.userInteractionEnabled = YES;
    test2.clipToBounds = YES;
    [self.view addSubview:test2];
}


CGPoint startLocation;
float diffX;
float diffY;

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == test1)
    {
        startLocation = [touch locationInView:self.view];
        [self.view bringSubviewToFront:test1];
    }

    if( [touch view] == test2)
    {
        startLocation = [touch locationInView:self.view];
        [self.view bringSubviewToFront:test2];
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == test1)
    {
        CGPoint location = [touch locationInView:self.view];
        diffX = location.x - startLocation.x;
        diffY = location.y - startLocation.y;
        test1.frame = CGRectMake(test1.frame.origin.x + diffX, test1.frame.origin.y + diffY, test1.frame.size.width, test1.frame.size.height);
        startLocation = test1.frame.origin;
    }

    if( [touch view] == test2)
    {
        CGPoint location = [touch locationInView:self.view];
        diffX = location.x - startLocation.x;
        diffY = location.y - startLocation.y;
        test2.frame = CGRectMake(test2.frame.origin.x + diffX, test2.frame.origin.y + diffY, test2.frame.size.width, test2.frame.size.height);
        startLocation = test2.frame.origin;
    }

}

//---EDIT---// Added: cliptobounds in viewdidload

Upvotes: 1

Stuart
Stuart

Reputation: 37043

You can use the UIView method bringSubviewToFront:, and pass in the UIImageView that you want displayed on top.

Upvotes: 2

Related Questions