Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Objective c 子类化UIButton时,uicontrol状态获取值的名称是什么?_Objective C_Cocoa Touch - Fatal编程技术网

Objective c 子类化UIButton时,uicontrol状态获取值的名称是什么?

Objective c 子类化UIButton时,uicontrol状态获取值的名称是什么?,objective-c,cocoa-touch,Objective C,Cocoa Touch,我正在子类化UIButton。 但我需要知道按钮所处的状态,以绘制向上或向下按钮的颜色: - (void)drawRect:(CGRect)rect{ if (state==UIControlStateNormal) { //draw rect RED } if (state==UIControlEventTouchDown) //draw rect BLUE } } 由于state是一个属性,因此可以使用self.

我正在子类化UIButton。 但我需要知道按钮所处的状态,以绘制向上或向下按钮的颜色:

- (void)drawRect:(CGRect)rect{
     if (state==UIControlStateNormal) {
         //draw rect RED
     }
     if (state==UIControlEventTouchDown) 
         //draw rect BLUE
     }
}

由于
state
是一个属性,因此可以使用
self.state
访问它

更新:


查看下一个答案的更新。

您是否尝试查看文档

  • 后藤
  • 检查属性
  • 没什么明显的
  • 查看
    UIButton
    是否有超类
  • 转到-
    ui按钮的超类
  • 检查属性
  • 哦,看,有一个
    状态
    属性

  • 更新 公认的答案有点不正确,可能会导致一些令人烦恼的难以追踪的bug

    UIControl
    的标题将
    状态声明为

    @property(nonatomic,readonly) UIControlState state;                  // could be more than one state (e.g. disabled|selected). synthesized from other flags.
    
    现在查看如何定义
    uicontrol状态

    enum {
        UIControlStateNormal       = 0,                       
        UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
        UIControlStateDisabled     = 1 << 1,
        UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
        UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
        UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
    };
    typedef NSUInteger UIControlState;
    

    更新2 你可以画一幅图片,然后把图片作为背景

    - (void)clicked:(UIButton *)button;
    {
        UIGraphicsBeginImageContext(button.frame.size);
    
        // Draw gradient
    
        UIImage *gradient = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
    

    为了重画rect,我做了这个

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        myState = 1;
        [self setNeedsDisplay];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        myState = 0;
        [self setNeedsDisplay];
    }
    
    
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        NSLog(@"drawRect state: %d", self.state);
        NSLog(@"rect %@", NSStringFromCGRect(self.frame));
        c = UIGraphicsGetCurrentContext();
    
        if (myState==0){
        ///////// plot border /////////////
        CGContextBeginPath(c);
        CGContextSetLineWidth(c, 2.0);
        CGContextMoveToPoint(c, 1, 1);
        CGContextAddLineToPoint(c, 1, 20);
        CGContextAddLineToPoint(c, 299, 20);
        CGContextAddLineToPoint(c, 299, 1);
        CGContextSetRGBFillColor(c, 0.0, 0.0, 1.0, 1.0);
        CGContextFillPath(c);
        CGContextClosePath(c);
        CGContextStrokePath(c);
        }else{
        CGContextBeginPath(c);
        CGContextSetLineWidth(c, 2.0);
        CGContextMoveToPoint(c, 1, 20);
        CGContextAddLineToPoint(c, 1, 40);
        CGContextAddLineToPoint(c, 299, 40);
        CGContextAddLineToPoint(c, 299, 20);
        CGContextSetRGBFillColor(c, 1.0, 0.0, 1.0, 1.0);
        CGContextFillPath(c);
        CGContextClosePath(c);
        CGContextStrokePath(c); 
        }
    }
    

    我假设当按钮状态改变时调用drawRect。。。我错了。。。你知道什么函数调用drawRect来重画它吗?thxOverwrite-设置状态并在其内部调用
    [自设置需要显示]
    。但是<代码> UIButton <代码>不是一个普通的代码> UIVIEW/COD>,它包含<代码> uILabel或<代码> uIIVIEVIEW ,您可以考虑其他方式而不是<代码> DRUCTRT:< /代码>。但我不知道如何正确地做这件事。。。开始-继续-结束只提供不同州的一些图像可能会更容易…我必须根据之前的选择绘制各种阴影和其他东西。。。数学计算您可以将它们绘制到图像并将结果图像设置为按钮背景。是更多还是“if(self.state&uicontrolstatemormal){…}”实际上不起作用。UIControlStateNormal为0,因此在if语句中用任何东西与之匹配都将始终为false如果您只是更改选定和正常状态的背景,为什么不设置背景图像?或者您的实际实现是使用动态颜色吗?这只是在按钮上绘制图形的起点
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        myState = 1;
        [self setNeedsDisplay];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        myState = 0;
        [self setNeedsDisplay];
    }
    
    
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        NSLog(@"drawRect state: %d", self.state);
        NSLog(@"rect %@", NSStringFromCGRect(self.frame));
        c = UIGraphicsGetCurrentContext();
    
        if (myState==0){
        ///////// plot border /////////////
        CGContextBeginPath(c);
        CGContextSetLineWidth(c, 2.0);
        CGContextMoveToPoint(c, 1, 1);
        CGContextAddLineToPoint(c, 1, 20);
        CGContextAddLineToPoint(c, 299, 20);
        CGContextAddLineToPoint(c, 299, 1);
        CGContextSetRGBFillColor(c, 0.0, 0.0, 1.0, 1.0);
        CGContextFillPath(c);
        CGContextClosePath(c);
        CGContextStrokePath(c);
        }else{
        CGContextBeginPath(c);
        CGContextSetLineWidth(c, 2.0);
        CGContextMoveToPoint(c, 1, 20);
        CGContextAddLineToPoint(c, 1, 40);
        CGContextAddLineToPoint(c, 299, 40);
        CGContextAddLineToPoint(c, 299, 20);
        CGContextSetRGBFillColor(c, 1.0, 0.0, 1.0, 1.0);
        CGContextFillPath(c);
        CGContextClosePath(c);
        CGContextStrokePath(c); 
        }
    }