手势总结

 

================================================================

 //<1> 获取点击按钮点击按钮的字符串名称

 

    NSString *name_str  = [NSString stringWithFormat:@"ViewController%d",btn.tag];

    // <2>将字符串名称转换成类名

    Class name_class = NSClassFromString(name_str);

    

    // <3>创建类名对应的对象

    

    UIViewController *controller = [[name_class alloc] init];

    

    // <4> 设置视图控制器导航条的标题

    controller.navigationItem.title = btn.currentTitle;

    

    // <5>进行页面切换

    [self.navigationController pushViewController:controller animated:YES];

 

 

// 触摸时间  包含四个非常重要的方法 触摸开始,触摸移动,触摸结束,触摸终至

 

 

========================触摸========================================

 

//<1>触摸开始 当你手指触摸屏幕的时候就会触发刚方法

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    UILabel *label1 = (UILabel *)[self.view viewWithTag:1];

    UILabel *label2  = (UILabel *)[self.view viewWithTag:2];

    

    label1.text = @"触摸开始";

    

    // <1>获取屏幕上获取触摸手指跟数

    int fingerNUm = touches.count;

    

    // <2>获取屏幕触摸屏幕的次数

       UITouch *touch = [touches anyObject];

    int tapCount = touch.tapCount;

    

    NSString *str = [NSString stringWithFormat:@"%d fingers tap screen %d",fingerNUm ,tapCount];

    

    label2.text = str;

    

    // <3>当前视图吧支持多点触摸  必须开启多点触摸这个 权限

    self.view.multipleTouchEnabled = YES;

    

    

    

}

 

// <2>触摸移动 点击屏幕的手指发生移动的时候就会调用该方法

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UILabel *label = (UILabel *)[self.view viewWithTag:1];

    label.text = @"触摸移动";

}

 

//<3>触摸结束 点击屏幕的手指离开屏幕就会触发该方法

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    UILabel *label = (UILabel *)[self.view viewWithTag:1];

    label.text = @"触摸结束";

    

}

// <4>触摸终止   当有电话或者其他信息进入的时候迫使触发终止

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"触摸终止");

}

 

 

========================轻扫========================================

 

 

/ 所有手势都继承一个父类      叫UIGestureRecognizer

    // 所有所有手势 都能自动开始视图的多点触摸模式  无需手动设置

 

/ <1>创建轻扫手势对象

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperAction:)];

 

 

 

    // <2>设置手指跟数

    swipe.numberOfTouchesRequired = 1;

    // 默认触摸屏幕的次数为一次

    // <3>设置轻扫的方向

    // [注意] 一个轻扫手势只能设置一个轻扫方向 如果设置多个轻扫方向 最后值会将前面设置的值全部覆盖掉

    /*

     UISwipeGestureRecognizerDirectionRight = 1 << 0,

     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,

     UISwipeGestureRecognizerDirectionUp    = 1 << 2,

     UISwipeGestureRecognizerDirectionDown  = 1 << 3

     */

    swipe.direction = UISwipeGestureRecognizerDirectionRight;

 

 

  // <4>为当前视图添加手势

    [self.view addGestureRecognizer:swipe];

 

 

 是向多中轻扫结合的 

-(void)swiperAction:(UISwipeGestureRecognizer *)swipe

{

    UILabel *label = (UILabel *)[self.view viewWithTag:100];

    // 形参的类型可以是UISwipeGestureRecognizer * 也可以是UIGestureRecognizer * 类型

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {

    label.text = @"向左扫";

    }

    else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){

            label.text = @"向右轻扫";

    }

 

    

}

 

========================轻击========================================

 

 

// 点击

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAtion1:)];

    

    //<1>设置单机屏幕的手指跟数

    tap1.numberOfTouchesRequired = 1;

    

    // <2> 设置点击屏幕的次数

    tap1.numberOfTapsRequired = 1;

    [self.view addGestureRecognizer:tap1];

 

/ 设置轻击的优先级

    [tap1 requireGestureRecognizerToFail:tap2];

 

 

========================长按========================================

 

 //<1>创建长按手势的对象

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];

    

    // <2>设置长按的手指跟数

    longPress.numberOfTouchesRequired = 1;

    

    // <3>设置长按手势的最短时间

    longPress.minimumPressDuration = 1;

    [self.view addGestureRecognizer:longPress];

 

 

========================捏合===============================

 

    // <1>创建

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];

 

改变视图第一种方式

    // <1>获取添加手手势的视图

    UILabel *label = (UILabel *)pinch.view;

    // <2>使视图的宽高等比例改变

    label.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);

    

 

   改变视图第二种方式  

    UIView *view = pinch.view;

    view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);

    

    // 设置捏合的倍数

    pinch.scale = 1.0

 

 

========================旋转===============================

 

// 创建旋转对象

 

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAtion:)];

 

    // 2 旋转手势手指数默认两根

    UILabel *label = (UILabel *)[self.view viewWithTag:100];

    [label addGestureRecognizer:rotation];

 

 

// 进行旋转一种方式

label.transform = CGAffineTransformMakeRotation(rotation.rotation);

 

// 第二种方式

 view.transform = CGAffineTransformRotate(view.transform, rotation.rotation);

 

 

 

 

 

====================多中手势同时器作用==========================

 

 

遵守的协议

<UIGestureRecognizerDelegate>

协议中得方法

/ 同时起作用 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    if (gestureRecognizer.view == otherGestureRecognizer.view) {

        return YES;

    }

    return NO;

}

 

 

 

====================拖拽==========================

 

//<1>创建拖拽对象

    UIPanGestureRecognizer  * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

 

    //<2>获取拖拽路线的方向 这个点的x值为正数 表示向右拖拽 x值为负数表示向左拖拽

    CGPoint tranlition = [pan translationInView:self.view];

 

//<3>重新设置label的显示位置

    label.center = CGPointMake(oldCenter.x + tranlition.x, oldCenter.y + tranlition.y);

 

/<5>将translition的值置为0 这样就会避免在上一次拖拽的距离后继续累加

    if(pan.state == UIGestureRecognizerStateChanged)

    {

        [pan setTranslation:CGPointZero inView:self.view];

    }

}

 

 

 

====================摇晃事件=====================

 

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 

 

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 

 

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

posted @ 2015-08-23 22:24  BN笨的很想飞  阅读(205)  评论(0编辑  收藏  举报