Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:47:35+00:00 2026-06-12T22:47:35+00:00

I am trying to play different animations and sounds depending on which button the

  • 0

I am trying to play different animations and sounds depending on which button the user presses. The buttons are shaped variously and I need to play a sound when the user is holding the button down and stop them while he lifts up his finger. I thought it would be easy just doing with touchesBegan and touchesMoved.

However, if the user moves his finger while touching the button (even a 1 pixel movement), then there is touchesMoved method called. So, I tried some options and I am able to stop the sound once the finger moves (by calling touchesEnded by myself), however it is not the perfect solution, because the user moves the finger even without him noticing (like 1 pixel or so) and then it is very hard to play the sound continuously while he is touching the button.

So I thought I could create two Integers, which to one I will set value to in touchesBegan, then in touchesMoved setting the another and lastly comparing them, checking if the move is in the same view (button) – if it is not then it calls the touchesEnded. However it has one problem, and that is if the user holds his finger on the button, then moves it (still on the same button) and then he lifts up, the touchesEnded is not called, because he started and moved in the same view.

What should I do to call the touchesEnded method after user lifts up his finger after moving it?

Here is my code (ignore those alpha settings, playing sounds etc.):

- (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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:47:36+00:00Added an answer on June 12, 2026 at 10:47 pm

    Ok, so found out that adding touchesBegan, touchesMoved and touchesEnded to a UIButton subclass doesn’t work as expected, so I have switched to adding targets for each of the UIControl actions.

    I used the code from here How do you get the touchesBegan coordinates when a UIButton is triggered?

    Used it for each button and modified it a little – like this:

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

    and then

    -(void)aButtonTouch{
    //do something
    }
    -(void)aButtonTouchEnd {
    //do something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to play a sound when my wheel is scratched(DJ Turn Table)
I am trying to play a sound in my javascript file document.getElementById(sound).innerHTML = <embed
I am trying to play video with different browser <video Controls> <source src=Videos/2.ogv type=video/ogv>
I'm trying to make an app that can play different midi files at the
I am trying to play 6 seperate .mp3 sound files using instances of AVAudioPlayer.
I'm trying to test 8 different game-playing algorithms. These algorithms can play against each
I'm trying to show two different animations. It works fine with the first one.
I am trying to make a play/pause Button . When the Play Button is
I am trying to play a video in iPhone, which has m3u8 format. The
I am trying to play a sound on Windows XP in multi-channel (parallel) manner.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.