GestureRecognizer

<span style="font-family:SimSun;font-size:18px;">
</span>
<span style="font-family:SimSun;font-size:18px;">
</span>
<span style="font-family:SimSun;font-size:18px;">
#import "ViewController.h"

@interface ViewController ()
{
    UIView *_canChangeView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _canChangeView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    _canChangeView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_canChangeView];
    /**        recognizer识别器
     *  使用手势
     *1.创建手势
     *2.创建绑定方法
     *3.添加到视图上
     */
    //点击手势
    UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapG:)];
    [self.view addGestureRecognizer:tapG];
    //长按
    /**
     如果长按时间过短 那么手势不会触发
     当长按手势触发后 会有两次输出  一次开始时输出 一次结束时输出
     */
    UILongPressGestureRecognizer *longG = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longG:)];
    [self.view addGestureRecognizer:longG];
    //拖动
    UIPanGestureRecognizer *panG = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panG:)];
    [_canChangeView addGestureRecognizer:panG];
    [self.view addGestureRecognizer:panG];
    //缩放
    UIPinchGestureRecognizer *pinchG = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchG:)];
    [_canChangeView addGestureRecognizer:pinchG];
    //旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationG:)];
    [_canChangeView addGestureRecognizer:rotation];
    
    //清扫
    /**
     清扫就是一个拖动手势的延伸 他能四个方向拖动
     他是一个移动一定距离内的拖动手势
     */
//    UISwipeGestureRecognizer *swipeG = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeG:)];
//    [self.view addGestureRecognizer:swipeG];
}
#pragma mark- tapGesture
- (void)tapG:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"点击手势----%s",__FUNCTION__);//__FUNCTION__执行的函数名字
    NSLog(@"%d",__LINE__);//执行代码的行数
}
#pragma mark- longPressGesture
- (void)longG:(UILongPressGestureRecognizer *)recognizer
{//  当手势比较复杂时 我们要判断手势状态
    /*
     UIGestureRecognizerStateBegan       开始
     UIGestureRecognizerStateEnded       结束
     UIGestureRecognizerStateRecognized
     UIGestureRecognizerStatePossible    有效
     UIGestureRecognizerStateFail        失败
     UIGestureRecognizerStateChanged     改变
     UIGestureRecognizerStateCancelled   取消
     */
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"长按手势开始---%s",__FUNCTION__);
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"长安手势结束---%s",__FUNCTION__);
    }
}
#pragma mark- panGesture
- (void)panG:(UIPanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan
        ||recognizer.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"拖动手势--%s",__FUNCTION__);
        /**
         *  获取当前相应视图
         */
        UIView *currentView = recognizer.view;
        if (currentView == self.view)
        {
            CGPoint point = [recognizer locationInView:currentView];
//            NSLog(@"%p",point);
            _canChangeView.center = point;
        }
        else
        {
            CGPoint point = [recognizer locationInView:currentView.superview];
            /**
             *  改变中心点
             */
            currentView.center = point;

        }
        /**
         anchor 锚点 固定点
         *  改变锚点
         */
//        CGPoint point = [recognizer locationInView:currentView.superview];
//        currentView.layer.anchorPoint = CGPointMake(-point.x/currentView.frame.size.width, -point.y/currentView.frame.size.height);
    }
}
#pragma mark- pinchG
- (void)pinchG:(UIPinchGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan
        ||recognizer.state == UIGestureRecognizerStateChanged)
    {
        UIView *currentView = recognizer.view;
        //transform 矩阵
        /**
         *  矩阵 
         *就是把一个整体图形 分割成一个一个的小方块
         *我们去修改每一个小方块的内容 从而达到整体图形的变换
         *矩阵是一个二维的 结构 我们一般只进行平移 缩放
         *在我们的程序中对矩阵添加了一个新的模块 Z轴 使图形可以旋转角度
         *  如果想让我们的图形 按照手势缩放的比例去放大或缩小 那摩直接传递recognizer.scale
         
         
         *   CGFloat sx  横向坐标的比例值
         *   CGFloat sy  纵向坐标的比例值
         *
         *
         */
        currentView.transform = CGAffineTransformScale(currentView.transform, recognizer.scale, recognizer.scale);
        /**
         *  scal 缩放比例  他的默认值是1 即缩放率是1
           他可以看做是手势生命周期中某个时间点的值
           他再开始的时候 视图的transform会记录下来 作为缩放的初始值 在手势改变过程中他都会做一次记录 所以当我们滑动过程中要将他还原到1 否则他的缩放过程会变得很迅速
         */
        recognizer.scale = 1;
    }
}
#pragma mark- rorationG
- (void)rotationG:(UIRotationGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan
        || recognizer.state == UIGestureRecognizerStateChanged)
    {
        /**
         *  recognizer.rotation 当前手势返回的旋转角度
         *  CGAffineTransformRotate也可以自定义角度
         */
        UIView *currentView = recognizer.view;
        currentView.transform = CGAffineTransformRotate(currentView.transform, recognizer.rotation);
        /**
         *  每次都要对rotation进行一次清空
         */
        recognizer.rotation = 0;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
</span>
<span style="font-family:SimSun;font-size:18px;">关于anchor 锚点 固定点 可参考<a target=_blank href="http://blog.csdn.net/yongyinmg/article/details/37927833">点击打开链接</a></span>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过代码检测Hololens 2上的双指下滑动作,可以使用以下代码: ``` using UnityEngine.XR.WSA.Input; public class GestureManager : MonoBehaviour { private GestureRecognizer gestureRecognizer; void Start() { gestureRecognizer = new GestureRecognizer(); gestureRecognizer.SetRecognizableGestures(GestureSettings.ManipulationTranslate); gestureRecognizer.ManipulationStartedEvent += OnManipulationStarted; gestureRecognizer.ManipulationUpdatedEvent += OnManipulationUpdated; gestureRecognizer.ManipulationCompletedEvent += OnManipulationCompleted; gestureRecognizer.ManipulationCanceledEvent += OnManipulationCanceled; gestureRecognizer.StartCapturingGestures(); } private void OnManipulationStarted(ManipulationStartedEventArgs args) { // 检查手势是否是双指下滑 if (args.cumulativeDelta.y < 0 && args.cumulativeDelta.x == 0) { // 双指下滑的动作 Debug.Log("Double finger swipe down detected"); } } private void OnManipulationUpdated(ManipulationUpdatedEventArgs args) { // 在手势继续进行时,可使用此函数来检测手势是否为双指下滑动作 } private void OnManipulationCompleted(ManipulationCompletedEventArgs args) { // 手势完成后的回调函数 } private void OnManipulationCanceled(ManipulationCanceledEventArgs args) { // 手势取消后的回调函数 } } ``` 在上述代码中,我们使用了`GestureRecognizer`类来捕捉手势,然后注册了`ManipulationStartedEvent`事件,该事件在手势开始时触发。在`OnManipulationStarted`回调函数中,我们检查手势是否是双指下滑动作,如果是,则输出日志信息。如果需要检测手势进行时的状态,可以使用`ManipulationUpdatedEvent`事件和`OnManipulationUpdated`回调函数。最后,我们还可以注册`ManipulationCompletedEvent`和`ManipulationCanceledEvent`事件,以便在手势完成或取消时进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值