当 UIImageView 位于特定区域内时播放声音

Play a sound when UIImageView are inside a specific area

我正在使用 UITouch 移动一些 UIImageview。 当图像在特定区域内时,我想播放声音。最简单的方法是什么? 感谢您的回答。

// ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize imageView1,imageView2;

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

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

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

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

    if ([touch view] == imageView1) { 

        imageView1.center = touchLocation;

    }

    else if ([touch view] == imageView2) {

        imageView2.center = touchLocation;

    }

}

@end

// ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    UIImageView *imageView1;

    UIImageView *imageView2;

}

@property(nonatomic,retain) IBOutlet UIImageView *imageView1;

@property(nonatomic,retain) IBOutlet UIImageView *imageView2;

@end

我是这样解决的:

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

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

[self.view bringSubviewToFront:imageView1];


if ([touch view] == imageView1) {

    imageView1.tag=0;

    if ((touchLocation.x >= 340 && touchLocation.x <= 420) && (touchLocation.y  >= 185 && touchLocation.y <= 265)) {
        NSLog(@"head");

        [UIView animateWithDuration:0.35
                              delay:0.0
                            options: UIViewAnimationOptionAllowAnimatedContent
                         animations:^{
                             imageView1.frame = CGRectMake(380.0-43, 225.0-54, 90.0, 108.0);
                             imageView1.tag=1;

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

      }

   }
}