Trouble on making touches trigger in my code

AndreasKvibyAndreasKviby SEMember
edited November 2014 in General

I have a new project and for some strange and unknown (of course) reason my touches does not trigger.

I have a button called scanButton and I have set it's frame. Then I have the below code to trigger the touch start and the start end. Any help would be appreciated.

public override void TouchesBegan (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
if (scanButton.Frame.Contains (touch.LocationInView (View)))
{
scanButton.SetTitle ("TouchesBegan", UIControlState.Highlighted);
}
}

    }

public override void TouchesEnded (NSSet touches, UIEvent evt)
    {
        base.TouchesEnded (touches, evt);
        UITouch touch = touches.AnyObject as UITouch;
        if (touch != null) {
            if (scanButton.Frame.Contains (touch.LocationInView (View)))
                scanButton.SetTitle("ended",UIControlState.Normal);
        }
        touchStartedInside = false;
    }

Posts

  • It seem that buttons cannot have a touchdown event so you have to use an ImageView instead of a button. Then it works like a charm.

  • KMullinsKMullins USMember, Xamarin Team Xamurai

    Hi @AndreasKviby‌,

    What you are more than likely looking for is the TouchUpInside method of the UIButton class. As long as they are on a view that has user interaction enabled, if the user touches and releases the button while still inside it's bounds, the event will fire.

    Kevin

  • Hey @KMullins‌ !
    No, I have tried that and that event do not offer me to trigger a tap event. If you click a button I can trigger that the button is tapped once but I need to trigger if the button is held down. I need to activate a scanner when the button is held down and deactivate the scanner when the user lifts his or hers finger from the button again.

  • KMullinsKMullins USMember, Xamarin Team Xamurai

    @AndreasKviby‌,

    Ah that's a different use case then... and for that you'll need something custom, like using a UIImageView as you have already noted.

    Cheers!

    Kevin

Sign In or Register to comment.