UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];UIVIEw *temp = [[UIVIEw alloc]initWithFrame:self.vIEw.frame];temp.backgroundcolor = [UIcolor clearcolor];[temp addGestureRecognizer:swipe];[self.vIEw addSubvIEw:temp];
在swipeAction选择器中我有:
- (voID)swipeAction: (UISwipeGestureRecognizer *)sender{ NSLog(@"Swipe action called"); if (sender.direction == UISwipeGestureRecognizerDirectionleft) { //Do Something } else if (sender.direction == UISwipeGestureRecognizerDirectionRight){ //Do Something Else }}
令我惊讶的是,此方法仅在您向右滑动时起作用(即,如果调用块,则为else).向左滑动时,swipeAction甚至不会被调用!这很奇怪,为什么会发生这种情况,我该如何更改代码?任何回复表示赞赏.非常感谢!
解决方法 这里有一些你应该注意的事情.首先,您必须为要观察的每个方向创建一个手势.这不是什么大问题,因为你可以简单地给他们相同的选择器,它就像一个两个方向的手势.UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];leftSwipe.direction = UISwipeGestureRecognizerDirectionleft;UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;UIVIEw *temp = [[UIVIEw alloc]initWithFrame:self.vIEw.frame];temp.backgroundcolor = [UIcolor clearcolor];[temp addGestureRecognizer:leftSwipe];[temp addGestureRecognizer:rightSwipe];[self.vIEw addSubvIEw:temp];
其次,你从未指定手势的方向,使其默认为右(或方向枚举为1)
从documentation:
The default direction is UISwipeGestureRecognizerDirectionRight. See descriptions of UISwipeGestureRecognizerDirection constants for more information.
typedef enum { UISwipeGestureRecognizerDirectionRight = 1 << 0,UISwipeGestureRecognizerDirectionleft = 1 << 1,UISwipeGestureRecognizerDirectionUp = 1 << 2,UISwipeGestureRecognizerDirectionDown = 1 << 3} UISwipeGestureRecognizerDirection;总结
以上是内存溢出为你收集整理的ios – UISwipeGestureRecognizer只有一个方向可行全部内容,希望文章能够帮你解决ios – UISwipeGestureRecognizer只有一个方向可行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)