触摸后调用touchesEnded移动并抬起手指

时间:2012-10-11 08:09:33

标签: objective-c uibutton touch

我正在尝试根据用户按下的按钮播放不同的动画和声音。按钮形状各异,我需要在用户按下按钮时发出声音,并在他抬起手指时停止按下。我觉得用touchesBegan和touchesMoved做起来会很容易。

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

所以我想我可以创建两个Integers,其中一个我将在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];
}

1 个答案:

答案 0 :(得分:0)

好的,所以发现将touchesBegan,touchesMoved和touchesEnded添加到UIButton子类不能正常工作,所以我已经切换到为每个UIControl操作添加目标。

我使用了How do you get the touchesBegan coordinates when a UIButton is triggered?

中的代码

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

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
}