UIImageView在viewDidLoad上漂移

时间:2009-11-16 03:39:03

标签: iphone objective-c uiimageview

我正在以编程方式将UIImageViewUILabel添加到我正在处理的程序中的多个屏幕上。因此,我写了一个UIViewController,其中包含用于加载UIImageView中的UILabelviewDidLoad的代码,以及用于更新内容的计时器。我将此视图控制器用作需要UIImageViewUILabel的任何视图的超类。

在大多数情况下它工作正常,但是当从特定屏幕转换时,该对似乎从(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

2 个答案:

答案 0 :(得分:1)

advanceToView中设置一个断点,因为这似乎是您唯一的动画代码。每次调用它时,请查看调试器中的调用堆栈。从这里开始,您可以看到在不应该使用此代码时调用的是什么。

答案 1 :(得分:0)

经过一些实验后,我发现使用initWithFrame可以纠正漂移问题。

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

我仍然不知道问题的根本原因是什么。如果有人能告诉我这里发生了什么,我很想知道将来参考。