Seven gestures for IOS and seven gestures for IOS

Source: Internet
Author: User

Seven gestures for IOS and seven gestures for IOS

Today, we will introduce seven types of gestures in IOS, which are often used in development. So let's talk about the seven gestures in a simple and easy-to-understand way. Let's not talk much about them. Let's look at the Code directly:

// Initialize UIimageView UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (100,100,300,300)]; imageView. image = [UIImage imageNamed: @ "12.jpg"]; // user interaction of UIImageView is disabled by default. to enable it to handle touch events, you must manually open it [imageView setUserInteractionEnabled: YES]; [self. window addSubview: imageView];

// Initialize a view (responder) to hold the gesture/* UIView * gestureView = [[UIView alloc] initWithFrame: CGRectMake (100,100,100,100)]; // place the current view in the center of the screen gestureView. center = self. window. center; gestureView. backgroundColor = [UIColor yellowColor]; [self. window addSubview: gestureView];

1. Pat gesture
// Create a pat gesture UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapAction :)];

1. Set the touch object and the number of Pat times

// Set the number of touch objects (several fingers) [tapGR setNumberOfTouchesRequired: 1]; // set the number of Pat Times [tapGR setNumberOfTapsRequired: 2];
// Add a gesture to the created view [gestureView addGestureRecognizer: tapGR];
// Callback method of the Pat gesture-(void) tapAction :( UITapGestureRecognizer *) sender {// you can obtain the current view UIImageView * imageView = (UIImageView *) sender according to the gesture. view; // obtain textfield viewWithTag. The returned value of this method is of the UIView type, but UITextField is a subclass of UIView. The parent class object cannot direct to the subclass object, therefore, you need to forcibly convert UITextField * textField = (UITextField *) [self. window viewWithTag: 1000]; // clear the keyboard, cancel the first responder [textField resignFirstResponder]; NSLog (@ "I tapped gestureView ");}
2. kneading gestures
// Create the kneading gesture UIPinchGestureRecognizer * pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinchAction :)]; pinchGR. delegate = self; // multiple gestures can be implemented in the same view
// The callback method of the pinch gesture-(void) pinchAction :( UIPinchGestureRecognizer *) sender {// float scale = sender by means of the pinch gesture. scale; // obtain the UIView * view = sender. view; // The scaling function in the 2D affine transform function to zoom in and out the view. // It is the first parameter to change the current view based on the original one. // function: the transform value of the existing view // The second parameter: the zoom ratio on the X axis // The third parameter: the zoom ratio on the Y axis // changes the original transform status of the view, regardless of the number of executions, the view is changed based on the original transform state of the view. transform = CGAffineTransformMakeScale (2, 2); view. transform = CGAffineTransformScale (view. transform, scale, scale); // after each kneading operation is completed, restore the kneading value so that it scales the sender from 100% each time. scale = 1 ;}
 
3. Rotating gesture
// Rotation gesture UIRotationGestureRecognizer * rotaGR = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotaAction :)]; rotaGR. delegate = self;
// Call back the rotation gesture method-(void) rotaAction :( UIRotationGestureRecognizer *) sender {// float rota = sender through the gesture. rotation; // obtain the UIView * view = sender of the gesture. view; // rotate the current view by using the rotation function in the 2D affine transform function. View. transform = CGAffineTransformRotate (view. transform, rota); // restore sender. rotation = 0 ;}
4. Translation gesture
// Translation gesture UIPanGestureRecognizer * panGP = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panAction :)];
// Callback method of the translation gesture-(void) panAction :( UIPanGestureRecognizer *) sender {// obtain the UIView * view = sender of the current gesture. view; // get the offset CGPoint currentPoint = [sender translationInView: view. superview]; // view the position change by using the displacement-related functions in the 2D affine transform function. transform = CGAffineTransformTranslate (view. transform, currentPoint. x, currentPoint. y); // restore // every time it starts from [sender setTranslation: CGPointZero inView: view. superview];}
5. Edge scanning gesture
// EdgePanGR = [UIScreenEdgePanGestureRecognizer alloc] initWithTarget: self action: @ selector (edgePanAction :)]; edgePanGR. edges = UIRectEdgeAll;
// Callback method of edge swipe gesture-(void) edgePanAction :( UIScreenEdgePanGestureRecognizer *) sender {NSLog (@ "I successfully triggered the screen edge gesture ");}
6. Long-pressed gesture
// 6. Long-pressed gesture * longPressPR = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPressAction :)]; longPressPR. minimumPressDuration = 1;
// 6. Callback method of long-pressed gesture- (void)longPressAction:(UILongPressGestureRecognizer *)sender{    if (sender.state == UIGestureRecognizerStateEnded) {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "" message: @ "Your initWithTitle:" delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK ",nil];    [alertView show];    }}
7. Swipe gestures
// 7. Swipe the UISwipeGestureRecognizer * swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipeAction :)];
// 7 callback method of the swipe gesture-(void) swipeAction :( UISwipeGestureRecognizer *) sender {if (sender. state = sheet) {UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: @ "Nima" delegate: self cancelButtonTitle: @ "" destructiveButtonTitle: @ "HAHAHA" comment: @ "", nil]; [actionSheet showInView: self. window] ;}}
Add a gesture to the imageView
// 3. you can add multiple gestures to a view by adding gestures to an image. However, one gesture can only be added to one view [imageView addGestureRecognizer: tapGR]; [imageView addGestureRecognizer: pinchGR: rotaGR]; [imageView progress: panGR]; [imageView addGestureRecognizer: edgePanGR]; [imageView progress: longPressPR]; [imageView addGestureRecognizer: swipeGR];

When you want to add multiple gestures to a view, you need to use the gesture proxy (Focus)

Pragma mark ---- proxy method of gesture
// Enables multiple gestures to respond simultaneously-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer handler :( UIGestureRecognizer *) else {// when the returned value is YES, when performing a gesture operation, you can also perform other gesture operations return YES ;}
 
 
 
 
 

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.