我目前正在努力在 Python 中为我的移动图形库实现自定义缩放手势。尽管尝试了多种解决方案,包括探索 ChatGPT,我还是遇到了代码无法正常运行的问题。
当我捏合并放大、松开并尝试再次放大时,就会出现问题。缩放重置而不是继续放大,这不是预期的行为。
我非常感谢社区为解决此问题提供的任何帮助。预先感谢您的帮助。
import sys
import sdl2
import sdl2.ext
import numpy as np
import math as ml
from spectrax import operations as op
class ZoomGestureHandler:
def __init__(self, window):
self.zoom_level = 1.0
self.zoom_factor = 0.01
self.max_zoom = 2.0
self.min_zoom = 0.5
self.window = window
self.finger_positions = [[0, 0], [0, 0]]
self.finger_positions_pre = [[0, 0], [0, 0]]
self.finger_count = 0
self.zooming = False
self.image_zoom = 0
def get_zoom(self):
# Use this function to retrieve the zoom_level
zoom = abs(ml.dist(*self.finger_positions)) * 10
return zoom if zoom > 1 else 1
def event_handler(self, event):
zoom_handler = self
fingers = event.touch_gestures.fingers
if event.mouse.press and "0" in fingers and "1" in fingers and event.touch_gestures.finger_count > 1:
f1 = event.touch_gestures.fingers["0"].xy
f2 = event.touch_gestures.fingers["1"].xy
self.finger_positions = [f1, f2]
self.zooming = True
else:
self.zooming = False