只要用户按下手指,iOS8 +应用程序中的按钮就会在按钮周围绘制轮廓以做出反应。目的是将这种行为封装到OutlineButton类中(在类层次结构下方为cp)。松开手指时,应用程序应执行已定义的操作(主要是对另一个视图控制器执行搜索)。这是我目前用于此目的的类层次结构:

 - 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

最佳答案

只要用户将手指按在iOS8 +应用程序中的按钮上,它们就会在按钮周围绘制轮廓


最符合框架要求的实现方式是,对于突出显示的状态,将具有轮廓的图像分配给按钮。当按钮被按下时,它被高亮显示。因此,只有在按下按钮时,它才会显示轮廓。

ios - iOS:如何正确实现父类(super class)中的自定义手势识别器?-LMLPHP

10-07 19:54
查看更多