English 中文(简体)
UIImageView drifting on viewDidLoad
原标题:

I am programmatically adding a UIImageView and a UILabel to multiple screens in a program I m working on. As such, I ve written a UIViewController that contains the code to load up the UIImageView and UILabel in viewDidLoad, along with a timer to update the contents. I use this view controller as a superclass for any views that need the UIImageView and UILabel.

For the most part it works fine, but when transitioning from specific screens the pair seem to start with a frame of (0,0,0,0) and slowly animate to the frame I ve defined. I have no idea what the issue is or even where to look. Any pointers here would be great.

Here is the code I m using as my superclass.

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
最佳回答

After some experimenting I ve discovered using initWithFrame corrects the drifting problem.

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

I still have no idea what the root cause of the issue is. If anyone could tell me what s going on here I would love to know for future reference.

问题回答

Put a break point in advanceToView as this seems to be your only animation code. Everytime it gets called, look at the call stack in the debugger. From here, you can see what is calling this code when they are not supposed to be.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签