IOS development (71)

Source: Internet
Author: User

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)

 
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.