iOS手势

 
#import"ViewController.h"

@interface ViewController ()
@property (strong,nonatomic) UIView * testView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    self.testView = [[UIViewalloc]initWithFrame:CGRectMake(20,60, 335, 200)];
    self.testView.backgroundColor = [UIColorcyanColor];
    [self.viewaddSubview:self.testView];
   
    

    //开启motion事件(摇晃事件)
    [[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];

    
   
    //点击手势
    UITapGestureRecognizer * tapGestureTwo = [[UITapGestureRecognizeralloc]initWithTarget:self action:@selector(tapGestureAction:)];
    tapGestureTwo.numberOfTapsRequired =2;//点击的次数(tap点击)
    tapGestureTwo.numberOfTouchesRequired =1;//点击的手指头数量(touches触摸)
    [self.testViewaddGestureRecognizer:tapGestureTwo];//添加手势到指定的视图/控件
   
    UITapGestureRecognizer * tapGestureThree = [[UITapGestureRecognizeralloc]initWithTarget:self action:@selector(tapGestureThreeAction:)];
    tapGestureThree.numberOfTapsRequired =3;
    tapGestureThree.numberOfTouchesRequired =1;
    [self.testViewaddGestureRecognizer:tapGestureThree];
    //当tapGestureThree失败后才去执行tapGestureTwo
    [tapGestureTwo requireGestureRecognizerToFail:tapGestureThree];
    

    //捏合手势
    UIPinchGestureRecognizer * pinchGesture = [[UIPinchGestureRecognizeralloc]initWithTarget:self action:@selector(pinchGestureAction:)];
    [self.testViewaddGestureRecognizer:pinchGesture];
    

    //旋转手势
    UIRotationGestureRecognizer * rotationGesture = [[UIRotationGestureRecognizeralloc]initWithTarget:self action:@selector(rotationGestureAction:)];
    [self.testViewaddGestureRecognizer:rotationGesture];
    

    //拖动手势
    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:self action:@selector(panGestureAction:)];
    [self.testViewaddGestureRecognizer:panGesture];
    

    //长按手势
    UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizeralloc]initWithTarget:self action:@selector(longPressGestureAction:)];
    [self.testViewaddGestureRecognizer:longPressGesture];
    

    //轻扫手势
    UISwipeGestureRecognizer * swipGesture = [[UISwipeGestureRecognizeralloc]initWithTarget:self action:@selector(swipGestureAction:)];
    //这个方向是叠加的方向(direction方向)
    swipGesture.direction = UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
    //[self.testView addGestureRecognizer:swipGesture];
}


 //点击手势
- (void)tapGestureAction:(UITapGestureRecognizer *)tapG{
    NSLog(@"点击两次");
}

- (void)tapGestureThreeAction:(UITapGestureRecognizer *)tapG{
    NSLog(@"点击三次");
}


//捏合手势
- (void)pinchGestureAction:(UIPinchGestureRecognizer *)pinchG
{
    NSLog(@"pinch......");
    UIView * view = pinchG.view;//获取手势加在哪一个view上
    if (pinchG.state ==UIGestureRecognizerStateBegan || pinchG.state ==UIGestureRecognizerStateChanged) {
        //Scale比例(需要的是x y两个方向上的比例而已)
        view.transform = CGAffineTransformMakeScale(pinchG.scale, pinchG.scale);
    }
}


//旋转事件
- (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotationG
{
    NSLog(@"旋转");
    UIView * view = rotationG.view;
    if (rotationG.state ==UIGestureRecognizerStateBegan || rotationG.state ==UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformMakeRotation(rotationG.rotation);
    }
}


//拖动手势
- (void)panGestureAction:(UIPanGestureRecognizer *)panG
{
    NSLog(@"拖动");
    UIView * view = panG.view;
    if (panG.state ==UIGestureRecognizerStateBegan || panG.state ==UIGestureRecognizerStateChanged) {
        //用p记录view在其父视图上移动的距离  translation平移
        CGPoint p = [panG translationInView:view.superview];//这里的p 就是相对偏移量(*重点*)
        NSLog(@"p = %@",NSStringFromCGPoint(p));
        //是view在新的位置上显示
        [view setCenter:CGPointMake(view.center.x + p.x, view.center.y+p.y)];
        //将偏移量设置为0(因为在上一步中已经根据偏移量进行过移动了)
        [panG setTranslation:CGPointZero inView:view.superview];
    }
}


//长按手势
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPressG
{
    if (longPressG.state ==UIGestureRecognizerStateBegan) {
        NSLog(@"开始录音");
    }else if (longPressG.state == UIGestureRecognizerStateEnded){
        NSLog(@"录音结束");
    }
}


//轻扫手势
- (void)swipGestureAction:(UISwipeGestureRecognizer *)swipG
{
    NSLog(@"轻扫");
}



#pragma mark Touch事件
//touch事件
//这里执行的顺序是在各种手势之前
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

  
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //    NSLog(@"moved");
    UITouch * touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.testView];
    NSLog(@"p = %@",NSStringFromCGPoint(p));
}
//这里执行的顺序是在各种手势之前
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch * touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.testView];
    //这是一种格式化的输出方式
    NSLog(@"p = %@",NSStringFromCGPoint(p));
    //判断点击的位置是否在指定的视图内
    BOOL isC = CGRectContainsPoint(self.testView.bounds, p);
    NSLog(@"bool = %d",isC);
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   
}

//motion
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"motion began");
    self.view.backgroundColor = [UIColorredColor];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    self.view.backgroundColor = [UIColorblueColor];
}

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

//使用storyboard的单击事件
- (IBAction)tap:(id)sender {
    NSLog(@"点击一次");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值