Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS7按钮边框上的选定效果_Ios_Objective C_Iphone_Ios7_Uibutton - Fatal编程技术网

iOS7按钮边框上的选定效果

iOS7按钮边框上的选定效果,ios,objective-c,iphone,ios7,uibutton,Ios,Objective C,Iphone,Ios7,Uibutton,所以我是iOS新手,我想要一些带圆形边框的按钮。我还希望这些边框在选中按钮时与按钮内的文本具有相同的效果 由于roundRect按钮不再是iOS中的对象(或者至少我找不到它,而且我读到的所有地方都说它已经不存在了),我决定编写一个扩展UIButton的自定义类。这就是我所拥有的: - (void)drawRect:(CGRect)rect{ { UIColor *blackColor = blackColor; UIColor *transBlack = [blackColor co

所以我是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)];
   }
我不确定我是否正确使用了ISSELECT。我在它下面有一个NSLog,无论我按多少次按钮,它似乎都不会被执行


任何形式的帮助和建议都将不胜感激。谢谢。

UIButton继承自UIView,因此您可以使用层的方法。。。 创建一个新的对象,子类化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方法,以确保无论在哪里设置按钮(故事板或通过其代码获得相同的init)

之后,只需将button自定义类配置为“Pressed button类”,就可以了


如果您需要更多帮助,请随时询问:)

为什么有这么多init函数?它们之间的区别是什么?我添加了touchesBegind和toucheSend功能,现在我的按钮根本无法点击。我添加了一个[super-touchesBegind:touche偶数:事件];到函数,这使我的按钮再次运行,但边框仍然没有改变。请创建类->子类化UIButton并复制代码,看看它是否适合您…,关于所有初始化方法。。。initWithFrame和init是由代码创建的按钮,InitWithCoder是由故事板创建的按钮的init方法我尝试了上面的方法,它使按钮无法单击。我添加了
[超级触摸开始:触摸事件:事件]
[超级触控已解除:触控事件:事件]
这似乎修复了按钮的功能,但按下按钮时边框仍然没有改变。