iOS 7自定义按钮损坏的行动/插座

问题描述:

所以我试图创建一个自定义按钮类与它周围的边框和边框和文字颜色更改为1/2阿尔法和回来一次按下。或多或少我有这个工作,但由于某种原因,它阻止我的按钮推到另一个视图。我不知道为什么。有人能向我解释这将如何破坏行动吗?iOS 7自定义按钮损坏的行动/插座

这是我为我的自定义按钮代码:

- (void)initialize{ 
    self.layer.cornerRadius = 10.f; 
    self.layer.borderColor = [UIColor blackColor].CGColor; 
    self.layer.borderWidth = 1.0; 
    self.layer.masksToBounds = YES; 
} 

- (instancetype)init{ 
    self = [super init]; 
    if(self){ 
     [self initialize]; 
    } 
    return self; 
} 

- (id)initWithFrame:(CGRect)frame{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self initialize]; 
    } 
    return self; 
} 

- (instancetype)initWithCoder:(NSCoder *)coder{ 
    self = [super initWithCoder:coder]; 
    if (self) { 
     [self initialize]; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    // When the button is pressed the border color chage to 1/2 alpha 
    UIColor *transBlack = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; 
    [self.layer setBorderColor: transBlack.CGColor]; 
    //EDIT based on solution 
    [super touchesBegan:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    // When the button is pressed the border color changes back to black 
    [self.layer setBorderColor:[UIColor blackColor ].CGColor]; 
    //EDIT based on solution 
    [super touchesEnded:touches withEvent:event]; 
} 

我有我的故事板的按钮都在一个视图。他们每个人都应该推出一个新的观点,与其他观点不同。我在故事板中有继续。但是,当我添加代码来更改边框颜色时,它不会执行任何操作。

+2

你有没有打过电话[超级touchesBegan:触及事件:事件];和[超级touchesEnded:触及事件:事件];除了您的自定义边框代码之外,还有这两种方法吗? – sfeuerstein 2014-09-03 20:58:24

+0

我只是厌倦了它似乎解决了这个问题。谢谢。 – CGTheLegend 2014-09-03 22:04:02

务必将触摸事件传递给基类你做你的颜色设置工作后,例如:

[super touchesBegan: touches withEvent: event];

[super touchesEnded: touches withEvent: event];

您是否尝试过在覆盖的touchesBegan/touchesEnded中调用super

每文档:

此方法的默认实现不执行任何操作。然而,UIResponder的立即UIKit子类,特别是UIView,将消息转发给响应者链。

由于UIButton是UIView的子类,因此您可能应该调用它们。