IOS/Objective-C:检测 UIVIew 上的滑动和屏幕相同位置的按钮触摸
Posted
技术标签:
【中文标题】IOS/Objective-C:检测 UIVIew 上的滑动和屏幕相同位置的按钮触摸【英文标题】:IOS/Objective-C: Detect swipe over UIVIew and button touch up in same location of screen 【发布时间】:2016-03-14 19:01:39 【问题描述】:您好,我有一些按钮可以触发操作方法。
此外,我希望用户能够使用按钮在区域上向左或向右滑动以显示一些不同的按钮
我的想法是在按钮顶部添加一个具有清晰背景颜色的 UIView,以便用户仍然可以看到按钮,但也可以向右或向左滑动。
但是,当视图位于按钮上方时,按钮不再起作用,而当视图位于按钮下方时,则不会检测到滑动。
除了将视图颜色更改为清除之外,我还尝试调整 alpha,但这似乎以相同的方式工作。当 UIView 的 alpha 为零时,不再检测到滑动,当 alpha 大于零时,按钮不再起作用。
谁能建议一种方法来做到这一点?
提前感谢您的任何建议。
我用于按钮和检测滑动的代码似乎是标准的。试图找出一种同时公开视图和按钮的方法。
按钮所连接的动作方法:
//.h file
- (IBAction)showIncoming:(id)sender;
- (IBAction)showOutcoming:(id)sender;
/.m file
- (IBAction)showIncoming:(id)sender
_showIncoming=YES;
_fetchedResultsController=nil;
[self.tableView reloadData];
[self updateInterface];
- (IBAction)showOutgoing:(id)sender
_showIncoming=NO;
_fetchedResultsController=nil;
[self.tableView reloadData];
[self updateInterface];
Swipes:
//in viewWillAppear
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
rightRecognizer.delegate = self;
[_topView addGestureRecognizer:rightRecognizer];
[_topView setUserInteractionEnabled:YES];
// [rightRecognizer release];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
leftRecognizer.delegate = self;
[_topView addGestureRecognizer:leftRecognizer];
[_topView setUserInteractionEnabled:YES];
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
//Do moving
NSLog(@"Right Swipe performed");
_showOld=YES;
[self updateInterface];
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
// do moving
NSLog(@"Left Swipe performed");
_showOld=NO;
[self updateInterface];
【问题讨论】:
【参考方案1】:我建议的方法是监听用户移动手指事件并触发指定的方法来检查用户的手指是否在您的按钮视图上。示例代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[self touchedViewWithTouches:touches andEvent:event];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
[self touchedViewWithTouches:touches andEvent:event];
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
[self touchedViewWithTouches:touches andEvent:event];
- (void)didTouchedButton:(NSSet *)touches andEvent:(UIEvent *)event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
UIView *touchedView;
for (UIView *view in self.subviews)
if(CGRectContainsPoint(view.frame, touchLocation))
touchedView = view;
break;
if (view == self.showIncomingButton)
[self showIncoming:self.showIncomingButton];
else if (view == self.showOutcomingButton)
[self howOutcoming:self.showOutcomingButton];
请注意,您必须为按钮添加两个插座(如果没有的话)。此外,您可以在 IBAction
通话中摆脱 (id)sender
。
【讨论】:
以上是关于IOS/Objective-C:检测 UIVIew 上的滑动和屏幕相同位置的按钮触摸的主要内容,如果未能解决你的问题,请参考以下文章
ios/objective-c: 检测 tabbarbutton 按下事件
IOS/Objective-C:检测自定义表格视图单元格中的按钮按下?