UITapGestureRecognizer can't get to work

classic Classic list List threaded Threaded
4 messages Options
Dae
Reply | Threaded
Open this post in threaded view
|

UITapGestureRecognizer can't get to work

Dae
Hi All,

I am trying to implement trivial full screen image view in response to user clicking a thumbnail. I have written following controller to show image:
public class ImagePreviewController : UIViewController
    {
        private UIImage _image;
        private UIImageView _fullScreenImageView;
 
        public ImagePreviewController(UIImage image)
        {
            _image = image;
            _fullScreenImageView = new UIImageView(UIScreen.MainScreen.Bounds);
            _fullScreenImageView.Image = image;
            var gestureRecognizer = new UITapGestureRecognizer(thisnew Selector("ImagePreviewController.HandleTap"));
            gestureRecognizer.NumberOfTapsRequired = 1;
            gestureRecognizer.Delegate = new RecognizerDelegate();
            _fullScreenImageView.AddGestureRecognizer(gestureRecognizer);
            this.View = _fullScreenImageView;
 
        }
 
        [Export("ImagePreviewController.HandleTap")]
        public void Recognized(UITapGestureRecognizer recognizer)
        {
            _image = null;
            this.RemoveFromParentViewController();
        }
        
    }
 
And here is how I call it from parent controller:
                        _fullScreenImageController = new ImagePreviewController(uiImage);
                        this.PresentViewController(_fullScreenImageController, truenull);

As result I get nice full size image on the screen, however tapping screen has no effect and Recognized method never gets called. 
Obviously I have done something wrong, but I can't figure out what.
Please help.

P.S. RecognizerDelegate looks like this:
    public class RecognizerDelegate : UIGestureRecognizerDelegate
    {
        public override bool ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
        {
            return true;
        }
 
        public override bool ShouldBegin(UIGestureRecognizer recognizer)
        {
            return true;
        }
 
        public override bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
        {
            return true;
        }
    }

Cheers,
Andre

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: UITapGestureRecognizer can't get to work

Adam Kemp
I'm guessing that it's not valid to have a . in a Selector name.

It's easier to use gesture recognizers like this:

var gesture = new UITapGestureRecognizer();
gesture.AddTarget(() => Recognized(gesture));
view.AddGetureRecognizer(gesture);

If you use AddTarget then you don't need to use a selector. It just takes
an Action. You also don't have to set NumberOfTapsRequired if you want the
default of 1, and you usually don't need a Delegate. The only thing your
delegate is doing that is non-default is return true for
ShouldRecognizeSimultaneously. You can also do this more easily:

gesture.ShouldRecognizeSimultaneously = (thisGesture, otherGesture) =>
true;

All of the delegate methods are exposed as properties on the gesture
itself. If you assign to one of them then it creates an internal delegate
object which just forwards to whatever you assigned to the properties.
That means the properties for those methods are mutually exclusive with
the delegate property: you can use one or the other but not both. I've
found that the properties are much easier because they don't require a
full class.

If that doesn't help then ensure that your view has UserInteractionEnabled
set to true. I think UIImageViews might default to false.
--
Adam Kemp
[hidden email]
(512) 683-6058

[hidden email] wrote on 08/05/2012 09:06:44 PM:

> From: Andre Dobroskok <[hidden email]>
> To: "[hidden email]" <[hidden email]>
> Date: 08/05/2012 09:06 PM
> Subject: [MonoTouch] UITapGestureRecognizer can't get to work
> Sent by: [hidden email]
>
> Hi All,
>
> I am trying to implement trivial full screen image view in response
> to user clicking a thumbnail. I have written following controller to
> show image:
> public class ImagePreviewController : UIViewController
>     {
>         private UIImage _image;
>         private UIImageView _fullScreenImageView;
>
>         public ImagePreviewController(UIImage image)
>         {
>             _image = image;
>             _fullScreenImageView = new UIImageView(UIScreen
> .MainScreen.Bounds);
>             _fullScreenImageView.Image = image;
>             var gestureRecognizer = new UITapGestureRecognizer(this, new

> Selector("ImagePreviewController.HandleTap"));
>             gestureRecognizer.NumberOfTapsRequired = 1;
>             gestureRecognizer.Delegate = new RecognizerDelegate();
> _fullScreenImageView.AddGestureRecognizer(gestureRecognizer);
>             this.View = _fullScreenImageView;
>
>         }
>
>         [Export("ImagePreviewController.HandleTap")]
>         public void Recognized(UITapGestureRecognizer recognizer)
>         {
>             _image = null;
>             this.RemoveFromParentViewController();
>         }
>
>     }
>
> And here is how I call it from parent controller:
>                         _fullScreenImageController = new
> ImagePreviewController(uiImage);
>                         this.PresentViewController
> (_fullScreenImageController, true, null);

