2012-03-04 30 views

回答

5

你甚至嘗試在看文檔?

  1. 轉到UIButton Reference
  2. 檢查屬性
  3. 沒有明顯
  4. 看看UIButton有超
  5. 轉到UIControl Reference - UIButton的超
  6. 檢查屬性
  7. 哦,看有是state財產

更新

接受的答案是不正確略,並可能導致一些煩人的臭蟲很難追查。

UIControl頭聲明state作爲

@property(nonatomic,readonly) UIControlState state;     // could be more than one state (e.g. disabled|selected). synthesized from other flags. 

現在擡頭看怎麼UIControlState定義,我們看到

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; 

因此當你正在處理一個位掩碼您應檢查適當地說,例如

if (self.state & UIControlStateNormal) { ... } 

更新2

你可以通過繪製成圖像,然後設置圖像爲背景,例如做

- (void)clicked:(UIButton *)button; 
{ 
    UIGraphicsBeginImageContext(button.frame.size); 

    // Draw gradient 

    UIImage *gradient = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    [button setBackgroundImage:image forState:UIControlStateNormal]; 
} 
+0

好吧,我能夠通過(跟蹤觸摸和重繪控制)得到狀態......但我不知道如何正確地做到這一點......開始繼續結束 – jdl 2012-03-04 03:24:30

+0

這最有可能是容易,只需提供一些圖片的不同狀態... – 2012-03-04 03:30:26

+0

我要繪製各種色調和,並根據先前的選擇的東西...數學計算 – jdl 2012-03-04 03:34:53

1

由於state是一個屬性,你可以使用self.state訪問它。

更新:

查看下一個答案的更新。

+0

我的假設是,當按鈕狀態發生變化......我是錯的drawRect被稱爲......你知道是什麼函數調用drawRect來重繪它? thx – jdl 2012-03-04 02:55:33

+1

覆蓋-setState並在其中調用'[self setNeedsDisplay]'。但是'UIButton'不是一個普通的'UIView',它包含了'UILabel'或'UIImageView',可能是你應該考慮其他的方式比'的drawRect:'。 – cxa 2012-03-04 03:16:58

0

重新繪製矩形,我這樣做...

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

如果你只是改變選擇和正常狀態的背景爲什麼不設置背景圖像?或者是您使用動態顏色的實際實現? – 2012-03-04 11:55:14

+0

這只是在按鈕上繪製圖形的起點 – jdl 2012-03-04 15:29:37

相關問題