简体   繁体   English

iOS7在按钮边框上选择效果

[英]iOS7 Selected Effect on Button Border

So I am new to iOS and I want some buttons with rounded boarders. 所以我是iOS的新手,我想要一些带有圆形边框的按钮。 I also want those borders to have the same effect as the text within the button when the button is selected. 我还希望当选择按钮时,这些边框与按钮内的文本具有相同的效果。

Since roundRect buttons are no longer an object in iOS (or at least I couldn't find it and everywhere I read said that is was no more) I decided to write a custom class that extends UIButton. 由于roundRect按钮不再是iOS中的对象(或者至少我找不到它,并且在我读过的所有地方都说不再存在),我决定编写一个扩展UIButton的自定义类。 This is what I have: 这就是我所拥有的:

- (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)];
   }

I'm not sure if I am using isSelected properly. 我不确定我是否正确使用了isSelected。 I had an NSLog under it and it never seems to be executed no matter how many times I press the buttons. 我下面有一个NSLog,无论我按几次按钮,它似乎都永远不会执行。

Any help and suggestions of all kinds would be greatly appreciated. 各种帮助和建议将不胜感激。 Thank you. 谢谢。

UIButton inherit from UIView, So you can use Layer's Methods... Create new Object that Subclassing UIButton call it whatever you want, then implement the next Code: In this sample i have UIButton subclass called PressedButton in the .m file: 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

*** I implemented the all init methods to be sure no matter where i set the button (storyboard or via Code its get the same init). ***我实现了所有init方法,以确保无论我在何处设置按钮(故事板或通过Code获得相同的init)。

After that just configure your button custom class to your "Pressed Button class" and that's it 之后,只需将按钮自定义类配置为“ Pressed Button类”即可 在此处输入图片说明

If you need more help be free to ask :) 如果您需要更多帮助,请随时询问:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM