开发者

Drag View from the point you touch it iPhone

开发者 https://www.devze.com 2023-03-18 13:51 出处:网络
I know how to drag a view or object: - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

I know how to drag a view or 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):

Drag View from the point you touch it iPhone

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:

Drag View from the point you touch it iPhone

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 dra开发者_开发技巧g the apps on the iPad:

Drag View from the point you touch it iPhone


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

精彩评论

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