>
> As result I get nice full size image on the screen, however tapping
> screen has no effect and Recognized method never gets called.
> Obviously I have done something wrong, but I can't figure out what.
> Please help.
>
> P.S. RecognizerDelegate looks like this:
>     public class RecognizerDelegate : UIGestureRecognizerDelegate
>     {
>         public override bool ShouldReceiveTouch(UIGestureRecognizer
> recognizer, UITouch touch)
>         {
>             return true;
>         }
>
>         public override bool ShouldBegin(UIGestureRecognizer recognizer)
>         {
>             return true;
>         }
>
>         public override bool
ShouldRecognizeSimultaneously(UIGestureRecognizer
> gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
>         {
>             return true;
>         }
>     }

>
> Cheers,
> Andre_______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: UITapGestureRecognizer can't get to work

Nic Wise
Also check the .UserInteractionEnabled (I think, something like that)
field of the UIImageView - it's off by default, so it doesn't respond
to gestures.

Same with UILabel.

On Mon, Aug 6, 2012 at 4:05 PM, Adam Kemp <[hidden email]> wrote:

> I'm guessing that it's not valid to have a . in a Selector name.
>
> It's easier to use gesture recognizers like this:
>
> var gesture = new UITapGestureRecognizer();
> gesture.AddTarget(() => Recognized(gesture));
> view.AddGetureRecognizer(gesture);
>
> If you use AddTarget then you don't need to use a selector. It just takes
> an Action. You also don't have to set NumberOfTapsRequired if you want the
> default of 1, and you usually don't need a Delegate. The only thing your
> delegate is doing that is non-default is return true for
> ShouldRecognizeSimultaneously. You can also do this more easily:
>
> gesture.ShouldRecognizeSimultaneously = (thisGesture, otherGesture) =>
> true;
>
> All of the delegate methods are exposed as properties on the gesture
> itself. If you assign to one of them then it creates an internal delegate
> object which just forwards to whatever you assigned to the properties.
> That means the properties for those methods are mutually exclusive with
> the delegate property: you can use one or the other but not both. I've
> found that the properties are much easier because they don't require a
> full class.
>
> If that doesn't help then ensure that your view has UserInteractionEnabled
> set to true. I think UIImageViews might default to false.
> --
> Adam Kemp
> [hidden email]
> (512) 683-6058
>
> [hidden email] wrote on 08/05/2012 09:06:44 PM:
>
>> From: Andre Dobroskok <[hidden email]>
>> To: "[hidden email]" <[hidden email]>
>> Date: 08/05/2012 09:06 PM
>> Subject: [MonoTouch] UITapGestureRecognizer can't get to work
>> Sent by: [hidden email]
>>
>> Hi All,
>>
>> I am trying to implement trivial full screen image view in response
>> to user clicking a thumbnail. I have written following controller to
>> show image:
>> public class ImagePreviewController : UIViewController
>>     {
>>         private UIImage _image;
>>         private UIImageView _fullScreenImageView;
>>
>>         public ImagePreviewController(UIImage image)
>>         {
>>             _image = image;
>>             _fullScreenImageView = new UIImageView(UIScreen
>> .MainScreen.Bounds);
>>             _fullScreenImageView.Image = image;
>>             var gestureRecognizer = new UITapGestureRecognizer(this, new
>
>> Selector("ImagePreviewController.HandleTap"));
>>             gestureRecognizer.NumberOfTapsRequired = 1;
>>             gestureRecognizer.Delegate = new RecognizerDelegate();
>> _fullScreenImageView.AddGestureRecognizer(gestureRecognizer);
>>             this.View = _fullScreenImageView;
>>
>>         }
>>
>>         [Export("ImagePreviewController.HandleTap")]
>>         public void Recognized(UITapGestureRecognizer recognizer)
>>         {
>>             _image = null;
>>             this.RemoveFromParentViewController();
>>         }
>>
>>     }
>>
>> And here is how I call it from parent controller:
>>                         _fullScreenImageController = new
>> ImagePreviewController(uiImage);
>>                         this.PresentViewController
>> (_fullScreenImageController, true, null);
>
>>
>> As result I get nice full size image on the screen, however tapping
>> screen has no effect and Recognized method never gets called.
>> Obviously I have done something wrong, but I can't figure out what.
>> Please help.
>>
>> P.S. RecognizerDelegate looks like this:
>>     public class RecognizerDelegate : UIGestureRecognizerDelegate
>>     {
>>         public override bool ShouldReceiveTouch(UIGestureRecognizer
>> recognizer, UITouch touch)
>>         {
>>             return true;
>>         }
>>
>>         public override bool ShouldBegin(UIGestureRecognizer recognizer)
>>         {
>>             return true;
>>         }
>>
>>         public override bool
> ShouldRecognizeSimultaneously(UIGestureRecognizer
>> gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
>>         {
>>             return true;
>>         }
>>     }
>
>>
>> Cheers,
>> Andre_______________________________________________
>> MonoTouch mailing list
>> [hidden email]
>> http://lists.ximian.com/mailman/listinfo/monotouch
>
> _______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch



--
Nic Wise
t.  +44 7788 592 806 | @fastchicken | http://www.linkedin.com/in/nicwise
b. http://www.fastchicken.co.nz/

mobileAgent (for FreeAgent): get your accounts in your pocket.
http://goo.gl/IuBU
Trip Wallet: Keep track of your budget on the go: http://goo.gl/ePhKa
Earnest: Self-employed? Track your business expenses and income.
http://earnestapp.com
Nearest Bus: find when the next bus is coming to your stop. http://goo.gl/Vcz1p
London Bike App: Find the nearest Boris Bike, and get riding! http://goo.gl/Icp2
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Dae
Reply | Threaded
Open this post in threaded view
|

Re: UITapGestureRecognizer can't get to work

Dae
In reply to this post by Adam Kemp
Thanks Adam and Nic, it worked like a charm!
 
Cheers,
Andre

From: Adam Kemp <[hidden email]>
To: [hidden email]
Sent: Tuesday, 7 August 2012 1:05 AM
Subject: Re: [MonoTouch] UITapGestureRecognizer can't get to work

I'm guessing that it's not valid to have a . in a Selector name.

It's easier to use gesture recognizers like this:

var gesture = new UITapGestureRecognizer();
gesture.AddTarget(() => Recognized(gesture));
view.AddGetureRecognizer(gesture);

If you use AddTarget then you don't need to use a selector. It just takes
an Action. You also don't have to set NumberOfTapsRequired if you want the
default of 1, and you usually don't need a Delegate. The only thing your
delegate is doing that is non-default is return true for
ShouldRecognizeSimultaneously. You can also do this more easily:

gesture.ShouldRecognizeSimultaneously = (thisGesture, otherGesture) =>
true;

All of the delegate methods are exposed as properties on the gesture
itself. If you assign to one of them then it creates an internal delegate
object which just forwards to whatever you assigned to the properties.
That means the properties for those methods are mutually exclusive with
the delegate property: you can use one or the other but not both. I've
found that the properties are much easier because they don't require a
full class.

If that doesn't help then ensure that your view has UserInteractionEnabled
set to true. I think UIImageViews might default to false.
--
Adam Kemp
[hidden email]
(512) 683-6058

[hidden email] wrote on 08/05/2012 09:06:44 PM:

> From: Andre Dobroskok <[hidden email]>
> To: "[hidden email]" <[hidden email]>
> Date: 08/05/2012 09:06 PM
> Subject: [MonoTouch] UITapGestureRecognizer can't get to work
> Sent by: [hidden email]
>
> Hi All,
>
> I am trying to implement trivial full screen image view in response
> to user clicking a thumbnail. I have written following controller to
> show image:
> public class ImagePreviewController : UIViewController
>    {
>        private UIImage _image;
>        private UIImageView _fullScreenImageView;
>
>        public ImagePreviewController(UIImage image)
>        {
>            _image = image;
>            _fullScreenImageView = new UIImageView(UIScreen
> .MainScreen.Bounds);
>            _fullScreenImageView.Image = image;
>            var gestureRecognizer = new UITapGestureRecognizer(this, new

> Selector("ImagePreviewController.HandleTap"));
>            gestureRecognizer.NumberOfTapsRequired = 1;
>            gestureRecognizer.Delegate = new RecognizerDelegate();
> _fullScreenImageView.AddGestureRecognizer(gestureRecognizer);
>            this.View = _fullScreenImageView;
>
>        }
>
>        [Export("ImagePreviewController.HandleTap")]
>        public void Recognized(UITapGestureRecognizer recognizer)
>        {
>            _image = null;
>            this.RemoveFromParentViewController();
>        }
>
>    }
>
> And here is how I call it from parent controller:
>                        _fullScreenImageController = new
> ImagePreviewController(uiImage);
>                        this.PresentViewController
> (_fullScreenImageController, true, null);

>
> As result I get nice full size image on the screen, however tapping
> screen has no effect and Recognized method never gets called.
> Obviously I have done something wrong, but I can't figure out what.
> Please help.
>
> P.S. RecognizerDelegate looks like this:
>    public class RecognizerDelegate : UIGestureRecognizerDelegate
>    {
>        public override bool ShouldReceiveTouch(UIGestureRecognizer
> recognizer, UITouch touch)
>        {
>            return true;
>        }
>
>        public override bool ShouldBegin(UIGestureRecognizer recognizer)
>        {
>            return true;
>        }
>
>        public override bool
ShouldRecognizeSimultaneously(UIGestureRecognizer
> gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
>        {
>            return true;
>        }
>    }

>
> Cheers,
> Andre_______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch



_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch