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

时间:2015-01-19 07:14:04

标签: uiimageview imageview uipangesturerecognizer playsound

我正在使用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 个答案:

答案 0 :(得分:0)

我解决了这个问题:

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

      }

   }
}