【问题标题】:Play a sound when UIImageView are inside a specific area当 UIImageView 在特定区域内时播放声音
【发布时间】: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

【问题讨论】:

    标签: uiimageview imageview uipangesturerecognizer playsound


    【解决方案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!");
                             }];
    
          }
    
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      相关资源
      最近更新 更多