简体   繁体   中英

move multiple image view don't work, jumps back to start position

SOVLED

See post below

I use UITouch to move two imageviews. When I move around the imageview1 and release the imageview1 it stay in that position but when I start to move imageview2, imageview1 jumps back to the start position. The same thing happens if I move imageview2 first. What i want is that the imageviews stay in the release position, not to jump back to the start position. I have use the same code in another old xcode projekt and it works perfekt there.

Here is the code I am using-

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

// get touch event

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

CGPoint touchLocation = [touch locationInView:self.view];

if ([touch view] == imageView1 && imageView1.tag == 0) { // If touched view is imageView1 , then assign it its new location

    imageView1.center = touchLocation;
    [self.view bringSubviewToFront:imageView1];

}

if ([touch view] == imageView2 && imageView2.tag == 0) { // If touched view is imageView2, then assign it its new location

    imageView2.center = touchLocation;
    [self.view bringSubviewToFront:imageView2];
    [imageView2 setFrame:CGRectMake(imageView2.frame.origin.x,
                                     imageView2.frame.origin.y,68,138)];

  }

}


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

CGPoint touchLocation = [touch locationInView:self.view];
NSLog(@"%@", NSStringFromCGPoint(touchLocation));

if ([touch view] == imageView1 && imageView1.tag == 0) {

    if ((touchLocation.x >= 289 && touchLocation.x <= 381) && (touchLocation.y  >= 99 && touchLocation.y <= 135)) {
        //NSLog(@"imageView1");

        imageView1.tag=1;

        [UIView animateWithDuration:0.35
                              delay:0.0
                            options: UIViewAnimationOptionAllowAnimatedContent
                         animations:^{
                             imageView1.frame = CGRectMake(244.0, 64.0, 112, 97);



                             NSURL *musicFile;
                             musicFile = [NSURL fileURLWithPath:
                                          [[NSBundle mainBundle]
                                           pathForResource:@"knapp"
                                           ofType:@"mp3"]];
                             audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
                             [audioPlayer play];
                             audioPlayer.delegate = self;

                         }
                         completion:^(BOOL finished){
                             NSLog(@"Auto adjust!");
                         }];

    }

    else {

        [UIView animateWithDuration:0.35
                              delay:0.0
                            options: UIViewAnimationOptionAllowAnimatedContent
                         animations:^{
                             imageView1.frame = CGRectMake(50, 626, 112, 97);

                         }
                         completion:^(BOOL finished){
                             NSLog(@"imageView1!");

                             //NSLog(@"Jump back!");
                             NSLog(@"%ld", (long)imageView1.tag);
                         }];


    }

}

if ([touch view] == imageView2 && imageView2.tag == 0) {

    if ((touchLocation.x >= 426 && touchLocation.x <= 450) && (touchLocation.y  >= 210 && touchLocation.y <= 240)) {
        //NSLog(@"imageView2");

        imageView2.tag=1;

        [UIView animateWithDuration:0.35
                              delay:0.0
                            options: UIViewAnimationOptionAllowAnimatedContent
                         animations:^{
                             imageView2.frame = CGRectMake(408.0, 152.0, 68, 138);

                             NSURL *musicFile;
                             musicFile = [NSURL fileURLWithPath:
                                          [[NSBundle mainBundle]
                                           pathForResource:@"knapp"
                                           ofType:@"mp3"]];
                             audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
                             [audioPlayer play];
                             audioPlayer.delegate = self;

                         }
                         completion:^(BOOL finished){
                             NSLog(@"Auto adjust!");
                         }];

    }

    else {

        [UIView animateWithDuration:0.35
                              delay:0.0
                            options: UIViewAnimationOptionAllowAnimatedContent
                         animations:^{
                             imageView2.frame = CGRectMake(82, 519, 49, 99);

                         }
                         completion:^(BOOL finished){
                             //NSLog(@"Jump back");
                             NSLog(@"%ld", (long)imageView2.tag);
                         }];


    }

  }

}

I am thankful for any help.

I solved the problem like this

I added a hidden UIImageView that i put on the right place.

In the if statement i added:

completion:^(BOOL finished){
                             NSLog(@"Auto position!");
                             imageView1x.hidden = NO;
                             imageView1.hidden = YES;
                         }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM