2010-06-07 38 views
0

我有一個自定義按鈕,從UIButton分類,如下初始化(顯然,所有按鈕都是使用自定義字體)。ViewDidLoad中的視圖跳轉

@implementation HWButton 

- (id)initWithCoder:(NSCoder *)decoder { 

    if (self = [super initWithCoder: decoder]) { 

    [self.titleLabel setFont:[UIFont fontWithName: @"eraserdust" size: self.titleLabel.font.pointSize]]; 
    } 

    return self; 
} 

到目前爲止好。但是,當我在我的筆尖中使用自定義類並啓動應用程序時,該按鈕最初會以小文本顯示爲細小的一秒,然後增長。所以結果是我想要的,但我不想看到過渡。任何人都可以讓我正確嗎?

謝謝。 JP

回答

0

我還沒有看到這個問題,但它聽起來像按鈕的初始框架是小的方式。當一個按鈕從筆尖加載時,它會根據筆尖中指定的框架自行繪製。在啓動和運行之後,它僅針對其他因素進行調整。

更改字體大小不是通常在初始化過程中所做的,它有很多副作用,所以類可能會忽略sizeToFit,直到按鈕完全初始化。

我認爲最簡單的解決方法就是將IB中的框架設置爲您要使用的字體的框架。這樣,你根本不應該看到轉換。

如果按鈕不需要一次繪製就更改大小,我會建議使用圖片而不是文本。只需鞭出一個Gimped按鈕並完成它。

+0

謝謝 - 我也注意到,如果我以編程方式添加(例如)一個UIButton,然後在-ViewDidLoad中應用一個轉換(稍微旋轉按鈕),該按鈕會立即出現未轉換,然後在已經顯示之後進行轉換。我現在不在我的辦公桌上,但是當我接觸到它時,我會發表一個我的意思的例子。 – 2010-06-07 21:25:09

+0

我已經改變了很多控件,我從來沒有見過。你想確保你把變換放在正確的地方,比如'viewWillAppear'而不是'viewDidAppear'。 – TechZen 2010-06-07 21:36:19

+0

對不起,最後只是一個通用的例子。將變換放在viewDidLoad中應該可以工作。 – TechZen 2010-06-07 21:37:59

0

下面是我在做什麼的例子:

在調用視圖控制器我用下面的代碼來切換視圖:

-(void)selectProfile:(User*)selectedUser{ 
    SelectGameViewController* selectGame=[[SelectGameViewController alloc]initWithNibName:@"SelectGame" bundle:nil]; 

    UIView* parent=self.view.superview; 

    [UIView beginAnimations:@"Show selection" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
    [UIView setAnimationDuration:0.50f]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:parent cache:YES]; 
    [selectGame viewWillAppear:YES]; 
    [self viewWillDisappear:YES]; 

    [parent insertSubview:selectGame.view atIndex:0]; 
    [self.view removeFromSuperview]; 

    [selectGame viewDidAppear:YES]; 
    [self viewDidDisappear:YES]; 
    [UIView commitAnimations]; 
} 

然後在出現的觀點,我有以下的代碼該-viewWillAppear方法:

-(void)viewWillAppear:(BOOL)animated{ 

    UIButton* newButton=[[UIButton alloc]initWithFrame:CGRectMake(50, 150, 500, 150)]; 
    [newButton setTitle:@"Play" forState:UIControlStateNormal]; 
    [newButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
    [newButton.titleLabel setFont:[UIFont fontWithName:@"EraserDust" size:80]]; 
    [newButton addTarget:self action:@selector(playGame:) forControlEvents:UIControlEventTouchUpInside]; 
    newButton.transform = CGAffineTransformMakeRotation(-.2); 
    [self.view addSubview:newButton]; 
    [newButton release]; 


    [super viewWillAppear:animated]; 
} 

這樣做的結果是,認爲出現與不旋轉的按鈕,但後來它的顯示後立即轉動。我很困惑,因爲這似乎與TechZen的建議不一致?

相關問題