[英]iOS7 Selected Effect on Button Border
所以我是iOS的新手,我想要一些帶有圓形邊框的按鈕。 我還希望當選擇按鈕時,這些邊框與按鈕內的文本具有相同的效果。
由於roundRect按鈕不再是iOS中的對象(或者至少我找不到它,並且在我讀過的所有地方都說不再存在),我決定編寫一個擴展UIButton的自定義類。 這就是我所擁有的:
- (void)drawRect:(CGRect)rect{
{
UIColor *blackColor = blackColor;
UIColor *transBlack = [blackColor colorWithAlphaComponent:(0.5)];
[self.layer setCornerRadius:10.0f];
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setBorderWidth:1.0];
if(self.isSelected){
[self.layer setBorderColor:(transBlack.CGColor)];
}
我不確定我是否正確使用了isSelected。 我下面有一個NSLog,無論我按幾次按鈕,它似乎都永遠不會執行。
各種幫助和建議將不勝感激。 謝謝。
UIButton繼承自UIView,因此您可以使用Layer的方法...創建一個新的對象,將該UIButton子類化為您想要的任何對象,然后實現下一個代碼:在此示例中,.m文件中有一個名為PressedButton的UIButton子類:
@implementation PressedButton
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//When the button is pressed the border color change to red
self.layer.borderColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//When the button is pressed the border color change back to black
self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
}
- (void)initialize{
self.layer.cornerRadius = 10.0f;
self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
self.layer.borderWidth = 2;
self.layer.masksToBounds = YES;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self initialize];
}
return self;
}
@end
***我實現了所有init方法,以確保無論我在何處設置按鈕(故事板或通過Code獲得相同的init)。
之后,只需將按鈕自定義類配置為“ Pressed Button類”即可
如果您需要更多幫助,請隨時詢問:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.