C# Xamarin将缩放/旋转添加到ContentView
我有个问题。 我创建了一个CustomView,可以在其中拖放视图,但现在我还想在其中添加一个缩放/旋转功能。现在,这里是为将视图移到内部而创建的OnTouchEvent:C# Xamarin将缩放/旋转添加到ContentView,c#,xamarin,xamarin.forms,xamarin.android,xamarin.ios,C#,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我有个问题。 我创建了一个CustomView,可以在其中拖放视图,但现在我还想在其中添加一个缩放/旋转功能。现在,这里是为将视图移到内部而创建的OnTouchEvent: public override bool OnTouchEvent(MotionEvent e) { float x = e.RawX; float y = e.RawY; var dragView = Element as DraggableView.DraggableView; swit
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);
}
public override bool OnTouchEvent(运动事件e)
{
浮点数x=e.RawX;
浮动y=e.RawY;
var dragView=作为DraggableView.DraggableView的元素;
开关(电动)
{
案例MotionEventActions.Down:
if(dragView.DragMode==DragMode.Touch)
{
如果(!touchedDown)
{
如果(第一次)
{
originalX=GetX();
原始=GetY();
第一次=错误;
}
dragView.DragStarted();
}
TextMoved=false;
touchedDown=true;
秒表。开始();
}
dX=x—this.GetX();
dY=y—this.GetY();
打破
case MotionEventActions。移动:
如果(触屏)
{
如果(dragView.DragDirection==DragDirectionType.All | | dragView.DragDirection==DragDirectionType.Horizontal)
{
SetX(x-dX);
}
如果(dragView.DragDirection==DragDirectionType.All | | dragView.DragDirection==DragDirectionType.Vertical)
{
SetY(y-dY);
}
TextMoved=true;
}
打破
case MotionEventActions.Up:
touchedDown=false;
if(TextMoved==true)
{
dragView.DragEnded();
}
其他的
{
MessagingCenter.Send(这是“EditSelectedText”,dragView);
}
打破
案例MotionEventActions。取消:
touchedDown=false;
打破
}
返回基地。OnTouchEvent(e);
}
但现在我还需要缩放/旋转功能。
问题是我已经为我的skiasharp位图创建了它,但这不是skiasharp,所以我不能使用它
如何在没有SkiaSharep的OnTouchEvent中实现此功能?关于有一个xamarin表单示例,但是它与
TouchEvent
无关。我找到了一种使用PangestureRecognitizer和PinchgestureRecognitizer实现它的方法
创建一个scaleAndRotate容器ContentView,其中包含PangEstureRecognitizer
和PinchEstureRecognitizer
:
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>
xmlns:local=“clr namespace:PinchGesture;assembly=YourNameSpace”
其效果是:
您好,您的意思是用手指触摸来实现对ContentView的缩放/旋转吗?这是一个关于的示例,你可以看一下。谢谢,比例函数工作得很好,但是我正在寻找一个2的旋转fingers@Vreesie好的,如果有两个手指的解决方案将在这里更新。如果有帮助,记得在你有时间的时候投票表决。