使用touchesBegan: 和touchesEnded 加速元素:

发布于 2024-12-11 03:18:09 字数 330 浏览 4 评论 0原文

我有一个 UIImageView,我尝试根据拖动的距离和拖动的速度来加速。通过加速我的意思是当调用touchesEnded:时,imageView应该沿着它被拖动的方向进一步滑动。它应该滑动多远和多快取决于它被拖动的距离和速度。

此时,我可以拖动图像视图并获取拖动的距离+花费的时间。基于此我可以计算速度和方向向量。

但我正在努力解决在 imageview 上通过 TouchEnded: 执行的幻灯片。

我的问题是:是否有任何常见或智能的方法可以在我尝试执行的 UIImageView 上执行这种“滑动”效果?

我很乐意接受任何可能有帮助的解决方案或提示。

谢谢。

I have a UIImageView that I try to accelerate based on the distance it has been dragged and how fast it was dragged. By accelerating I mean that when touchesEnded: is called, the imageView should slide further in the direction it was dragged. How far and how fast it should slide depends on the distance and the speed with which it was dragged.

At this point I am able to drag the imageview around and get the distance it was dragged + how long time it took. Based on this I can calculate the speed and a direction vector.

But I am struggling with the slide performed on the imageview by touchesEnded:.

My questions is: Is there any common or smart way to perform this "sliding" effect on the UIImageView that I am trying do to?

I gladly accept any solution or tip that might help out.

Thanks.

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

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

发布评论

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

评论(1

悍妇囚夫 2024-12-18 03:18:10

这个问题的解决方案比我想象的要简单得多。以下是我的想法(这是简单的版本,没有所有花哨的代码):

@interface myViewController {
    CGPoint _velocity;
    CGFloat _damping;
    UIImageView *_myImageView;
}

- (void)viewDidLoad {
    _velocity = CGPointZero; // x = 0, y = 0

   // Accelerate _myImageView 
   NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:0.02f // Update frequency 
                                               target:self 
                                             selector:@selector(slideImageView) 
                                             userInfo:nil 
                                              repeats:YES];
}

@implementation myViewController

- (void)slideImageView {
    // No need to move _myImageView when _velocity = 0
    if (_velocity.x > 0 && _velocity.y > 0)
        CGPoint position; // The next position
        position = _myImageView.center;

        position.x += _velocity.x / 30;
        position.y += _velocity.y / 30;

        // Damp _velocity with damping factor
        _velocity.x *= _damping;
        _velocity.y *= _damping;

        // Bounce on edges
        if (position.x < X_AXIS_MIN_VALUE || position.x > X_AXIS_MAX_VALUE)
            _velocity.x = -_velocity.x;

        if (position.y < Y_AXIS_MIN_VALUE || position.y > Y_AXIS_MAX_VALUE)
            _velocity.y = -_velocity.y;

        // Move 
        _myImageView.center = position;
    }
}

// Move been around by implementing touchesBegan: and touchesMoved:
// There are a million tutorials on how to do this.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // Do whatever you need to do and calculate x- and y-axis velocity,
    // maybe based on the distance between the last 5 points / time.
    CGPoint mySpeed;
    mySpeed.x = //the new x speed;
    mySpeed.y = //the new y speed
    _velocity = mySpeed;
}

@end

上面的代码(+缺少的实现)允许您在屏幕上拖动 UIImageView。当您松开手指时,ImageView 将继续在屏幕上滑动,如果被击中,则会在边缘上弹起。您移动手指的速度越快,ImageView 的加速就越多(具体取决于您计算速度的方式)。

我希望任何遇到此类问题的人都会发现它很有用。

The solution to this problem is far simpler than I had expected. The following is what I have come up with (this is the simple version, without all the fancy code):

@interface myViewController {
    CGPoint _velocity;
    CGFloat _damping;
    UIImageView *_myImageView;
}

- (void)viewDidLoad {
    _velocity = CGPointZero; // x = 0, y = 0

   // Accelerate _myImageView 
   NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:0.02f // Update frequency 
                                               target:self 
                                             selector:@selector(slideImageView) 
                                             userInfo:nil 
                                              repeats:YES];
}

@implementation myViewController

- (void)slideImageView {
    // No need to move _myImageView when _velocity = 0
    if (_velocity.x > 0 && _velocity.y > 0)
        CGPoint position; // The next position
        position = _myImageView.center;

        position.x += _velocity.x / 30;
        position.y += _velocity.y / 30;

        // Damp _velocity with damping factor
        _velocity.x *= _damping;
        _velocity.y *= _damping;

        // Bounce on edges
        if (position.x < X_AXIS_MIN_VALUE || position.x > X_AXIS_MAX_VALUE)
            _velocity.x = -_velocity.x;

        if (position.y < Y_AXIS_MIN_VALUE || position.y > Y_AXIS_MAX_VALUE)
            _velocity.y = -_velocity.y;

        // Move 
        _myImageView.center = position;
    }
}

// Move been around by implementing touchesBegan: and touchesMoved:
// There are a million tutorials on how to do this.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // Do whatever you need to do and calculate x- and y-axis velocity,
    // maybe based on the distance between the last 5 points / time.
    CGPoint mySpeed;
    mySpeed.x = //the new x speed;
    mySpeed.y = //the new y speed
    _velocity = mySpeed;
}

@end

The above code (+ the missing implementation) allows you to drag a UIImageView across the screen. When you release your finger, the ImageView will continue sliding over the screen, bouncing on the edges if they're hit. The faster you move your finger, the more the ImageView will accelerate (well, based on how you calculate the velocity).

I hope that anyone who has struggled with an issue like this will find it useful.

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