How to implement a crane and hold UIImageView?

I try to bring up a warning window when I touch and hold the image for 2 seconds. Here is what I got so far:

- (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *tapAndHoldGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAndHoldGesture:)]; tapAndHoldGesture.minimumPressDuration = 0.1; tapAndHoldGesture.allowableMovement = 600; [self.view addGestureRecognizer:tapAndHoldGesture]; } - (void) handleTapAndHoldGesture:(UILongPressGestureRecognizer *)gestureRecognizer{ if (gestureRecognizer.state != UIGestureRecognizerStateEnded) { return; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gesture:" message:@"hold it" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } 

Not sure if this means anything, but the Image View is programmatically created later, and not at boot time. Thank you in advance for any help.

In addition, I looked at the following links:

Long gesture of clicking on a UICollectionViewCell

Long gesture recognition id in UIButton?

Apple Link 1

Apple Link 2

+6
source share
2 answers
 -(void)viewDidLoad { [super viewDidLoad]; [self setupGesture]; } -(void) setupGesture { UILongPressGestureRecognizer *lpHandler = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleHoldGesture:)]; lpHandler.minimumPressDuration = 1; //seconds lpHandler.delegate = self; //myUIImageViewInstance - replace for your instance/variable name [**myUIImageViewInstance** addGestureRecognizer:lpHandler]; } - (void) handleHoldGesture:(UILongPressGestureRecognizer *)gesture { if(UIGestureRecognizerStateBegan == gesture.state) { // Called on start of gesture, do work here } if(UIGestureRecognizerStateChanged == gesture.state) { // Do repeated work here (repeats continuously) while finger is down } if(UIGestureRecognizerStateEnded == gesture.state) { // Do end work here when finger is lifted } } 
+6
source

UIImageViews by default have userInteractionEnabled = NO . If you add your gesture recognizer to an instance of UIImageView , make sure you return it to YES: myImageView.userInteractionEnabled = YES

+2
source

Source: https://habr.com/ru/post/972251/


All Articles