所以我正在尝试创建一个带有边框的自定义按钮类,边框和文本颜色更改为1/2 alpha并在按下后返回。或多或少我有这个工作,但由于某种原因它停止我的按钮推动到另一个视图。我不知道为什么。有人可以向我解释这会打破行动吗?
以下是我的自定义按钮类的代码:
- (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];
}
我的故事板中的按钮都在一个视图中。他们每个人都应该推动一个与其他人不同的新观点。我在故事板中有segue。但是,当我添加代码来更改边框颜色时,它不会执行任何操作。
答案 0 :(得分:1)
您是否尝试在覆盖的touchesBegan / touchesEnded中调用super
?
根据文档:
此方法的默认实现不执行任何操作。但是,UIResponder的UIKit子类,特别是UIView,会立即将消息转发给响应者链。
看到UIButton是UIView的子类,那么你可能应该调用它们。
答案 1 :(得分:1)
确保在进行颜色设置工作后将触摸事件传递给基类,例如:
[super touchesBegan: touches withEvent: event];
和
[super touchesEnded: touches withEvent: event];