开发者

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 开发者_JAVA技巧the mouse is dragged.the image movement should be restricted to only a particular portion of the view. can someone help me code the desirable task tnx in advance

dinakar


Dinakar i am very sorry to tell you that there is no mouse (cursor) in iphone or ipad.


robin is right.mouse cursor just use as a touch for simulator so these are not separate events

you need to read tutorials for moving images.see this link.

This will be help you.


This can be accomplished with touchesBegan and touchesMoved events.

- (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