如何使用UIPanGestureRecognizer
移动屏幕中的多个控件以及如何识别当前移动的控件。
示例:
我在视图中使用UITextField
,UIButton
和UIImageView
。我可以通过为每个控件使用不同的实例来移动多个控件,但是平移的方法是相同的方法。
请帮我找出目前我在运行时平移的控件。
答案 0 :(得分:0)
1)以下是设置pag手势的方法
- (void)setupGestures
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePanel:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.view addGestureRecognizer:panRecognizer];
}
2)只要视图移动
,就会调用此方法 -(void)movePanel:(id)sender
{
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
//here are different states from which you can change view
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
}
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {
// Are you more than halfway? If so, show the panel when done dragging by setting this value to YES (1).
// Here you can change the position of the component depending on translation point
// translatedPoint.x & translatedPoint.y keep on changing as you move view over screen
}
}
3)为了检查哪个视图正在移动,您可以通过标签&从该标签识别视图&改变框架