如何检测Xcode中是否触摸了图像?我查看了Apple文档,这真的令人困惑......我看到:
if (CGRectContainsPoint([imageView frame], location))
Run Code Online (Sandbox Code Playgroud)
但我的形象仍然不会动.我尝试使用touchesBegan + touchesMoved,并将图像的userInteractionIsEnabled设置为YES,但它仍然无法检测到它:(
编辑:非常感谢你们提出的好建议!最后,我真的想让它变得尽可能简单,我知道我的代码应该可以工作,所以我一直在摆弄它,经过一夜好眠,我意识到这是一个相当简单的解决方案:
在我的触摸中移动:
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGRect shapeRect = [imageView frame];
CGRect dropSquareRect = [dropSquare frame];
CGPoint touchLocation = CGPointMake(location.x, location.y);
if (CGRectContainsPoint(shapeRect, touchLocation))
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[imageView setCenter:touchLocation];
[UIView commitAnimations];
}
if (CGRectIntersectsRect(shapeRect, dropSquareRect))
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.imageView.alpha = 0;
self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y);
self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud) 我有一个相对简单的应用程序播放声音使用AVAudioPlayer
,像这样:
NSURL *file3 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ball_bounce.aiff", [[NSBundle mainBundle] resourcePath]]];
player3 = [[AVAudioPlayer alloc] initWithContentsOfURL:file3 error:nil];
[player3 prepareToPlay];
Run Code Online (Sandbox Code Playgroud)
后来被叫去玩,像这样:
[player3 play];
Run Code Online (Sandbox Code Playgroud)
但是这种声音在多个地方被调用(与弹跳球碰撞)很多次.有时,球会以足够快的速度击中两个东西,当第二次碰撞时,第一次弹跳的声音仍在播放.第二次弹跳然后不会发出声音.每次碰撞时如何让它发出声音,即使它涉及发出的声音与已播放的声音重叠?
任何帮助是极大的赞赏.