>

iOS种种手势详解,手势详解

- 编辑:澳门新葡亰平台游戏 -

iOS种种手势详解,手势详解

iOS中有无数常用的手势,比方单击、双击、缩放、拖拽等等。接下来,作者为我们来所有人家介绍一下这么些手势的行使。

// 初始化一个UIimageView
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 300, 300)];
    imageView.image = [UIImage imageNamed:@"12.jpg"];

// UIImageView的用户交互是默认关闭的,要想使他可以处理触摸事件,我们得手动打开它
    [imageView setUserInteractionEnabled:YES];
    [self.window addSubview:imageView];

//初始化一个视图(响应者)来承载手势
    /*UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    //当前视图放置到屏幕中央
    gestureView.center = self.window.center;
    gestureView.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:gestureView];
单击
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; singleTap.numberOfTapsRequired = 1; [self.view addGestureRecognizer:singleTap];
1、轻拍掌势
//创建轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//给创建好的视图添加手势
   [gestureView addGestureRecognizer:tapGR];

    //设置触控对象的个数(几个手指)
    [tapGR setNumberOfTouchesRequired:1];
    //设置轻拍次数
    [tapGR setNumberOfTapsRequired:2];

//轻拍手势的回调方法
- (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");
}
双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; doubleTap.numberOfTapsRequired = 2; [self.view addGestureRecognizer:doubleTap];
2、捏合手势
//创建捏合手势
    UIPinchGestureRecognizer* pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    pinchGR.delegate = self; // 可以在同一个视图上实现多个手势

//捏合手势的回调方法
- (void)pinchAction:(UIPinchGestureRecognizer*)sender{

    //通过捏合手势的到缩放比率
    float scale = sender.scale;
    //得到该手势所作用的视图
    UIView *view = sender.view;
    //2D仿射变换函数中的缩放函数来实现视图的放大缩小

    //是在原有基础上来改变当前的视图
    //函数的第一个参数:现有的视图的transform值
    //第二个参数:x轴上的缩放比率
    //第三个参数:y轴上的缩放比率

    //是在视图最初的transform状态上改变,不管执行多少次,都是以该视图最初的transform状态为基础来改变
    view.transform = CGAffineTransformMakeScale(2, 2);
//    view.transform = CGAffineTransformScale(view.transform, scale, scale);
    //每次捏合动作完毕之后,让此捏合值复原,使得它每次都是从100%开始缩放
    sender.scale = 1;
}
左滑
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:leftSwipe];
3、旋转手势
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;
    sender.rotation = 0;
}
右滑
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:rightSwipe];
4、平移手势
//平移手势
    UIPanGestureRecognizer *panGP = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

//平移手势的回调方法
- (void)panAction:(UIPanGestureRecognizer*)sender{
    //得到当前手势所在视图
    UIView *view = sender.view;
    //得到我们在视图上移动的偏移量
    CGPoint currentPoint = [sender translationInView:view.superview];
    //通过2D仿射变换函数中与位移有关的函数实现视图位置变化
    view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x, currentPoint.y);
    //复原 // 每次都是从00点开始
    [sender setTranslation:CGPointZero inView:view.superview];
}
拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; [self.view addGestureRecognizer:pan];
5、边缘轻扫手势
//边缘轻扫手势
    UIScreenEdgePanGestureRecognizer *edgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)];
    edgePanGR.edges = UIRectEdgeAll;

//边缘轻扫手势回调方法
- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{
    NSLog(@"我成功的触发了屏幕边缘手势");
}
缩放
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; [self.view addGestureRecognizer:pinch];
6、长按手势
// ⑥长按手势
    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];

    }
}
旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; [self.view addGestureRecognizer:rotation];
澳门新葡亰平台游戏,7、轻扫手势
// ⑦轻扫手势
    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];
    }
}
长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)]; longPress.minimumPressDuration = 2.0; [self.view addGestureRecognizer:longPress];
给imageView视图增加手势
// 3.给图片添加手势  一个视图可以添加多种手势,但是一个手势,只能添加到一个视图上
    [imageView addGestureRecognizer:tapGR];
    [imageView addGestureRecognizer:pinchGR];
    [imageView addGestureRecognizer:rotaGR];
    [imageView addGestureRecognizer:panGR];
    [imageView addGestureRecognizer:edgePanGR];
    [imageView addGestureRecognizer:longPressPR];
    [imageView addGestureRecognizer:swipeGR];

// 使得多个手势可以同时响应
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    // 返回值为YES的时候,当执行一个手势的操作的时候,也可以执行其他手势的操作
    return YES;
}

本文由java编程发布,转载请注明来源:iOS种种手势详解,手势详解