应在特定区域使用鼠标移动图像

时间:2011-01-11 05:45:38

标签: iphone objective-c ipad

我带了一个uiview应用程序。我在控制器的视图上放置了一个图像。我的要求是图像应该可以在鼠标的帮助下移动。鼠标应该选择图像并且图像应该在拖动鼠标时移动。图像移动应仅限于视图的特定部分。  有人可以帮助我编写理想的任务 tnx提前

dinakar

3 个答案:

答案 0 :(得分:3)

Dinakar我很遗憾地告诉你iphone或ipad中没有鼠标(光标)。

答案 1 :(得分:3)

robin是对的。鼠标光标只是用作模拟器的触摸,所以这些不是单独的事件

您需要阅读移动图片的教程。请参阅此link

这对你有帮助。

答案 2 :(得分:2)

这可以通过touchesBegan和touchesMoved事件来完成。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    // This gets you starting position of 
    UITouch *touch = [ [ event allTouches ] anyObject ] ;   

    float touchXBeginPoint = [ touch locationInView:touch.view ].x ;
    float touchYBeginPoint = [ touch locationInView:touch.view ].y ;

    // Calculate the offset between the current image center and the touched points.
    //  Moving image only along X - direction and try thinking as how to move in 
    // any direction using this as a reference. It isn't that hard.

    touchOffset = image.center.x - touchXBeginPoint ;
    // touchOffset should be a member variable of class or a variable with global scope

} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Calculate the difference from previous position and the current position
    // Add this difference to the previous point and move the image center to that point.
    // How ever, you should have an UIImageView outlet connected on to the image placed
    // on the interface builder.

    // And regarding image movement restriction, since you always have co-ordinates with
    // you, you can set the boundaries.

    UITouch *touch = [ [ event allTouches ] anyObject ] ;

    float distanceMoved =( [ touch locationInView:touch.view ].x + touchOffset ) -  image.center.x  ;
    float newX = image.center.x + distanceMoved ;

    if( newX > 30 && newX < 290 ) // setting the boundaries
        image.center = CGPointMake(newX, image.center.y) ;
}

希望这应该有所帮助。