2009-11-16 55 views
1

我正在編程添加一個UIImageView和一個UILabel到我正在處理的程序中的多個屏幕。因此,我寫了一個UIViewController,其中包含加載UIImageViewUILabel以及viewDidLoad的代碼以及更新內容的計時器。我使用這個視圖控制器作爲任何需要UIImageViewUILabel的視圖的超類。UIImageView在viewDidLoad上漂流

大多數情況下,它可以正常工作,但是從特定屏幕轉換時,該對視圖似乎以(0,0,0,0)幀開始,並且緩慢地對我定義的幀進行動畫處理。我不知道這個問題是甚麼,甚至在哪裏尋找。任何指針都會很棒。

這是我用作超類的代碼。

FoundationViewController.h

#import 

@interface FoundationViewController : UIViewController { 
    IBOutlet UIImageView *activeInventoryImage; 
    IBOutlet UILabel *activeInventoryLabel; 
    NSTimer *inventoryStatusTimer; 
} 

@property (nonatomic, retain) IBOutlet UIImageView *activeInventoryImage; 
@property (nonatomic, retain) IBOutlet UILabel *activeInventoryLabel; 

- (void) advanceToView:(id)nextView; 
- (void) inventoryStatus; 
- (BOOL) inventoryActive:(NSDate*)inventoryExpire withImage:(NSString*)imageName; 
@end 

FoundationViewController.m

#import "FoundationViewController.h" 
#import "GameData.h" 
#import "Globals.h" 


@implementation FoundationViewController 

@synthesize activeInventoryImage; 
@synthesize activeInventoryLabel; 

- (void) viewDidLoad{ 

    // initialize the active inventory icon and label 
    activeInventoryImage = [[UIImageView alloc] init]; 
    activeInventoryLabel = [[UILabel alloc] init]; 

    // clear 
    activeInventoryImage.image = [UIImage imageNamed:@""]; 
    activeInventoryLabel.text = @""; 

    // set the center points 
    activeInventoryImage.frame = CGRectMake(258, 7, 20, 20); 
    activeInventoryLabel.frame = CGRectMake(280, 6, 30, 20); 

    // set label parameters 
    activeInventoryLabel.backgroundColor = [UIColor clearColor]; 
    activeInventoryLabel.font = [UIFont fontWithName:@"Helvetica" size:11.0f]; 
    activeInventoryLabel.textColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.6 alpha:1]; 

    // add to view 
    [self.view addSubview:activeInventoryImage]; 
    [self.view addSubview:activeInventoryLabel]; 

    // start the timer if there is any active inventory 
    if(
     [self inventoryActive:[GameData sharedGameData].player.inventory.aExpire withImage:@"a.png"] || 
     [self inventoryActive:[GameData sharedGameData].player.inventory.bExpire withImage:@"b.png"] || 
     [self inventoryActive:[GameData sharedGameData].player.inventory.cExpire withImage:@"c.png"] 
     ) 
     inventoryStatusTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(inventoryStatus) userInfo:nil repeats:YES]; 

    [super viewDidLoad]; 
} 

- (BOOL) inventoryActive:(NSDate*)inventoryExpire withImage:(NSString*)imageName{ 
    NSTimeInterval expireSeconds; 
    NSDateFormatter *formatter; 
    BOOL runTimer = FALSE; 

    // expire 
    expireSeconds = [inventoryExpire timeIntervalSinceNow]; 
    if(expireSeconds > 0){ 
     formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateFormat:@"mm:ss"]; 
     NSDate *expireTime = [NSDate dateWithTimeIntervalSince1970:expireSeconds]; 
     activeInventoryImage.image = [UIImage imageNamed:imageName]; 
     activeInventoryLabel.text = [formatter stringFromDate:expireTime]; 
     [formatter release]; 
     runTimer = TRUE; 
    } 

    return runTimer; 
} 

- (void) inventoryStatus{ 

    // if there is no active inventory kill the timer 
    if(
     ![self inventoryActive:[GameData sharedGameData].player.inventory.aExpire withImage:@"a.png"] && 
     ![self inventoryActive:[GameData sharedGameData].player.inventory.bExpire withImage:@"b.png"] && 
     ![self inventoryActive:[GameData sharedGameData].player.inventory.cExpire withImage:@"c.png"] 
     ){ 
     activeInventoryImage.image = [UIImage imageNamed:@""]; 
     activeInventoryLabel.text = @""; 
     if(inventoryStatusTimer != nil) 
      [inventoryStatusTimer invalidate]; 
    } 

} 

- (void) advanceToView:(id)nextView{ 
    if([nextView isKindOfClass:[UIView class]]){ 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:2.7]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES]; 
     [self.view addSubview:nextView]; 
     [UIView commitAnimations]; 
    } 
    else 
     NSLog(@" Error Loading View: %@",nextView); 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [activeInventoryImage removeFromSuperview]; 
    [activeInventoryImage release]; 
    [activeInventoryLabel release]; 
    [super dealloc]; 
} 


@end 

回答

0

經過一番嘗試,我發現使用initWithFrame可以糾正漂移問題。

activeInventoryImage = [[UIImageView alloc] initWithFrame:CGRectMake(258, 7, 20, 20)]; 
activeInventoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(280, 6, 30, 20)]; 

我仍然不知道問題的根源是什麼。如果有人能告訴我這裏發生了什麼,我很想知道以備參考。

1

把一個破發點中advanceToView,因爲這似乎是你唯一的動畫代碼。每次調用時,都要查看調試器中的調用堆棧。從這裏,你可以看到當這些代碼不應該是什麼時候調用這個代碼。

+0

我在advanceToView中放下了一個斷點,並確認它正在按照預期調用(僅在按下按鈕時發起的一次屏幕更改)。我也註釋掉了例程中的所有動畫代碼,只留下了[self.vew addSubView:nextView];我仍然在viewControllers的一小部分中看到這種行爲。我確實注意到,當我通過調試器時,動畫不存在。然而,當我沒有破發點的時候。我仍然難倒。這裏有什麼指標? – 2009-11-16 18:09:29