我用一个旋钮创建了一个控件。这个旋钮应该沿着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;有人能给我一个提示吗?如何使沿着这些两轴的运动,只有。
发布于 2012-07-14 23:21:14
我能想到的最快的方法,从我的头顶:
在touchesBegan中,记录下旋钮内第一次触摸的位置。然后,在touchesMoved中,等待至少有一个最小的距离(可能是15个像素)从第一次触摸开始,看看什么是最有效的。检查它是从x方向的第一次触摸到还是在y方向上。这将决定在当前触摸序列的其余部分中,应该限制哪个轴的运动。设置一个标志来指示轴,并从那时起使用您已经拥有的代码,以便将运动限制在单个轴上。在touchesEnded中或当旋钮返回到离轴交点一定距离的范围内重置标志。
https://stackoverflow.com/questions/11486509
复制相似问题