1 Preface
UILongPressGestureRecognizer is used to listen to and capture gesture events from somewhere where a user uses his fingers to hold down the screen for a long time.
2. code example
ZYViewController. m
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
Self. dummyButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Self. dummyButton. frame = CGRectMake (0.0f, 0.0f, 100366f, 37366f );
Self. dummyButton. center = self. view. center;
[Self. dummyButton setTitle: @ "note my location" forState: UIControlStateNormal];
[Self. view addSubview: self. dummyButton];
/* Create a gesture identification device for the first time */
Self. longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]
InitWithTarget: self
Action: @ selector (handleLongPressGestures :)];
/* The numberOfTouchesRequired attribute stores how many fingers have clicked on the screen. Therefore, make sure that the number of fingers clicked each time is the same. The default value is 0 .*/
Self. longPressGestureRecognizer. numberOfTouchesRequired = 2;
/* Maximum 100 pixels of movement allowed before the gesture is recognized */
/* The motion of up to 100 pixels is permitted by gesture recognition */
Self. longPressGestureRecognizer. allowableMovement = 100366f;
/* This parameter indicates the interval between two clicks. */
Self. longPressGestureRecognizer. minimumPressDuration = 1.0;
[Self. view addGestureRecognizer: self. longPressGestureRecognizer];
}
-(Void) handleLongPressGestures :( UILongPressGestureRecognizer *) paramSender {
If ([paramSender isEqual: self. longPressGestureRecognizer]) {
// If the index of the pressed hand is 2
If (paramSender. numberOfTouchesRequired = 2 ){
/******* Obtain the midpoint Start between two fingers ********/
CGPoint touchPoint1 = [paramSender locationOfTouch: 0 inView: paramSender. view];
CGPoint touchPoint2 = [paramSender locationOfTouch: 1 inView: paramSender. view];
CGFloat midPointX = (touchPoint1.x + touchPoint2.x)/2.0f;
CGFloat midPointY = (touchPoint1.y + touchPoint2.y)/2.0f;
CGPoint midPoint = CGPointMake (midPointX, midPointY );
/******* Get the midpoint End between two fingers ********/
Self. dummyButton. center = midPoint;
}
}
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
Self. dummyButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Self. dummyButton. frame = CGRectMake (0.0f, 0.0f, 100366f, 37366f );
Self. dummyButton. center = self. view. center;
[Self. dummyButton setTitle: @ "note my location" forState: UIControlStateNormal];
[Self. view addSubview: self. dummyButton];
/* Create a gesture identification device for the first time */
Self. longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]
InitWithTarget: self
Action: @ selector (handleLongPressGestures :)];
/* The numberOfTouchesRequired attribute stores how many fingers have clicked on the screen. Therefore, make sure that the number of fingers clicked each time is the same. The default value is 0 .*/
Self. longPressGestureRecognizer. numberOfTouchesRequired = 2;
/* Maximum 100 pixels of movement allowed before the gesture is recognized */
/* The motion of up to 100 pixels is permitted by gesture recognition */
Self. longPressGestureRecognizer. allowableMovement = 100366f;
/* This parameter indicates the interval between two clicks. */
Self. longPressGestureRecognizer. minimumPressDuration = 1.0;
[Self. view addGestureRecognizer: self. longPressGestureRecognizer];
}
-(Void) handleLongPressGestures :( UILongPressGestureRecognizer *) paramSender {
If ([paramSender isEqual: self. longPressGestureRecognizer]) {
// If the index of the pressed hand is 2
If (paramSender. numberOfTouchesRequired = 2 ){
/******* Obtain the midpoint Start between two fingers ********/
CGPoint touchPoint1 = [paramSender locationOfTouch: 0 inView: paramSender. view];
CGPoint touchPoint2 = [paramSender locationOfTouch: 1 inView: paramSender. view];
CGFloat midPointX = (touchPoint1.x + touchPoint2.x)/2.0f;
CGFloat midPointY = (touchPoint1.y + touchPoint2.y)/2.0f;
CGPoint midPoint = CGPointMake (midPointX, midPointY );
/******* Get the midpoint End between two fingers ********/
Self. dummyButton. center = midPoint;
}
}
}
Running result
Result After two fingers are long pressed (you can also use Option + long mouse Press)