SOVLED
请参阅下面的帖子
我使用UITouch移动两个图像视图。当我在imageview1中移动并释放imageview1时,它会保持在该位置,但是当我开始移动imageview2时,imageview1会跳回到开始位置。如果我先移动imageview2,会发生同样的事情。 我想要的是图像视图保持在释放位置,而不是跳回到开始位置。 我在另一个旧的xcode projekt中使用相同的代码,它在那里工作。
以下是我正在使用的代码 -
- (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);
}];
}
}
}
我感谢任何帮助。
答案 0 :(得分:0)
我解决了这个问题
我添加了一个隐藏的UIImageView,我把它放在正确的位置。
在if语句中我添加了:
completion:^(BOOL finished){
NSLog(@"Auto position!");
imageView1x.hidden = NO;
imageView1.hidden = YES;
}];