简体   繁体   中英

UISwipeGestureRecognizer from specific location

so I have a button and TouchUpInside event already assigned to that button. When the button is pressed, it animates a menu from bottom of the screen to about half of the screen.

But I want the user to be able to swipe UP from WITHIN the button to animate the menu and swipe DOWN from WITHIN the button to animate away.

I was able to figure out up and down swipe gestures but they work on the whole view.
so when user swipes up anywhere on the view, it animates.

How can I get the LocationOfTouch and limit the swipe's starting point from within the button?

This is what I have right now:

I delclared:

UISwipeGestureRecognizer UpSwipe;
UISwipeGestureRecognizer DownSwipe;

And then this is how I used them:

    public void setupGestures() {

        this.UpSwipe = new UISwipeGestureRecognizer () {
            Direction = UISwipeGestureRecognizerDirection.Up,
            Enabled = true
        };

        this.DownSwipe = new UISwipeGestureRecognizer () {
            Direction = UISwipeGestureRecognizerDirection.Down,
            Enabled = true
        };

        UpSwipe.AddTarget (animateRecentActivityView);
        DownSwipe.AddTarget (animateAwayRecentActivityView);

        this.View.AddGestureRecognizer (UpSwipe);
        this.View.AddGestureRecognizer (DownSwipe);
    }

How can I get the LocationOfTouch and limit the swipe's starting point from within the button?

In pure Cocoa touch you can override UIView 's method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

parameter "touches" contains a set of UITouch

call UITouch 's method

- (CGPoint)locationInView:(UIView *)view

you get the location of touch point

I was actually able to figure it out. My button was inside a view and even though I added a swipe gesture to the button, somehow it didn't work. Once I removed it from the view, adding swipeGesture to the button directly worked perfectly.

        this.UpSwipe = new UISwipeGestureRecognizer () {
            Direction = UISwipeGestureRecognizerDirection.Up,
            Enabled = true
        };

        this.DownSwipe = new UISwipeGestureRecognizer () {
            Direction = UISwipeGestureRecognizerDirection.Down,
            Enabled = true
        };

        UpSwipe.AddTarget (animateRecentActivityView);
        DownSwipe.AddTarget (animateAwayRecentActivityView);

        btnAct_HomeRecentActivity.AddGestureRecognizer (UpSwipe);
        btnAct_HomeRecentActivity.AddGestureRecognizer (DownSwipe);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM