简体   繁体   English

如何通过触摸和滑动来移动和滑出UIView?

[英]How can I move around and slide away a UIView with touches and swipes?

I´m trying to animate a sort of draggable UIView where the behaviour should be something like this: 我正在尝试制作一种可拖动的UIView动画,其行为应如下所示:

  • Drag along the screen following the touch of the user. 跟随用户的触摸,沿着屏幕拖动。
  • Detect a swipe touch and drag automatically with an initial speed and a negative acceleration that makes is stop after some time. 检测滑动触摸并以初始速度和负加速度自动拖动,该负加速度会在一段时间后停止。

Something like moving around a photo and then through it to one side of the screen. 就像在照片周围移动,然后穿过照片到达屏幕的一侧。

Any tips on this? 有什么提示吗?

A similar question was asked (by me) about 3 weeks ago. 大约3周前,(我)问了一个类似的问题 Though some of the answers I received were a little off the mark the best advice was: 1- study Apple's MoveMe example for how to touch an object and drag it around. 尽管我收到的一些答案有些偏离,但最好的建议是:1-研究Apple的MoveMe示例,以了解如何触摸物体并拖动物体。 And 2- you "throw" the object by basically comparing the [touch locationInView:self.view] to the [touch previousLocationInView:self.view] in touchesEnded (could also store a reference to first touch in touchesBegan but I find that less helpful as user may pause) using the time stamp of each you can determine the direction of swipe (flick) and rate of speed and use them to send your little UIView flying around. 然后2-通过在touchesEnded中将[touch locationInView:self.view][touch previousLocationInView: self.view ]基本进行比较来“抛出”对象(还可以在touchesBegan中存储对首次触摸的引用,但我发现它没什么帮助(用户可能会暂停),使用每个时间戳记可以确定滑动(轻拂)的方向和速度,并使用它们发送您的小UIView飞来飞去。 Obviously you want to apply forces ("friction") or whatever to slow it down so it doesn't fly right of your screen... 显然,您想施加力量(“摩擦”)或使其减慢速度,以便它不会在屏幕上飞来飞去...

I am not sure. 我不确定。 But may be you can use decelerating property of UIScrollView object. 但是也许您可以使用UIScrollView对象的de减速属性。 Put your imageView in UIScrollView and use this property. 将您的imageView放在UIScrollView中并使用此属性。 Its a BOOL var and the returned value is YES if user isn't dragging the content but scrolling is still occurring. 它是BOOL变量,如果用户不拖动内容但仍在滚动,则返回值为YES。

Take a look at the MoveMe sample project from Apple (registration may be required). 查看Apple的MoveMe示例项目(可能需要注册)。 It demonstrates how to have a placard follow a touch as well as transform itself based on different touches. 它演示了如何让标语牌跟随触摸以及如何根据不同的触摸进行自我转换。

Essentially, the idea is that you override the UIResponder touch methods to analyze what kind of touch the device received, and then either move the object to follow the user's finger or give it a speed in a direction depending on whether it was a slow drag or a swipe. 本质上,其想法是,您可以覆盖UIResponder触摸方法以分析设备接收到的触摸类型,然后移动对象以跟随用户的手指,或者根据是否是缓慢拖动还是在某个方向赋予其速度轻扫。 You can then use Core Animation to animate changes to the object's frame. 然后,您可以使用“核心动画”为对象的帧设置动画。

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

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