简体   繁体   English

从您触摸它的点拖动视图 iPhone

[英]Drag View from the point you touch it iPhone

I know how to drag a view or object:我知道如何拖动视图或 object:

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

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


    if( [touch view] == ViewMain)
    {
       CGPoint location = [touch locationInView:self.view];
       ViewMain.center = location;

    }


}

but I would like to start dragging that view or image from the point where I touched it.但我想从我触摸它的点开始拖动该视图或图像。 for example if I drag (I highlighted the view and placed blue on the cursor):例如,如果我拖动(我突出显示视图并将蓝色放在光标上):

在此处输入图像描述

the moment I start dragging if I drag the mouse 20 px to the right for example then I would like the view to also drag 20 px instead of:例如,如果我将鼠标向右拖动 20 px,那么当我开始拖动时,我希望视图也拖动 20 px,而不是:

在此处输入图像描述

maybe I have to change the center of the view to the point where I fist touch it.也许我必须将视图的中心更改为我用拳头触摸它的点。 How could I do that?我怎么能那样做?

In other words I would like to do something like when you drag the apps on the iPad:换句话说,当您在 iPad 上拖动应用程序时,我想做一些事情:

在此处输入图像描述

float startX = 0;
float startY = 0;
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == ViewMain)
    {
        CGPoint location = [touch locationInView:self.view];
        startX = location.x - ViewMain.center.x;        
        startY = ViewMain.center.y;
    }
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    if( [touch view] == ViewMain)
    {
        CGPoint location = [touch locationInView:self.view];
        location.x =location.x - startX;
        location.y = startY;
        ViewMain.center = location;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM