通过触摸拖动图像视图

发布于 2024-11-30 21:10:58 字数 1345 浏览 1 评论 0原文

我的应用程序处理图像视图,我想通过触摸将图像拖动到整个视图上。

-(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.
     touchOffset = hairImage.center.x - touchXBeginPoint;
    touchOffset1=hairImage.center.y-touchYBeginPoint;

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

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

    float distanceMoved =( [ touch locationInView:touch.view ].x + touchOffset ) -  hairImage.center.x  ;
    float distanceMoved1=([touch locationInView:touch.view].y+touchOffset)-hairImage.center.y;
    float newX = hairImage.center.x + distanceMoved;//+distanceMoved1 ;
    float newY=hairImage.center.y+distanceMoved1;
    if( newX > 70 && newX < 150 ){ // setting the boundaries
        hairImage.center = CGPointMake(newX, hairImage.center.y) ;}
    if(newY>100 && newY<180){
        hairImage.center=CGPointMake(newY, hairImage.center.x);
    }}

这是代码。在这里,我可以沿 X 方向成功移动 imageview,但我的目标是在整个视图中移动 imageview。

My App deals with an imageview and I want to drag the image all over the view by means of touches.

-(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.
     touchOffset = hairImage.center.x - touchXBeginPoint;
    touchOffset1=hairImage.center.y-touchYBeginPoint;

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

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

    float distanceMoved =( [ touch locationInView:touch.view ].x + touchOffset ) -  hairImage.center.x  ;
    float distanceMoved1=([touch locationInView:touch.view].y+touchOffset)-hairImage.center.y;
    float newX = hairImage.center.x + distanceMoved;//+distanceMoved1 ;
    float newY=hairImage.center.y+distanceMoved1;
    if( newX > 70 && newX < 150 ){ // setting the boundaries
        hairImage.center = CGPointMake(newX, hairImage.center.y) ;}
    if(newY>100 && newY<180){
        hairImage.center=CGPointMake(newY, hairImage.center.x);
    }}

This is the code. Here I can move the imageview successfully along X direction, but my target is to move imageview all over the view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2024-12-07 21:10:58

关于你的问题的更多信息会有所帮助......比如你尝试过什么,你在哪里被卡住等等......无论如何,我只是复制粘贴你的标题问题并用谷歌搜索......这是第一个结果

http://www.icodeblog.com/2008/10/20/iphone-programming-tutorial-using-uitouch-to-drag-an-image-around-the-screen/

Bit more info on your question helps...like what you have tried, where you got stuck etc..Anyway I just copy pasted the question of your title and googled..Here is the first result

http://www.icodeblog.com/2008/10/20/iphone-programming-tutorial-using-uitouch-to-drag-an-image-around-the-screen/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文