我创建了一个带有旋钮的控件。该旋钮只能沿 x 轴和 y 轴移动。
我已经实现了以下方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
我知道,必须在touchMoved
. 当我限制沿例如 x 轴的移动时,一切都按预期工作。但是如何启用 y 轴的运动呢?当我启用第二个轴时,旋钮的移动不仅限于这 两个轴。这里是touchMoved
实现的一部分:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint currentLocation = [touch locationInView:self.view];
CGPoint centerPoint = _triggerCap.center;
CGPoint selectionTriggerCenter = _selectionTrigger.center;
double distance = ({double d1 = centerPoint.x - selectionTriggerCenter.x, d2 = centerPoint.y - selectionTriggerCenter.y; sqrt(d1 * d1 + d2 * d2); });
if (distance < 100.0) {
if (fabsf(selectionTriggerCenter.y - currentLocation.y) > 5) {
centerPoint.y = currentLocation.y;
_triggerCap.center = centerPoint;
有人可以给我一个提示如何仅沿这两个轴移动吗?