UIPinchGestureRecognizer在水平和垂直方向上的比例分别 [英] Scale of UIPinchGestureRecognizer in horizontal and vertical directions separately

查看:117
本文介绍了UIPinchGestureRecognizer在水平和垂直方向上的比例分别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用UIPinchGestureRecognizer时,分别在水平和垂直方向检测/读取捏缩比例的最佳方法是什么?我看到了这篇文章

When using UIPinchGestureRecognizer what is the best way to detect/read the pinch scale in horizontal and vertical directions individually? I saw this post

UIPinchGestureRecognizer在不同的x和y方向上缩放视图

但是我注意到有这么多看似常规的任务来回走动我不确定这是最好的答案/方式。

but I noticed there were so many going back and forth for such a seemingly routine task that I am not sure that is the best answer/way.

如果没有完全使用UIPinchGestureRecognizer这个目的就是答案,什么是检测两个夹点比例的最佳方法不同的方向?

If not using UIPinchGestureRecognizer altogether for this purpose is the answer, what's the best way to detect pinch scale in two different directions?

推荐答案

在我的C#中,我做了以下

In my C# I do the following

    private double _firstDistance = 0;
    private int _firstScaling = 0;
    private void PinchHandler(UIPinchGestureRecognizer pinchRecognizer)
    {
        nfloat x1, y1, x2, y2 = 0;
        var t1 = pinchRecognizer.LocationOfTouch(0, _previewView);
        x1 = t1.X;
        y1 = t1.Y;
        var t2 = pinchRecognizer.LocationOfTouch(1, _previewView);
        x2 = t2.X;
        y2 = t2.Y;

        if (pinchRecognizer.State == UIGestureRecognizerState.Began)
        {
            _firstDistance = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
            _firstScaling = _task.TextTemplates[_selectedTextTemplate].FontScaling;
        }
        if (pinchRecognizer.State == UIGestureRecognizerState.Changed)
        {
            var distance = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
            var fontScaling = Convert.ToInt32((distance - _firstDistance) / _previewView.Frame.Height * 100);
            fontScaling += _firstScaling;
            _task.TextTemplates[_selectedTextTemplate].FontScaling = fontScaling;
            UpdateBitmapPreview();
        }
    }

我计算夹点时两点之间的距离开始并将这个价值保留在两个私人中。然后我根据第一个测量距离和第二个测量距离(更改)计算缩放比例(fontScaling)。
我使用自己的视图(_previewView)设置为基础(100%),但您可以使用View.Bounds.height或宽度。在我的情况下,我总是有一个方形视图,所以我的应用程序中的高度==宽度。

I calculate the distance between the two points when pinch "began" and hold that value in two privates. Then I calculate a scaling (fontScaling) based on the first measured distance and the second one (in "changed"). I use my own view (_previewView) to set as base (100%), but you can use View.Bounds.height instead or the width for that matter. in my case, I always have a square view, so height == width in my app.

这篇关于UIPinchGestureRecognizer在水平和垂直方向上的比例分别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