简体   繁体   中英

IOS/Objective-C: Detect swipe over UIVIew and button touch up in same location of screen

Hi I have some buttons that trigger action methods.

In addition, I would like the user to be able to swipe left or right over the area with the buttons to show some different buttons

My idea was to add a UIView with a clear background color on top of the buttons so that the user could still see the buttons but could also swipe to the right or left.

However, when the view is over the buttons the buttons no longer work and when it is under the buttons, the swipe is not detected.

In addition to changing the view color to clear, I also tried adjusting the alpha however this seems to work the same way. When alpha of the UIView is zero, swipes are no longer detected and when alpha is greater than zero, the buttons no longer work.

Can anyone suggest a way to do this?

Thanks in advance for any suggestions.

Code I am using for buttons and to detect swipes seems standard. Trying to figure out a way to expose view and buttons at the same time.

Action methods to which buttons are wired:

//.h file
- (IBAction)showIncoming:(id)sender;
- (IBAction)showOutcoming:(id)sender;
/.m file
- (IBAction)showIncoming:(id)sender {
    _showIncoming=YES;
    _fetchedResultsController=nil;
    [self.tableView reloadData];
    [self updateInterface];
}
- (IBAction)showOutgoing:(id)sender {
    _showIncoming=NO;
    _fetchedResultsController=nil;
    [self.tableView reloadData];
    [self updateInterface];
}

Swipes:

//in viewWillAppear
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [rightRecognizer setNumberOfTouchesRequired:1];
    rightRecognizer.delegate = self;
    [_topView addGestureRecognizer:rightRecognizer];
    [_topView setUserInteractionEnabled:YES];
    //   [rightRecognizer release];

    //........towards left Gesture recogniser for swiping.....//
    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [leftRecognizer setNumberOfTouchesRequired:1];
    leftRecognizer.delegate = self;
    [_topView addGestureRecognizer:leftRecognizer];
    [_topView setUserInteractionEnabled:YES];

}
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //Do moving
    NSLog(@"Right Swipe performed");
       _showOld=YES;
    [self updateInterface];
}

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    // do moving
    NSLog(@"Left Swipe performed");
    _showOld=NO;
    [self updateInterface];
}

The approach I suggest is listening to a user moving a finger events and firing specified method that would check if user's finger is on your button's view. Sample code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchedViewWithTouches:touches andEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchedViewWithTouches:touches andEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchedViewWithTouches:touches andEvent:event];
}

- (void)didTouchedButton:(NSSet *)touches andEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];

    UIView *touchedView;

    for (UIView *view in self.subviews) {
        if(CGRectContainsPoint(view.frame, touchLocation)) {
            touchedView = view;
            break;
        }
    }

    if (view == self.showIncomingButton) {
        [self showIncoming:self.showIncomingButton];    
    } else if (view == self.showOutcomingButton) {
        [self howOutcoming:self.showOutcomingButton]; 
    }
}

Note that you will have to add two outlets for your buttons (if you don't have any). Also, you can get rid off (id)sender in your IBAction calls.

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