How to assign tap and long tap actions to the UIView

Asked 1 years ago, Updated 1 years ago, 77 views

In the development of iOS applications, I would like to place multiple UI Views on one view and assign a tap and long tap operation (Segue) to each.

Each UIView is assigned a tag.I was able to implement the tap using the UITouch method below, but I don't know how to implement the long tap

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

    UITouch*touch=[touch anyObject];
    switch(touch.view.tag){
        case1:
            // What to do when a tag of 1 is tapped?
             appDelegate.locno=@"1";
            [self performSegueWithIdentifier:@"read" sender:self];
        case2:
             appDelegate.locno=@"2";
            [self performSegueWithIdentifier:@"read" sender:self];

        default:
            break;
    }
}

When you tap long,

appDelegate.locno=@"Tag Number";
[self performSegueWithIdentifier:@"write" sender:self];

What should I do if I want to run ?

ios objective-c iphone

2022-09-30 20:32

1 Answers

Override touchesBegan:withEvent: is basically only a special case, and it would be easier to use GestureRecognizer.

There are many ways to tap, but UITapGestureRecognizer.

Long taps can be detected by UILongPressGestureRecognizer.

See the reference for more information on how to use it.


2022-09-30 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.