[英]image should be moved using mouse in a particular area
i took a uiview application.i placed a image on the controller's view.my requirement is the image should be movable with the help of mouse.mouse should select the image and the image should be moved as the mouse is dragged.the image movement should be restricted to only a particular portion of the view. 我拿了一个uiview应用程序。我在控制器的视图上放置了一个图像。我的要求是图像应该可以在鼠标的帮助下移动。鼠标应该选择图像并且图像应该在拖动鼠标时移动。图像移动应该仅限于视图的特定部分。 can someone help me code the desirable task tnx in advance 有人可以帮助我提前编码理想的任务
dinakar dinakar
Dinakar我很遗憾地告诉你,iphone或ipad中没有鼠标(光标)。
This can be accomplished with touchesBegan and touchesMoved events. 这可以通过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) ;
}
Hope this should be helpful. 希望这应该是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.