おもちゃによくある引っ張った分だけ音が鳴るようなのを

(XcodeのiOS6 Simulatorで試しています。)

ポイント

・音はAVAudioPlayerで適当なmp3ファイルを再生

・PanGestureのStateで再生、停止を管理

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController () {

    UIView *ring;

    UIView *cord;

    UILabel *disp;

    AVAudioPlayer *player;

    NSTimer *timer;

}

@end

@implementation ViewController

#define startPoint CGPointMake(160, 100)

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:1 alpha:1.0];

    

    [self setupRing];

    

    [self setupSound];

    

    disp = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 100, 30)];

    disp.backgroundColor = [UIColor blackColor];

    disp.text = @”00.00″;

    disp.textAlignment = 1;

    disp.textColor = [UIColor whiteColor];

    [self.view addSubview:disp];

}

– (void)setupRing

{

    UIView *stopper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

    stopper.backgroundColor = [UIColor lightGrayColor];

    stopper.layer.cornerRadius = 10.0;

    stopper.layer.borderColor = [UIColor grayColor].CGColor;

    stopper.layer.borderWidth = 8.0;

    stopper.center = CGPointMake(startPoint.x, startPoint.y25);

    [self.view addSubview:stopper];

    

    cord = [[UIView alloc] initWithFrame:CGRectZero];

    cord.backgroundColor = [UIColor brownColor];

    cord.layer.shadowOpacity = 1.0;

    cord.layer.shadowOffset = CGSizeMake(0.5, 0.5);

    [self.view addSubview:cord];

    

    

    ring = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    ring.center = startPoint;

    ring.backgroundColor = [UIColor clearColor];

    ring.layer.cornerRadius = 25;

    ring.layer.borderWidth = 10.0;

    ring.layer.borderColor = [UIColor whiteColor].CGColor;

    ring.layer.shadowOpacity = 1.0;

    ring.layer.shadowOffset = CGSizeMake(2.0, 2.0);

    [self.view addSubview:ring];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

    [ring addGestureRecognizer:pan];

}

– (void)pan:(UIPanGestureRecognizer*)pgr

{

    float len = ring.center.ystartPoint.y;

    float duration = len / 50.0;

    

    if (pgr.state != UIGestureRecognizerStateEnded)

    {

        CGPoint p = [pgr locationInView:self.view];

        ring.center = CGPointMake(startPoint.x, p.y);

        cord.frame = CGRectMake(startPoint.x2, startPoint.y25, 4, len);

        disp.text = [NSString stringWithFormat:@”%2.2f”, duration];

    } else {

        [player play];

        [self startTimer];

        

        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationCurveLinear animations:^{

            ring.center = startPoint;

            cord.frame = CGRectMake(startPoint.x2, startPoint.y25, 4, 0);

        } completion:^(BOOL finished) {

            [player stop];

            [self stopTimer];

        }];

    }

}

– (void)setupSound

{

    NSString *path = [[NSBundle mainBundle] pathForResource:@”sound” ofType:@”mp3″];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

}

#define interval 0.05

– (void)startTimer

{

    timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(updateDisp) userInfo:nil repeats:YES];

}

– (void)stopTimer

{

    [timer invalidate];

    timer = nil;

    disp.text = [NSString stringWithFormat:@”00.00″];

}

– (void)updateDisp

{

    float new = [disp.text floatValue] – interval;

    disp.text = [NSString stringWithFormat:@”%2.2f”, new];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end