Seven gestures of iOS event handling

Source: Internet
Author: User

Today for everyone to introduce the iOS seven gestures, gestures in the development of often used, so simple and easy to understand the next, words do not say, directly look at the code:

//initialize a uiimageview uiimageview *imageview = [[uiimageview Alloc]initwithframe:cgrectmake (100, 100, 300, 300)]; Imageview.image = [uiimage imagenamed: @ "12.jpg"]; //Uiimageview user interaction is turned off by default, to enable him to handle touch events, we have to manually open it [ImageView setuserinteractionenabled:< Span class= "hljs-literal" >yes]; [self.window Addsubview:imageview]; 

/pre>
//初始化一个视图(响应者)来承载手势    /*UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];    //当前视图放置到屏幕中央    gestureView.center = self.window.center;    gestureView.backgroundColor = [UIColor yellowColor];    [self.window addSubview:gestureView];

1. Pat gesture
//创建轻拍手势    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

1. Set the Touch object, and the number of taps

    //设置触控对象的个数(几个手指)    [tapGR setNumberOfTouchesRequired:1];    //设置轻拍次数    [tapGR setNumberOfTapsRequired:2];
//给创建好的视图添加手势    [gestureView addGestureRecognizer:tapGR];
//轻拍手势的回调方法- (void)tapAction:(UITapGestureRecognizer*)sender{    //可以根据手势得到它当前所作用的视图    UIImageView *imageView = (UIImageView*)sender.view; //得到textfield viewWithTag此方法的返回值为UIView类型,但是UITextField为UIView的子类,父类对象不能直接指向子类对象,所以需要强制转换 UITextField *textField = (UITextField*)[self.window viewWithTag:1000]; //回收键盘,取消第一响应者 [textField resignFirstResponder]; NSLog(@"我轻拍了gestureView");}
2. Pinch gesture
//创建捏合手势    UIPinchGestureRecognizer* pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; pinchGR.delegate = self; // 可以在同一个视图上实现多个手势
The callback method of the pinch gesture-(void) Pinchaction: (uipinchgesturerecognizer*) sender{To zoom ratio by pinch gestureFloat scale = Sender. scale;//get the view that the gesture functions uiview *view = Sender .view; //2d the zoom function in the affine transform function to zoom in and out of the view //is on the original basis to change the current view //the second parameter: zoom ratio on the x-axis //the third parameter: the scaling ratio on the y-axis //is changed on the original transform state of the view, no matter how many times it is executed, Change View.transform = 2, 2); //after each kneading action, let this pinch value be restored so that it is scaled from 100% every time Sender.scale = 1;}               
 
3. Rotating gestures
//旋转手势    UIRotationGestureRecognizer* rotaGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaAction:)]; rotaGR.delegate = self;
//旋转手势回调方法- (void)rotaAction:(UIRotationGestureRecognizer*)sender{    //通过手势的到旋转角度    float rota = sender.rotation; //得到该手势作用的视图 UIView *view = sender.view; //通过2D仿射变换函数中的旋转函数来使得当前视图旋转。 view.transform = CGAffineTransformRotate(view.transform, rota); //复原 sender.rotation = 0;}
4. Pan gesture
//平移手势    UIPanGestureRecognizer *panGP = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
The callback method for the panning gesture-(void) Panaction: (uipangesturerecognizer*) sender{ //Get the current gesture in the view UIView *view = sender. View; //Get the offset we moved on the view cgpoint currentpoint = [Sender Translationinview:view. Superview]; //using the displacement-related functions in the 2D affine transformation function to realize the view position change. Transform = cgaffinetransformtranslate (view. Transform, Currentpoint. x, Currentpointy); // recovery//each time starting from 00 o'clock [sender settranslation:cgpointzero inview:view. Superview];}    
5. Edge Swipe gesture
//边缘轻扫手势    UIScreenEdgePanGestureRecognizer *edgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)]; edgePanGR.edges = UIRectEdgeAll;
//边缘轻扫手势回调方法- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{    NSLog(@"我成功的触发了屏幕边缘手势");}
6. Long press gesture
// ⑥长按手势    UILongPressGestureRecognizer *longPressPR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; longPressPR.minimumPressDuration = 1;
// ⑥长按手势的回调方法- (void)longPressAction:(UILongPressGestureRecognizer *)sender{ if (sender.state == UIGestureRecognizerStateEnded) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"看你麻痹" message:@"不服你咬死我" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertView show]; }}
7. Swipe gesture
 //⑦ swipe gesture uiswipegesturerecognizer *swipegr = [[uiswipegesturerecognizer alloc] Initwithtarget:self action: @selector (swipeaction:)];   
// ⑦轻扫手势的回调方法- (void)swipeAction:(UISwipeGestureRecognizer *)sender{    if (sender.state == UIGestureRecognizerStateEnded) { UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"槽尼玛" delegate:self cancelButtonTitle:@"纸张" destructiveButtonTitle:@"哈哈哈" otherButtonTitles:@"切毛毛", nil]; [actionSheet showInView:self.window]; }}
Adding gestures to the ImageView view
// 3.给图片添加手势  一个视图可以添加多种手势,但是一个手势,只能添加到一个视图上    [imageView addGestureRecognizer:tapGR];    [imageView addGestureRecognizer:pinchGR];    [imageView addGestureRecognizer:rotaGR];    [imageView addGestureRecognizer:panGR];    [imageView addGestureRecognizer:edgePanGR];    [imageView addGestureRecognizer:longPressPR];    [imageView addGestureRecognizer:swipeGR];

Use gesture proxies when you want to add multiple gestures to a view (emphasis)

pragma mark----Gesture Proxy method
// 使得多个手势可以同时响应- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    // 返回值为YES的时候,当执行一个手势的操作的时候,也可以执行其他手势的操作 return YES;}

Seven gestures of iOS event handling

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.