0

私は現在iPhoneアプリを作成していますが、アニメーションは小さな揺れに反応します。これが私のコードです。

static BOOL SJHShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
double
deltaX = fabs(last.x - current.x),
deltaY = fabs(last.y - current.y),
deltaZ = fabs(last.z - current.z);

return
(deltaX > threshold && deltaY > threshold) ||
(deltaX > threshold && deltaZ > threshold) ||
(deltaY > threshold && deltaZ > threshold);
}

- (id)initWithFrame:(CGRect)frame
{
if (self) {
    // Initialization code
}
return self;
}

-(void)awakeFromNib{
[super awakeFromNib];
[UIAccelerometer sharedAccelerometer].delegate = self;
}

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration      
*)    acceleration {
if (self.lastAction) {
    if (hasBeenShaken && SJHShaking (self.lastAction, acceleration, 0.7)) {
        hasBeenShaken = YES;
        animation.animationImages= [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"Frame01.png"],
                                    [UIImage imageNamed:@"Frame02.png"],
                                    [UIImage imageNamed:@"Frame03.png"],
                                    [UIImage imageNamed:@"Frame04.png"],
                                    [UIImage imageNamed:@"Frame05.png"],
                                    [UIImage imageNamed:@"Frame06.png"],
                                    [UIImage imageNamed:@"Frame07.png"],
                                    [UIImage imageNamed:@"Frame08.png"],
                                    [UIImage imageNamed:@"Frame09.png"],
                                    [UIImage imageNamed:@"Frame010.png"],
                                    [UIImage imageNamed:@"Frame011.png"],
                                    [UIImage imageNamed:@"Frame012.png"],
                                    [UIImage imageNamed:@"Frame013.png"],
                                    nil];

        [animation setAnimationRepeatCount:1];
        animation.animationDuration = 1;
        [animation startAnimating];
        [self performSelector:@selector(didFinishAnimating) withObject:nil     
        afterDelay:1.0];
        bottle.hidden=YES;


        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/champagne   
        animate.wav", [[NSBundle mainBundle] resourcePath]]];
        NSError *error;
        audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer2.numberOfLoops = 0;
        audioPlayer2.volume = 1;
        if (audioPlayer2 == nil);
        else
            [audioPlayer2 play];
        [audioPlayer3 stop];
        back.hidden = YES;



        } else if (hasBeenShaken && !SJHShaking(self.lastAction, acceleration, 0.2)) {
        hasBeenShaken = NO;
        }
        }

        self.lastAction = acceleration;
         }

        /*
        // Only override drawRect: if you perform custom drawing.
       // An empty implementation adversely affects performance during animation.
         - (void)drawRect:(CGRect)rect
       {
       // Drawing code
        }
        */

        -(void)dealloc{
       [UIAccelerometer sharedAccelerometer].delegate = nil;
        }

同じビューコントローラで、アニメーションの前に別のページがあります。今度は、ボタン「開始」を押す必要があります。そうすれば、シェイクできますが、「スタート」を押す前に、シェイクしても何も起こりません。どうすればいいですか?

4

1 に答える 1

0

ボタンを押したときに設定される変数を用意するだけです。次に、シェイクが発生したかどうかを判断する方法で、最初にボタンがクリックされたかどうかを確認します。

たとえば、didAccelerateメソッドでは、最初にボタンが押されたかどうかを確認し、押されていない場合は戻るだけです。

于 2012-08-08T13:08:23.650 に答える