【问题标题】:Drag View from the point you touch it iPhone从您触摸它的点拖动视图 iPhone
【发布时间】:2011-07-07 16:40:36
【问题描述】:

我知道如何拖动视图或对象:

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

    }


}

但我想从我触摸它的位置开始拖动该视图或图像。例如,如果我拖动(我突出显示视图并在光标上放置蓝色):

例如,如果我将鼠标向右拖动 20 px,那么我开始拖动的那一刻,我希望视图也拖动 20 px,而不是:

也许我必须将视图的中心更改为我用拳头触摸它的点。我怎么能那样做?

换句话说,当您在 iPad 上拖动应用程序时,我想做一些事情:

【问题讨论】:

    标签: iphone objective-c xcode ipad drag


    【解决方案1】:
    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;
        }
    }
    

    【讨论】:

    • 确保做 location.y - startY 以及 x 方面的事情
    • 是的。我忘了说这个例子只是沿着x轴拖动一些东西。
    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2012-11-12
    相关资源
    最近更新 更多