【问题标题】:Help me understand iPhone touchesBegan implementation and dragging a UIView帮我理解 iPhone touchesBegan 的实现和拖拽一个 UIView
【发布时间】:2010-11-21 00:13:27
【问题描述】:

我知道以前有人回答过这个问题 (iPhone "touchesBegan" and "touchesMoved" message ... do not move to centre of touch),但我对 iPhone 开发很陌生,所以不明白答案。

基本上,我想要做的是有一个 UIImageView 响应触摸,在屏幕上移动它。来自 Apple 的示例有一个视图,当它有 touchesBegan 时,将其中心移动到触摸,然后移动视图。

我想要的是当您使用 UIImagePicker 时图像可以平移。所选图像不会与触摸对齐,而是会从触摸中移动。

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone uiimageview drag touchesbegan


    【解决方案1】:

    首先,您需要了解您第一次触摸屏幕的位置。然后对于每个“触摸移动”,获取新点的 delta x 和 y 坐标,并将图像移动该数量。

    // This is untested code and I can't even be sure if it compiles
    // Hopefully it is verbose enough to help you with that you are
    // Trying to do. If not I can update once I get back infront of 
    // a Mac.
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        lastPoint = [touch locationInView:self.view];
        CGPoint delta = currentPoint - lastPoint;
    
        currentViewLoc = imageView.center;
    
        imageView.center.x = currentViewLoc.x - delta.x;
        imageView.center.y = currentViewLoc.y - delta.y;
    
        currentPoint = lastPoint;
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject]; 
        currentPoint = [touch locationInView:self.view];
    }
    

    反正就是这样。我希望你能明白我想说什么。

    【讨论】:

    • 非常感谢您花时间回答,但是作为一个完整的初学者,看看它是如何实际使用的真的很有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2021-10-20
    • 2010-11-28
    相关资源
    最近更新 更多