Xamarin将Scale / Rotate添加到ContentView

恐惧

我有个问题。我创建了一个CustomView,可以在其中拖放视图,但现在我还想为其添加缩放/旋转功能。现在,这里是创建的OnTouchEvent,用于在其中移动视图:

public override bool OnTouchEvent(MotionEvent e)
{
    float x = e.RawX;
    float y = e.RawY;
    var dragView = Element as DraggableView.DraggableView;

    switch (e.Action)
    {
        case MotionEventActions.Down:
            if (dragView.DragMode == DragMode.Touch)
            {
                if (!touchedDown)
                {
                    if (firstTime)
                    {
                        originalX = GetX();
                        originalY = GetY();
                        firstTime = false;
                    }
                    dragView.DragStarted();
                }
                TextMoved = false;
                touchedDown = true;
                stopwatch.Start();
            }
            dX = x - this.GetX();
            dY = y - this.GetY();
            break;
        case MotionEventActions.Move:
            if (touchedDown)
            {
                if (dragView.DragDirection == DragDirectionType.All || dragView.DragDirection == DragDirectionType.Horizontal)
                {
                    SetX(x - dX);
                }

                if (dragView.DragDirection == DragDirectionType.All || dragView.DragDirection == DragDirectionType.Vertical)
                {
                    SetY(y - dY);
                }

                TextMoved = true;
            }
            break;
        case MotionEventActions.Up:
            touchedDown = false;

            if(TextMoved == true)
            {
                dragView.DragEnded();
            }
            else
            {
                MessagingCenter.Send<object, DraggableView.DraggableView>(this, "EditSelectedText", dragView);
            }

            break;
        case MotionEventActions.Cancel:
            touchedDown = false;
            break;
    }
    return base.OnTouchEvent(e);
}

但是现在我还需要“缩放/旋转”功能。问题是我已经为我的skiasharp位图创建了它,但这不是skiasharp,所以我不能使用它。

没有skiasharp,如何在OnTouchEvent中实现此功能?

少年江-MSFT

有一个关于ScaleAndRotate的xamarin-forms-samples ,但是与无关TouchEvent。我找到了一种方法,可以使用PanGestureRecognizerPinchGestureRecognizer来实现它。

创建一个ScaleAndRotateContainer ContentView,其中包含PanGestureRecognizerPinchGestureRecognizer

public class ScaleAndRotateContainer : ContentView
{
    double currentScale = 1;
    double startScale = 1;
    double xOffset = 0;
    double yOffset = 0;
    double rotateNum = 1;
    public ScaleAndRotateContainer()
    {
        var pinchGesture = new PinchGestureRecognizer ();
        pinchGesture.PinchUpdated += OnPinchUpdated;
        var panGesture = new PanGestureRecognizer();
        panGesture.PanUpdated += PanGesture_PanUpdated;
        GestureRecognizers.Add (pinchGesture);
        GestureRecognizers.Add(panGesture);
    }

    private void PanGesture_PanUpdated(object sender, PanUpdatedEventArgs e)
    {
        rotateNum++;
        this.RotateTo(rotateNum);
        this.AnchorX = 0.5;
        this.AnchorY = 0.5;
    }

    void OnPinchUpdated (object sender, PinchGestureUpdatedEventArgs e)
    {
        if (e.Status == GestureStatus.Started) {
            // Store the current scale factor applied to the wrapped user interface element,
            // and zero the components for the center point of the translate transform.
            startScale = Content.Scale;
            Content.AnchorX = 0;
            Content.AnchorY = 0;
        }
        if (e.Status == GestureStatus.Running) {
            // Calculate the scale factor to be applied.
            currentScale += (e.Scale - 1) * startScale;
            currentScale = Math.Max (1, currentScale);

            // The ScaleOrigin is in relative coordinates to the wrapped user interface element,
            // so get the X pixel coordinate.
            double renderedX = Content.X + xOffset;
            double deltaX = renderedX / Width;
            double deltaWidth = Width / (Content.Width * startScale);
            double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;

            // The ScaleOrigin is in relative coordinates to the wrapped user interface element,
            // so get the Y pixel coordinate.
            double renderedY = Content.Y + yOffset;
            double deltaY = renderedY / Height;
            double deltaHeight = Height / (Content.Height * startScale);
            double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;

            // Calculate the transformed element pixel coordinates.
            double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
            double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);

            // Apply translation based on the change in origin.
            Content.TranslationX = targetX.Clamp (-Content.Width * (currentScale - 1), 0);
            Content.TranslationY = targetY.Clamp (-Content.Height * (currentScale - 1), 0);

            // Apply scale factor
            Content.Scale = currentScale;
        }
        if (e.Status == GestureStatus.Completed) {
            // Store the translation delta's of the wrapped user interface element.
            xOffset = Content.TranslationX;
            yOffset = Content.TranslationY;
        }
    }
}

