2011-01-11 133 views
2

我拿了一个UIView application.i置于控制器的view.my需求的图像使用鼠标的形象应该是可移动的mouse.mouse的帮助下应该选择图像和图像移动应该在拖动鼠标时移动。图像移动应该仅限于视图的特定部分。 有人可以帮我提前图像应在某一特定领域

dinakar

回答

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) ; 
} 

希望这应该是有帮助的。

3

Dinakar代码的理想任务 TNX我很遗憾地告诉你,没有鼠标(光标)在iPhone或iPad。

+1

你需要做的是在那个地方创建一个UIScrollView,而不是在.m文件中定义它的大小(UIScrollView)并且休息将被照顾 – Robin 2011-01-11 05:55:56

+0

没有鼠标指针事件,确切地说! – Mahesh 2011-01-11 06:53:52

3

知更鸟是right.mouse光标只是触摸了模拟器使用,因此这些都不是独立的事件

你需要阅读教程移动images.see这个link

这将帮助你。