2011-11-11 37 views
1

我有一個子類UIButton在子類UITableViewCell內。該按鈕從筆尖加載。CALayer沒有渲染或繪圖

我有一個圖像我想用作按鈕的圖像。我想用CALayer來更好地控制動畫。當用戶點擊按鈕時,會出現動畫。但是,我甚至無法將圖像展現出來。

QuartzCore是導入的。

我的子類UIButton(總是從筆尖加載)代碼:

-(void)awakeFromNib { 
    [super awakeFromNib]; 
    self.clipsToBounds = NO; 
    self.backgroundColor = [UIColor clearColor]; 
    self.opaque = NO; 
    self.userInteractionEnabled = YES; 
} 

碼錶視圖控制器:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyCustomCell *cell = (BLCustomCellCenter*)[tableView dequeueReusableCellWithIdentifier:@"customCellID"]; 
    if (cell == nil) { 
     // ... cell is initialized 
    } 

    // configure the image for the button 
    // 
    // Note: there is an outlet to the UIButton called 'customButton' 

    CALayer *imgLayer = [CALayer layer]; 
    imgLayer.bounds = CGRectMake(0, 0, cell.customButton.bounds.size.width, cell.customButton.bounds.size.height); 
    imgLayer.position = CGPointMake(cell.customButton.bounds.size.width/2, cell.customButton.bounds.size.height/2); 
    UIImage *img = [UIImage imageNamed:@"someImage"]; 
    imgLayer.contents = (id)[img CGImage]; 
    [cell.customButton.layer addSublayer:imgLayer]; 

    // ... configure subviews of 'customButton' 

    return cell; 
} 

任何幫助非常感謝,一如既往。

回答

1

經過幾個小時的調試才發現它。事情(也許我忘了提及)是自定義UITableViewCell從筆尖加載。我想查看的子視圖也是從筆尖加載的。所以,這是一個筆尖內的筆尖。

this wonderful article,覆蓋awakeAfterUsingCoder:裏面的子視圖的類做的伎倆。加載父級筆尖時,超級的initUsingCoder:在僅加載佔位符對象的子視圖上調用。

這「佔位」對象引起了我的問題,因爲我是操縱佔位符代替實際的對象我想要的。

因此,您必須從筆尖加載並初始化子視圖的對象,然後重寫NSObjectawakeAfterUsingCoder:

0

設置內容後嘗試添加[imgLayer setNeedsDisplay];

+0

沒有,沒有這樣的運氣。 –

+0

你是否證實img不是零? –

+1

它不是零。 NSLog給了我反饋,比如「」。 –