问题描述
我有这个(现在可以正常使用)代码,该代码基于我在各个地方收集的信息:
I have this (now working) code based on bits I picked up various places:
procedure TFormMain.imgMapsGesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
var
LObj: IControl;
LImage: TImage;
W: Single;
H: Single;
begin
LImage := nil;
LObj := Self.ObjectAtPoint(ClientToScreen(EventInfo.Location));
if (LObj is TImage) and (LObj.Visible) then
begin
LImage := TImage(LObj.GetObject);
if (LImage <> imgMaps) then
LImage := nil
;
end
;
if LImage = nil then
Exit
;
if LImage.Bitmap = nil then
Exit
;
case EventInfo.GestureID of
igiZoom:
begin
if (EventInfo.Distance < 1) then
Exit
;
if
(not(TInteractiveGestureFlag.gfBegin in EventInfo.Flags))
and
(not(TInteractiveGestureFlag.gfEnd in EventInfo.Flags))
then
begin
W := LImage.Width + 2 * ((EventInfo.Distance - FLastDistanceZoom) / 3);
H := LImage.Height + 2 * ((EventInfo.Distance - FLastDistanceZoom) / 3);
if
(W < layoutMapsContent.Width)
or
(H < layoutMapsContent.Height)
then
begin
W := layoutMapsContent.Width;
H := layoutMapsContent.Height;
end
;
LImage.Width := W;
LImage.Height := H;
FLastDistanceZoom := EventInfo.Distance;
end
;
end
;
igiPan:
begin
if
(not(TInteractiveGestureFlag.gfEnd in EventInfo.Flags))
then
begin
if (not(TInteractiveGestureFlag.gfBegin in EventInfo.Flags)) then
begin
LImage.Position.X := LImage.Position.X + (EventInfo.Location.X - FMapsLastPositionPan.X);
LImage.Position.Y := LImage.Position.Y + (EventInfo.Location.Y - FMapsLastPositionPan.Y);
end
;
FMapsLastPositionPan.X := EventInfo.Location.X;
FMapsLastPositionPan.Y := EventInfo.Location.Y;
end
;
end
;
我的变焦工作得很好(虽然不是在模拟器中,但是在iOS iPhone上),但是平移根本不起作用.在模拟器中平移时,我可以看到eventdisance始终为0.我在TImage上启用了pan + zoom几何图形.
I got zoom working fairly well (not in simulator though, but on iOS iPhone), however panning is not working at all. When panning in simulator I can see eventdisance is always 0. I have enabled pan + zoom geatures on the TImage.
推荐答案
我怀疑您在各个地方获取的代码是否有效:
I doubt the code you picked up in various places actually works:
关于TGestureInfo的文档文档明确指出
The documentation on TGestureInfo clearly states that
这意味着您将必须跟踪上一次调用onGesture
的手指已注册的位置.然后,您可以现在就EventInfo.Location
和EventInfo.Location
之差进行一些操作.当然,这仅在gfBegin
不在GestureEvent.Flags
中时才起作用,因为您没有前一个职位的有效值,但您已经知道了.
That means that you will have to keep track of the position the finger was registered in the previous call of onGesture
. You can then do something regarding the difference of EventInfo.Location
then and EventInfo.Location
now. Of course, this is only going to work if gfBegin
is not in GestureEvent.Flags
because you won't have a valid value for a previous position but you already know that.
此外,您可能还会看到EventInfo.InertiaVector
,因为当您抬起手指时,东西继续移动了一点".但这是完全可选的.
Additionally, you might have a look at EventInfo.InertiaVector
for "stuff continues moving for a bit when you lift your finger". But that's entirely optional.
此外,如果要处理手势(无论是交互式手势还是标准手势),都应将Handled
设置为True
.这样,您就不必冒险尝试其他组件来处理手势.但老实说,我不确定FireMonkey是否也是如此.有了Vcl,事实就是如此.将 FMX.Types.TInteractiveGestures 的文档与.安全胜过遗憾.
Also, if you're handling a gesture (no matter if interactive or standard gesture), you should set Handled
to True
. This way, you don't risk another component trying to handle the gesture. But to be honest, I'm not exactly sure if this is also the case with FireMonkey. With Vcl, it is. Compare the documentation for FMX.Types.TInteractiveGestures with Vcl.Controls.TInteractiveGestureOption. Better safe than sorry.
这篇关于Delphi iOS和平移手势-距离始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!