仅在定义的区域内拖动对象

时间:2011-08-23 21:03:57

标签: objective-c

我有一个对象我希望用户能够沿着屏幕上的定义区域拖动,而不仅仅是屏幕上的任何位置。我希望它在屏幕(y轴)上的特定高度移过屏幕(x轴),因此,用户可以沿x轴移动对象,同时保持y轴上的设置位置。

如果我的坐标是正确的......

我希望对象从500x,580y开始,然后用户可以沿着x轴将对象拖动到20x到990x之间的任何位置,同时保持y轴上的对象位置为580.

这是我到目前为止所发表的......已注释掉的部分//是我正在尝试但不起作用的部分....

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
//CGPoint location=[touch locationInView:touch.view]; 
//CGPoint yLocation=CGPointMake(object.center.x,location.y) object.center=yLocation;

    if([touch view] ==myimage)
{
    myimage.center = touchLocation;
}
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
//new Draggable('myimage',
              //{snap: function(y)
                 // {return[ (y<1024)?(y>0?y:0):1024];
                  //}
              //}
 //CGPoint location=[touch locationInView:touch.view]; 
// CGPoint yLocation=CGPointMake(object.center.x,location.y) object.center=yLocation;

    if([touch view] ==myimage)

    if([touch tapCount] ==2)
    {
        myimage.transform =CGAffineTransformMakeRotation (3.141592654); //M_PI*1.00

    }

 }

感谢

1 个答案:

答案 0 :(得分:0)

我认为您将图像中心点设置为触摸位置。这会将图像移动到您触摸的任何位置。要一次控制一个轴,您只需要将触摸位置分解为X和Y轴,并仅更新要移动的图像的X或Y坐标。同时,每次更新都会始终使用您希望保持不变的静态坐标。这应该可以为您提供所需的结果。我很确定你不需要拖动代码的其余部分。

示例:

CGPoint touchLocation = [touch locationInView:self.view];
myImage.center = CGPointMake(touchLocation.x, 580);
//Here we just use the X coordinate of the touch
//We make a new point location and use 580 always since we never
//want the Y axis to move. This should give you an image that moves
//left or right as you move your finger but never up and down.
相关问题