2014-09-03 30 views
0

所以我試圖創建一個自定義按鈕類與它周圍的邊框和邊框和文字顏色更改爲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

回答

1

務必將觸摸事件傳遞給基類你做你的顏色設置工作後,例如:

[super touchesBegan: touches withEvent: event];

[super touchesEnded: touches withEvent: event];

1

您是否嘗試過在覆蓋的touchesBegan/touchesEnded中調用super

每文檔:

此方法的默認實現不執行任何操作。然而,UIResponder的立即UIKit子類,特別是UIView,將消息轉發給響應者鏈。

由於UIButton是UIView的子類,因此您可能應該調用它們。