只要用户按下手指,iOS8 +应用程序中的按钮应该通过在按钮周围绘制轮廓来作出反应。目标是将此行为封装到类(cp。类以下的层次结构)中。当释放手指时,应用程序应该执行定义的动作(主要执行到另一个视图控制器的继续)。这里是我为了这个目的当前类层次结构:iOS:如何在超类中正确实现自定义手势识别器?
- UIButton
|_ OutlineButton
|_ FlipButton
的FlipButton类进行一些花哨的翻页效果,另外我有阴影在UIView的一个类别,圆润的边角和轮廓。
目前,我有以下附加类:
#import <UIKit/UIKit.h>
@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end
......与对应的实现:
#import "UIView+Extension.h"
#import "TouchDownGestureRecognizer.h"
@implementation TouchDownGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view showOutline]; // this is a function in the UIView category (cp. next code section)
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view hideOutline]; // this is a function in the UIView category (cp. next code section)
}
@end
...这是UIView的+ Extension.m类别的相关片段用于在按钮上绘制轮廓:
- (void)showOutline {
self.layer.borderColor = [UIColor whiteColor].CGColor;
self.layer.borderWidth = 1.0f;
}
- (void)hideOutline {
self.layer.borderColor = [UIColor clearColor].CGColor;
}
...和OutlineButton.m文件中我见到目前为止如下:
#import "OutlineButton.h"
@implementation OutlineButton
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addGestureRecognizer:[[TouchDownGestureRecognizer alloc] init]];
}
return self;
}
@end
外观上看,这款尽快正常工作,作为一个按钮被触摸的轮廓被绘制并尽快释放手指再次隐藏。但是,通过故事板连接到这些按钮的IBAction和segues是在经过很长时间(大约2秒)之后才会执行的。如果多次按下该按钮(...长时间延迟后),也会执行多次操作。真的很奇怪的行为...
有人有任何想法如何解决这个问题?
解决方案(基于马特的回答,谢谢):
#import "OutlineButton.h"
#import "UIView+Extension.h"
@implementation OutlineButton
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addTarget:self action:@selector(showOutline) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(hideOutline) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}
return self;
}
@end
感谢您的快速响应。如果还有另一种方式比为轮廓制作图像,我更喜欢这样做......您提出的实现方式可能很简单,但为每种可能的尺寸管理图像时,我的按钮最终都会变得乱七八糟。所以,这个问题中描述的另一种方法将受到高度赞赏。超级类中的自定义手势识别器是否真的搞砸了响应者链? – salocinx
你只是傻了。没有“图像”需要被“管理”。看看我的答案中的屏幕录像。这里没有使用“图像”。该矩形根据按钮的大小创建并分配在代码中。 – matt
好吧,我知道了,你的措辞有点尴尬......我根据你的回答,用正确的代码段扩展了我的问题。谢谢。 – salocinx