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

Posted

技术标签:

【中文标题】当 UIImageView 在特定区域内时播放声音【英文标题】:Play a sound when UIImageView are inside a specific area 【发布时间】:2015-01-19 07:14:03 【问题描述】:

我正在使用 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

【问题讨论】:

【参考方案1】:

我是这样解决的:

- (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!");
                         ];

      

   

【讨论】:

以上是关于当 UIImageView 在特定区域内时播放声音的主要内容,如果未能解决你的问题,请参考以下文章

当 Player 与具有特定标签的对象碰撞时 Unity3D 播放声音

如何在特定时间段内使用 SoundPool 同时播放声音

试图在Swift的信标区域内合并声音

点击 CollectionView Cell 按钮以播放来自特定阵列的声音并从特定阵列复制电影

如何将 UIImageView 放置在子视图的特定区域中

使用 NAudio 在特定频道上播放声音