然后在Xaml中将其用于ContentPage:

xmlns:local="clr-namespace:PinchGesture;assembly=YourNameSpace"

<local:ScaleAndRotateContainer>
    <local:ScaleAndRotateContainer.Content>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Center" >
            <Label Text="Hello Xamarin.Forms!" />
            <Image Source="waterfront.jpg" />
        </StackLayout>
    </local:ScaleAndRotateContainer.Content>
</local:ScaleAndRotateContainer>

效果:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将子视图添加到cell.contentView

来自分类Dev

使用约束将 UIView 添加到 TableViewCell 中的 ContentView

来自分类Dev

为什么不能直接将UIView添加到UITableViewCell而将contentView添加到UITableViewCell

来自分类Dev

将UIView添加到cell与cell.contentView的优点/缺点是什么?

来自分类Dev

通过Xib或Storyboard将子视图添加到UICollectionViewCell的contentView中

来自分类Dev

当我将contentview作为孩子添加到stackpanel时,scrollview跳跃

来自分类Dev

将UIView添加到cell与cell.contentView的优点/缺点是什么?

来自分类Dev

如何将子视图添加到tableView cell.contentView swift

来自分类Dev

将Facebook jar添加到Xamarin

来自分类Dev

哪个选项更好?直接将子视图添加到单元格中或单元格contentView中?

来自分类Dev

将Xamarin组件添加到共享代码

来自分类Dev

将jar文件添加到我的Xamarin项目

来自分类Dev

Xamarin将TouchRecognizer添加到每个单元格

来自分类Dev

Xamarin将图像添加到导航栏

来自分类Dev

将jar文件添加到我的Xamarin项目

来自分类Dev

将PopoverviewController添加到UIBarButtonItem Xamarin IOS

来自分类Dev

无法将软件包添加到Xamarin Studio

来自分类Dev

将XAML样式从Blend添加到xamarin

来自分类Dev

Xamarin.Forms 将子页面添加到 TabbedPage

来自分类Dev

Xamarin.iOS 将左图像添加到 UILable

来自分类Dev

Xamarin - 添加到深色/浅色主题

来自分类Dev

无法使用scale_x_discrete将标签添加到ggplot的x轴

来自分类Dev

无法将Xamarin.Android和Xamarin.iOS添加到PCL

来自分类Dev

将元素添加到数组

来自分类Dev

将UIViewcontrollers添加到UIScrollview

来自分类Dev

将值添加到对象数组

来自分类Dev

将道具添加到... this.props

来自分类Dev

将声明添加到ApplicationUser的子类

来自分类Dev

无法将XAttribute添加到XElement

Related 相关文章

  1. 1

    将子视图添加到cell.contentView

  2. 2

    使用约束将 UIView 添加到 TableViewCell 中的 ContentView

  3. 3

    为什么不能直接将UIView添加到UITableViewCell而将contentView添加到UITableViewCell

  4. 4

    将UIView添加到cell与cell.contentView的优点/缺点是什么?

  5. 5

    通过Xib或Storyboard将子视图添加到UICollectionViewCell的contentView中

  6. 6

    当我将contentview作为孩子添加到stackpanel时,scrollview跳跃

  7. 7

    将UIView添加到cell与cell.contentView的优点/缺点是什么?

  8. 8

    如何将子视图添加到tableView cell.contentView swift

  9. 9

    将Facebook jar添加到Xamarin

  10. 10

    哪个选项更好?直接将子视图添加到单元格中或单元格contentView中?

  11. 11

    将Xamarin组件添加到共享代码

  12. 12

    将jar文件添加到我的Xamarin项目

  13. 13

    Xamarin将TouchRecognizer添加到每个单元格

  14. 14

    Xamarin将图像添加到导航栏

  15. 15

    将jar文件添加到我的Xamarin项目

  16. 16

    将PopoverviewController添加到UIBarButtonItem Xamarin IOS

  17. 17

    无法将软件包添加到Xamarin Studio

  18. 18

    将XAML样式从Blend添加到xamarin

  19. 19

    Xamarin.Forms 将子页面添加到 TabbedPage

  20. 20

    Xamarin.iOS 将左图像添加到 UILable

  21. 21

    Xamarin - 添加到深色/浅色主题

  22. 22

    无法使用scale_x_discrete将标签添加到ggplot的x轴

  23. 23

    无法将Xamarin.Android和Xamarin.iOS添加到PCL

  24. 24

    将元素添加到数组

  25. 25

    将UIViewcontrollers添加到UIScrollview

  26. 26

    将值添加到对象数组

  27. 27

    将道具添加到... this.props

  28. 28

    将声明添加到ApplicationUser的子类

  29. 29

    无法将XAttribute添加到XElement

热门标签

归档