我有以下问题。
我使用UILongPressGestureRecognizer
将UIView置于“切换模式”。如果UIView
处于“切换模式”,则用户可以在屏幕上拖动UIView。为了在屏幕上拖动UIView,我使用方法touchesBegan
,touchesMoved
和touchesEnded
。
它有效,但是:我必须抬起手指才能拖动它,因为touchesBegan
方法已被调用,因此不会再次调用,因此我无法拖动UIView
屏幕周围。
触发touchesBegan
后,是否有办法手动调用UILongPressGestureRecognizer
UILongPressGestureRecognizer
更改BOOL值,touchesBegan
仅在此BOOL设置为YES时才有效
答案 0 :(得分:10)
UILongPressGestureRecognizer
是一个连续的手势识别器,所以不要诉诸touchesMoved
或UIPanGestureRecognizer
,只需检查UIGestureRecognizerStateChanged
,例如:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gesture];
}
- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
// user held down their finger on the screen
// gesture started, entering the "toggle mode"
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
// user did not lift finger, but now proceeded to move finger
// do here whatever you wanted to do in the touchesMoved
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
// user lifted their finger
// all done, leaving the "toggle mode"
}
}
答案 1 :(得分:0)
我建议您使用UIPanGestureRecognizer作为推荐的拖动手势。
您可以配置分钟。最多使用以下属性进行平移所需的触摸次数:
<强> maximumNumberOfTouches 强>
<强> minimumNumberOfTouches 强>
您可以处理Began,Changed和Ended等状态,例如为所需状态设置动画。
使用以下方法将该点转换为您想要的UIView。
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
示例:
您必须使用全局变量来保留旧框架。在UIGestureRecognizerStateBegan中获取此信息。
当状态为UIGestureRecognizerStateChanged时。您可以使用
-(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{ if(panGesture.state==UIGestureRecognizerStateBegan){ //retain the original state }else if(panGesture.state==UIGestureRecognizerStateChanged){ CGPoint translatedPoint=[panGesture translationInView:self.view]; //here you manage to get your new drag points. } }
阻力的速度。根据速度,您可以提供动画来显示UIView的弹跳
- (CGPoint)velocityInView:(UIView *)view