美文网首页
手势识别器

手势识别器

作者: lilinjianshu | 来源:发表于2016-03-29 22:31 被阅读25次

    - (void)viewDidLoad

    {

    [superviewDidLoad];

    // [self tapTest1];

    // [self loongpressTest];

    // [self swipeTest];

    //[self rotationTest];

    //[self pinchTest];

    [selfpanTest];

    }

    -(void)panTest

    {

    UIPanGestureRecognizer*pan=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panView:)];

    [self.iconviewaddGestureRecognizer:pan];

    }

    -(void)panView:(UIPanGestureRecognizer*)pan

    {

    switch(pan.state)

    {

    caseUIGestureRecognizerStateBegan://开始触发手势

    break;

    caseUIGestureRecognizerStateEnded://手势触发结束

    default:

    break;

    }

    //在view上挪动的距离

    CGPointpoint=[pantranslationInView:pan.view];

    // NSLog(@"%@",NSStringFromCGPoint(point));

    //找到相对于view视图的中心点

    CGPointcenter=pan.view.center;

    //相对于view视图中心点的横坐标+移动的横坐标

    center.x+=point.x;

    //相对于view视图中心点的纵坐标+移动的纵坐标

    center.y+=point.y;

    pan.view.center=center;

    //清空移动距离

    [pansetTranslation:CGPointZeroinView:pan.view];

    }

    //缩放手势

    -(void)pinchTest

    {

    UIPinchGestureRecognizer*pinch=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchView:)];

    //pinch.delegate=self;

    [self.iconviewaddGestureRecognizer:pinch];

    }

    -(void)pinchView:(UIPinchGestureRecognizer*)pinch

    {

    // NSLog(@"%f",pinch.scale);

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

    pinch.scale=1;

    }

    //旋转手势

    -(void)rotationTest

    {

    UIRotationGestureRecognizer*rota=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationView:)];

    //rota.delegate=self;

    [self.iconviewaddGestureRecognizer:rota];

    }

    -(void)rotationView:(UIRotationGestureRecognizer*)rota

    {

    // NSLog(@"----rotateview");

    //旋转角度

    NSLog(@"%f",rota.rotation);

    self.iconview.transform=CGAffineTransformRotate(self.iconview.transform, rota.rotation);

    rota.rotation=0;

    }

    //轻扫手势

    -(void)swipeTest

    {

    UISwipeGestureRecognizer*swipe=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipView)];

    //swipe.delegate=self;

    //扫描的方向

    swipe.direction=UISwipeGestureRecognizerDirectionLeft;

    [self.iconviewaddGestureRecognizer:swipe];

    }

    -(void)swipView

    {

    NSLog(@"---swipe");

    }

    //长按手势

    -(void)loongpressTest

    {

    UILongPressGestureRecognizer*longpress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longpressView)];

    //longpress.delegate=self;

    //至少长安5秒

    longpress.minimumPressDuration=5;

    //触发之前,再按下的点周围50范围内有效

    longpress.allowableMovement=50;

    [self.iconviewaddGestureRecognizer:longpress];

    }

    -(void)longpressView

    {

    NSLog(@"---longview");

    }

    //敲击手势

    -(void)tapTest1

    {

    UIGestureRecognizer*tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapview)];

    //tap.delegate=self;

    [self.iconviewaddGestureRecognizer:tap];

    }

    -(void)tapTest

    {

    //创建手势识别器对象

    UITapGestureRecognizer*tap=[[UITapGestureRecognizeralloc]init];

    //tap.delegate=self;

    //属性:连续敲两下,手势识别才成功

    //tap.numberOfTapsRequired=2;

    //属性:两个手指同时按住,手势识别才成功

    //tap.numberOfTouchesRequired=2;

    //添加手势识别器到对应的view

    [self.viewaddGestureRecognizer:tap];

    //添加监听方法(识别到了对应手势,就会调用监听方法)

    [tapaddTarget:selfaction:@selector(tapview)];

    //imageview默认不跟用户进行交互

    }

    -(void)tapview

    {

    NSLog(@"---tapview");

    }

    //当点击view时,会先调用这个方法

    -(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch

    {

    CGPointpoint=[touchlocationInView:touch.view];

    NSLog(@"%@",NSStringFromCGPoint(point));

    if(point.x<=self.iconview.frame.size.width*0.5)

    {

    returnYES;

    }

    returnNO;

    }

    //是否允许多个识别器同时有效

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

    {

    returnYES;

    }

    相关文章

      网友评论

          本文标题:手势识别器

          本文链接:https://www.haomeiwen.com/subject/dcvslttx.html