触摸与手势-----事件

触摸与手势—–事件

手势—>控件.userInteractionEnabled

触摸
UITouch
打开交互

UIView中
//找可以相应触摸事件的视图 (拦截事件)

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        UIButton *btn1 = (UIButton *)[self viewWithTag:1];
        UIButton *btn3 = (UIButton *)[self viewWithTag:3];

        //圆心和触摸点的距离
    float distance = sqrtf((point.x - btn3.center.x)*(point.x - btn3.center.x) + (point.y - btn3.center.y)*(point.y - btn3.center.y));

    //判断某个点是否在某个范围内
    if (CGRectContainsPoint(btn1.frame, point))
    {
        return btn1;
    }
    //在圆形范围内
    else if(distance <= btn3.frame.size.width/2)
    {
        return btn3;
    }

    return self;

}

//判断点是否在

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return [super pointInside:point withEvent:event];

}



UIViewController
//开始触摸

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

//触摸移动

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


}

//触摸结束

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

手势

// UITapGestureRecognizer; 点击
// UISwipeGestureRecognizer; 轻扫
// UILongPressGestureRecognizer; 长按
// UIPanGestureRecognizer; 拖动
// UIRotationGestureRecognizer; 旋转
// UIPinchGestureRecognizer; 捏合


点击手势:
1.单手指的单击
2.单手指双击
3.双手指的单击
4.双手指的双击
创建手势

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

//哪个视图添加手势添加到哪个视图

[self.view addGestureRecognizer:tap];

//点击的次数
tapGesture.numberOfTapsRequired = 1;
//手指的个数
tapGesture.numberOfTouchesRequired = 1;

协议
//手势设置代理

tap.delegate=self;

//手势是否开始

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
//添加手势对象
gestureRecognizer.view
    return  YES;
}

//是否 多个手势可以共存–或者说同时触发

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

//手势与touch哪个优先

“`
- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldReceiveTouch:(UITouch )touch{
//YES表示执行手势,NO表示手势失效
return YES;

}

“`

你可能感兴趣的:(iOS开发)