0

我正在尝试根据用户按下的按钮播放不同的动画和声音。按钮的形状各不相同,我需要在用户按住按钮时播放声音,并在他抬起手指时停止它们。我认为使用 touchesBegan 和 touchesMoved 会很容易。

但是,如果用户在触摸按钮时移动手指(即使移动了 1 个像素),则会调用 touchesMoved 方法。所以,我尝试了一些选项,一旦手指移动(通过自己调用 touchesEnded),我就可以停止声音,但这不是完美的解决方案,因为即使用户在没有注意到的情况下移动手指(比如 1 个像素左右) ) 然后在他触摸按钮时很难连续播放声音。

所以我想我可以创建两个整数,我将在 touchesBegan 中将值设置为一个,然后在 touchesMoved 中设置另一个并最后比较它们,检查移动是否在同一个视图(按钮)中 - 如果不是那么它调用 touchesEnded。然而它有一个问题,那就是如果用户将他的手指放在按钮上,然后移动它(仍然在同一个按钮上)然后他抬起,不调用 touchesEnded,因为他在同一个视图中开始和移动.

用户在移动手指后抬起手指后,我应该怎么做才能调用 touchesEnded 方法?

这是我的代码(忽略那些 alpha 设置、播放声音等):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == leftArmBtn) {

        leftArmBtn.alpha = 0;
        leftLegBtn.alpha = 0;
        mainImageView.image = [UIImage imageNamed:@"leftArmPush.jpg"];
        [[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"LEFTARM"];
        touchParent = 1;
    } else if ([touch view] == mouthBtn) {

        mouthBtn.alpha = 0;
        mainImageView.image = [UIImage imageNamed:@"mouthPush.jpg"];
        [[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"MOUTH"];
        touchParent = 2;
    } else if ([touch view] == rightArmBtn) {

        rightArmBtn.alpha = 0;
        righLegBtn.alpha = 0;
        mainImageView.image = [UIImage imageNamed:@"rightArmPush.jpg"];
        [[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"RIGHTARM"];
        touchParent = 3;
    } else if ([touch view] == leftLegBtn) {

        leftLegBtn.alpha = 0;
        mainImageView.image = [UIImage imageNamed:@"leftLegPush.jpg"];
        [[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"LEFTLEG"];
        touchParent = 4;
    } else if ([touch view] == righLegBtn) {

        righLegBtn.alpha = 0;
        mainImageView.image = [UIImage imageNamed:@"rightLegPush.jpg"];
        [[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"RIGHTLEG"];
        touchParent = 5;
    } else if ([touch view] == vakBtn) {

        vakBtn.alpha = 0;
        mainImageView.image = [UIImage imageNamed:@"vakPush.jpg"];
        [[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"VAK"];
        touchParent = 6;
    } else {
        touchParent = 0;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    if ([touch view] == leftArmBtn) {
        leftLegBtn.alpha = 1;
        leftArmBtn.alpha = 1;
        mainImageView.image = defaultImage;
        [[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"LEFTARM"];

    } else if ([touch view] == mouthBtn) {

        mouthBtn.alpha = 1;
        mainImageView.image = defaultImage;

    } else if ([touch view] == rightArmBtn) {

        rightArmBtn.alpha = 1;
        righLegBtn.alpha = 1;
        mainImageView.image = defaultImage;

        [[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"RIGHTARM"];
    } else if ([touch view] == leftLegBtn) {

        leftLegBtn.alpha = 1;
        mainImageView.image = defaultImage;

        [[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"LEFTLEG"];
    } else if ([touch view] == righLegBtn) {

        righLegBtn.alpha = 1;
        mainImageView.image = defaultImage;

        [[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"RIGHTLEG"];
    } else if ([touch view] == vakBtn) {

        vakBtn.alpha = 1;
        mainImageView.image = defaultImage;

    } else {

    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch view] == leftArmBtn) {
        movingTouch = 1;

    } else if ([touch view] == mouthBtn) {
        movingTouch = 2;

    } else if ([touch view] == rightArmBtn) {
        movingTouch = 3;

    } else if ([touch view] == leftLegBtn) {
        movingTouch = 4;

    } else if ([touch view] == righLegBtn) {
        movingTouch = 5;

    } else if ([touch view] == vakBtn) {
        movingTouch = 6;

    } else {
        movingTouch = 10;
    }

    if (touchParent != movingTouch) {
        [self touchesEnded:touches withEvent:event];
    }

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}
4

1 回答 1

0

好的,所以发现将 touchesBegan、touchesMoved 和 touchesEnded 添加到 UIButton 子类不能按预期工作,因此我已切换到为每个 UIControl 操作添加目标。

我使用了这里的代码触发 UIButton 时如何获取 touchesBegan 坐标?

将它用于每个按钮并对其进行一些修改 - 如下所示:

UIButton *aButton = [UIButton ......
[aButton addTarget:self action:@selector(aButtonTouch) forControlEvents:UIControlEventTouchDown];
[aButton addTarget:self action:@selector(aButtonTouchEnd) forControleEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

接着

-(void)aButtonTouch{
//do something
}
-(void)aButtonTouchEnd {
//do something
}
于 2012-10-11T08:59:23.807 回答