如何在按钮和TouchUpInside上有两种不同的SWIPE方法?

如何解决如何在按钮和TouchUpInside上有两种不同的SWIPE方法?

| 我有一个视图,其中有几个大按钮,它们的设置如下:
SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55,300,50)] autorelease];    
btn.tag = k;

[btn setBackgroundImage:[UIImage imageNamed:[Nsstring stringWithFormat:@\"titleBackground.png\"]] 
                                                              forState:UIControlStatenormal];
[btn   addTarget:self
          action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
我希望能够触摸这些按钮,然后触发indexAction方法,或者-如果识别出轻扫,则触发另一个方法。 我以为我可以继承UIButton的子类,但这并不是真正的解决方案,因为现在可以识别出滑动和点击,因此可以同时使用这两种方法。 有没有解决的办法?如何在没有刷卡的情况下优先安排刷卡的优先级,并且只允许在触摸屏上方进行刷卡? 任何帮助将不胜感激。 这是我将UIButton子类化的方式:
#import \"SwipeButton.h\"

@implementation SwipeButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.superview touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesMoved:touches withEvent:event];
    [self.superview touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.superview touchesEnded:touches withEvent:event];

} 
@end
    

解决方法

我认为您不需要继承UIButton类。你为什么不尝试这样的事情:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@\"picture_0.png\"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake((applicationFrame.size.width - image.size.width)/2,applicationFrame.size.height/2,image.size.width,image.size.height)];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
然后,创建用于处理按钮上的轻击和滑动的方法:
// Prevent recognizing touches on the slider
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

if ([touch.view isKindOfClass:[UIButton class]]) {
    return YES;
}
return NO;
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {

    // Handle left swipe
    NSLog(@\"left swipe\");

} else {

    // Handle right swipe
    NSLog(@\"right swipe\");

}
}

- (void)buttonTapped:(id)sender {

NSLog(@\"button pressed\");

}
协议中声明了前面三个方法中的第一个,因此不要忘了说您的类正在实现该协议:
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
它有效,我自己尝试过。希望能帮助到你! ;)     ,看一下
UIGestureRecognizer
文档和相关示例(例如)。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?