Objective c 与UIButton对象一起识别滑动方向
Objective c 与UIButton对象一起识别滑动方向,objective-c,ios,uiswipegesturerecognizer,Objective C,Ios,Uiswipegesturerecognizer,我需要识别执行滑动的方向,并知道视图上的哪个对象发送了滑动。我现在已经找到了解决办法,但它看起来复杂而可怕,所以我怀疑有没有最简单的解决办法?
我的解决方法是:
我将识别器附加到视图中的每个UIButton对象
有一个指示灯,显示在执行刷卡时点击了哪个按钮
我必须检查这个指示器和滑动方向,以便做出正确的决定,移动哪个对象和方向
谢谢你的建议
@synthesize swipeDirection; //label which shows swipe direction
@synthesize
我需要识别执行滑动的方向,并知道视图上的哪个对象发送了滑动。我现在已经找到了解决办法,但它看起来复杂而可怕,所以我怀疑有没有最简单的解决办法?
我的解决方法是:
我将识别器附加到视图中的每个UIButton对象
有一个指示灯,显示在执行刷卡时点击了哪个按钮
我必须检查这个指示器和滑动方向,以便做出正确的决定,移动哪个对象和方向
谢谢你的建议
@synthesize swipeDirection; //label which shows swipe direction
@synthesize buttonNumber; //label which shows number of button which being tapped
- (void)viewDidLoad
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //creating button
[button setTitle:@"test1" forState:UIControlStateNormal];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self //adding selector which will update indicator which button been tapped, there isn't way to make swipe without tap button
action:@selector(updateButtonNumber:)
forControlEvents:UIControlEventTouchDown];
[button setTag:1];
[self.view addSubview:button]; //add button to view
[self beginListenToSwipes:button]; //send button to method which will add UISwipeGestureRecognizer for 4 directions
/*same task for second button*/
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"test2" forState:UIControlStateNormal];
button1.frame = CGRectMake(200, 200, 100, 100);
[button1 addTarget:self
action:@selector(updateButtonNumber:)
forControlEvents:UIControlEventTouchDown];
[button1 setTag:2];
[self.view addSubview:button1];
[self beginListenToSwipes:button1];
}
/*every time when we tap button (begin swipe this method is called and buttonNumber always points to current button*/
-(void)updateButtonNumber:(UIButton*)sender
{
self.buttonNumber.text = [NSString stringWithFormat:@"%d",sender.tag];
}
/*method receives UIButton and add to it recognizers*/
- (void) beginListenToSwipes:(UIButton*)sender
{
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[sender addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[sender addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[sender addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[sender addGestureRecognizer:recognizer];
}
/*method which called when swipe performed and shows in which direction it was performed*/
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
int i = recognizer.direction;
self.swipeDirection.text = [NSString stringWithFormat:@"%d",i];
}
@综合定向//显示滑动方向的标签
@合成纽扣数//显示被点击按钮数量的标签
-(无效)viewDidLoad
{
UIButton*button=[UIButton button类型:UIButtonTypeRondRect];//创建按钮
[按钮设置标题:@“test1”表示状态:UIControlStateNormal];
button.frame=CGRectMake(100100100100);
[button addTarget:self//添加选择器,该选择器将更新被点击的按钮的指示器,没有点击按钮就无法进行刷卡
操作:@选择器(updateButtonNumber:)
forControlEvents:UIControlEventTouchDown];
[按钮设置标签:1];
[self.view addSubview:button];//将按钮添加到视图
[self beginListenToSwipes:button];//将按钮发送到方法,该方法将为4个方向添加UISwipegestureRecognitor
/*第二个按钮的任务相同*/
UIButton*button1=[UIButton Button类型为:UIButtonyPeroundRect];
[按钮1设置标题:@“test2”表示状态:UIControlStateNormal];
button1.frame=CGRectMake(200200100100);
[按钮1添加目标:自我
操作:@选择器(updateButtonNumber:)
forControlEvents:UIControlEventTouchDown];
[按钮1设置标签:2];
[self.view addSubview:button1];
[自启动列表滑动:按钮1];
}
/*每次我们点击按钮(开始滑动)时,会调用此方法,buttonNumber始终指向当前按钮*/
-(无效)updateButtonNumber:(UIButton*)发送方
{
self.buttonNumber.text=[NSString stringWithFormat:@“%d”,sender.tag];
}
/*方法接收UIButton并将其添加到识别器中*/
-(无效)开始列表滑动:(UIButton*)发送方
{
UISwipegestureRecognitor*识别器;
识别器=[[UISweepGestureRecognitizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[识别器设置方向:(UISwipegestureRecognitizerDirectionRight)];
[发送方地址识别器:识别器];
识别器=[[UISweepGestureRecognitizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[识别器设置方向:(UISwipegestureRecognitizerDirectionLeft)];
[发送方地址识别器:识别器];
识别器=[[UISweepGestureRecognitizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[识别器设置方向:(UISwipegestureRecognitizerDirectionUp)];
[发送方地址识别器:识别器];
识别器=[[UISweepGestureRecognitizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[识别器设置方向:(UISwipegestureRecognitizerDirectionDown)];
[发送方地址识别器:识别器];
}
/*方法,该方法在执行刷卡时调用,并显示执行刷卡的方向*/
-(void)无手柄擦拭:(UISwipegestureRecognitor*)识别器
{
int i=识别器方向;
self.swipeDirection.text=[NSString stringWithFormat:@“%d”,i];
}
我认为您还可以将手势识别器置于self.view中,然后查看手势是否发生在所讨论的控件上(显然,您希望将按钮设置为IVAR或viewcontroller的属性,以便跟踪哪个是哪个),例如
这取决于您是否在UIGestureRecognitzerStateStart或UIGestureRecognitzerStateEnd执行此逻辑,或者同时执行这两种操作
更新:
顺便说一句,如果你正在检测你移动某物的方向,你也可以考虑使用UIPangestureRecognitor,它在一个手势识别器中处理所有四个方向(因为它是连续的,所以你可以在UI中为用户提供移动动画的实时反馈).我认为您还可以将手势识别器置于self.view中,然后查看手势是否发生在所讨论的控件上(显然,您希望将按钮设置为IVAR或viewcontroller的属性,以便跟踪哪个是哪个),例如
这取决于您是否在UIGestureRecognitzerStateStart或UIGestureRecognitzerStateEnd执行此逻辑,或者同时执行这两种操作
更新:
顺便说一句,如果你正在检测你移动某物的方向,你也可以考虑使用UIPanGestureRecognizer,它在一个手势识别器中处理所有四个方向(因为它是连续的,你可以在UI中为用户提供移动动画的实时反馈)。我的想法是
注册UIButton的touchUpoutside
事件
当你将手指移出按钮时,它将被触发
从touchesEnded方法中找到接触点,并存储在类级别变量中
在触碰外部
方法中比较按钮框和触碰点,以确定移动方向
我的想法是
注册UIButton的touchUpoutside
事件
当你将手指移出按钮时,它将被触发
从touchesEnded方法中找到接触点,并存储在类级别变量中
比较touchupou中的按钮框和触摸点
CGPoint point = [sender locationInView:self.view];
if (CGRectContainsPoint(button1.frame, point))
// do whatever you want