0

アプリ ユーザーに情報を表示するために、いくつかの異なる UIView サブクラスを構築しようとしています。私が抱えている問題の 1 つは、ビューを複数回呼び出すことができることです。

テスト呼び出しは次のとおりです。

if(rewardsView==nil){
    rewardsView = [[RewardsView alloc] init];
    [rewardsView showRewardType:RewardsAttack withXP:100 withZBucks:10 isCritical:NO];
}else{
    [rewardsView showRewardType:RewardsAttack withXP:200 withZBucks:20 isCritical:NO];
}

初期化して表示するコードは次のとおりです。

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code
        self.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);

        //Z-Bucks
        zBucksIcon = [UIImage imageNamed:@"rewards-z-bucks"];
        zBucksView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
        [zBucksView setImage:zBucksIcon];
        zBucksView.alpha = 0;

        zBucksLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, zBucksIcon.size.height, zBucksIcon.size.width,25)];
        zBucksLabel.backgroundColor = [UIColor clearColor];
        zBucksLabel.textAlignment = NSTextAlignmentCenter;
        zBucksLabel.font = [UIFont fontWithName:@"Open Sans Condensed" size:24];
        zBucksLabel.textColor = [UIColor whiteColor];
        zBucksLabel.shadowColor = [UIColor blackColor];
        zBucksLabel.shadowOffset = CGSizeMake(1, 1);
        [zBucksView addSubview:zBucksLabel];

        //XP
        xpIcon = [UIImage imageNamed:@"rewards-xp"];
        xpView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
        [xpView setImage:xpIcon];

        xpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, xpIcon.size.height, xpIcon.size.width,25)];
        xpLabel.backgroundColor = [UIColor clearColor];
        xpLabel.textAlignment = NSTextAlignmentCenter;
        xpLabel.font = [UIFont fontWithName:@"Open Sans Condensed" size:24];
        xpLabel.textColor = [UIColor blackColor];
        xpLabel.shadowColor = [UIColor whiteColor];
        xpLabel.shadowOffset = CGSizeMake(1, 1);
        [xpView addSubview:xpLabel];
        xpView.alpha = 0;

        [self addSubview:zBucksView];
        [self addSubview:xpView];
    }
    return self;
}

-(void)showRewardType:(RewardsType)type withXP:(int)xp withZBucks:(int)zBucks isCritical:(BOOL)critical{

    //Set Values
    zBucksLabel.text = [NSString stringWithFormat:@"+%i",zBucks];
    xpLabel.text = [NSString stringWithFormat:@"+%i",xp];

    if(type==RewardsAttack){

    }
    if(type==RewardsHeal){

    }
    if(type==RewardsDaily){

    }

    UIWindow *window = [appDelegate window];
    [window addSubview:self];
    [self animateRewards];
}

これは閉じるコードです (いくつかのアニメーション関数が呼び出された後、ここには示されていません):

-(void)dismissRewards{
    [self removeFromSuperview];
    //Reset Views
    zBucksView.alpha = 0;
    xpView.alpha = 0;
    [zBucksView setFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
    [xpView setFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
}
4

0 に答える 0