6-2 老师,我已经按照你的改正了,还是没有显示出硬币和金额...

来源:6-2 作业题

SiuNam

2018-10-12 09:55:21

//
//  ViewController.m
//  看图猜字游戏
//
//  Created by  on 2018/10/9.
//  Copyright © 2018年 lzn. All rights reserved.
//

#import "ViewController.h"
#import "DiyCoinView.h"
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addUpView];
    //[self addcoinView];
}

-(void)addUpView{
    UIView *UpView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 390)];
    UpView.backgroundColor = [UIColor colorWithRed:178/255.0 green:34/255.0 blue:34/255.0 alpha:1];
    [self.view addSubview:UpView];
    
    DiyCoinView *coin = [[DiyCoinView alloc]init];
    coin.backgroundColor = UIColor.clearColor;
    coin.coinImageView.image = [UIImage imageNamed:@"coin.png"];
    coin.money.text = @"20000";
    coin.money.textColor = [UIColor blackColor];
    [self.view addSubview:coin];
}

/*
//硬币图片加个数
-(void)addcoinView{
    DiyCoinView *coin = [[DiyCoinView alloc]initWithFrame:CGRectMake(244, 20, 131, 30)];
    coin.backgroundColor = UIColor.clearColor;
    coin.coinImageView.image = [UIImage imageNamed:@"coin.png"];
    coin.money.text = @"20000";
    [self.view addSubview:coin];
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
//
//  DiyCoinView.h
//  看图猜字游戏
//
//  Created by  on 2018/10/12.
//  Copyright © 2018年 lzn. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DiyCoinView : UIView
@property(nonatomic,weak) UIImageView *coinImageView;
@property(nonatomic,weak) UILabel *money;
@end
//
//  DiyCoinView.m
//  看图猜字游戏
//
//  Created by  on 2018/10/12.
//  Copyright © 2018年 lzn. All rights reserved.
//

#import "DiyCoinView.h"

@implementation DiyCoinView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        UIImageView *coinImageView = [[UIImageView alloc]init];
        self.coinImageView = coinImageView;
        [self addSubview:coinImageView];
        
        UILabel *money = [[UILabel alloc]init];
        money.textAlignment = NSTextAlignmentCenter;
        
        self.money = money;
        [self addSubview:money];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat coinImageViewX = 244;
    CGFloat coinImageViewY = 20;
    CGFloat coinImageViewW = 30;
    CGFloat coinImageViewH = 30;
    self.coinImageView.frame = CGRectMake(coinImageViewX, coinImageViewY, coinImageViewW, coinImageViewH);
    
    CGFloat moneyLabelX = 244+coinImageViewX;
    CGFloat moneyLabelY = 20;
    CGFloat moneyLabelW = 30;
    CGFloat moneyLabelH = 30;
    self.money.frame = CGRectMake(moneyLabelX, moneyLabelY, moneyLabelW, moneyLabelH);
}

@end

http://img.mukewang.com/climg/5bbfff0400015df808681608.jpg

写回答

1回答

Tender10

2018-10-12

根据你的代码我帮你重新调整了一下,你可以参考如下代码哦:

// DiyCoinView.h
#import <UIKit/UIKit.h>
#import "CustomModel.h"
@interface DiyCoinView : UIView
@property(nonatomic,weak) UIImageView *coinImageView;
@property(nonatomic,weak) UILabel *money;
@property (nonatomic, strong) CustomModel *model;
@end

// DiyCoinView.m
#import "DiyCoinView.h"
#import "CustomModel.h"
@implementation DiyCoinView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        UIImageView *coinImageView = [[UIImageView alloc]init];
        self.coinImageView = coinImageView;
        [self addSubview:coinImageView];
        
        UILabel *money = [[UILabel alloc]init];
        money.textAlignment = NSTextAlignmentCenter;
        
        self.money = money;
        [self addSubview:money];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat coinImageViewX = self.frame.origin.x;
    CGFloat coinImageViewY = self.frame.origin.y;
    CGFloat coinImageViewW = 30;
    CGFloat coinImageViewH = self.frame.size.height;
    self.coinImageView.frame = CGRectMake(coinImageViewX, coinImageViewY, coinImageViewW, coinImageViewH);
    
    CGFloat moneyLabelX = self.frame.origin.x+40;
    CGFloat moneyLabelY = self.frame.origin.y;
    CGFloat moneyLabelW = self.frame.size.width-40;
    CGFloat moneyLabelH = self.frame.size.height;
    self.money.frame = CGRectMake(moneyLabelX, moneyLabelY, moneyLabelW, moneyLabelH);
}

- (void)setModel:(CustomModel *)model
{
    _model = model;
    
    self.coinImageView.image = [UIImage imageNamed:model.icon];
    self.money.text = model.name;
    
}

@end


//  CustomModel.h
#import <Foundation/Foundation.h>

@interface CustomModel : NSObject
/**
 *  金钱
 */
@property (nonatomic, copy) NSString *name;
/**
 *  图片
 */
@property (nonatomic, copy) NSString *icon;

+ (instancetype)modelWithName:(NSString *)name icon:(NSString *)icon;
- (instancetype)initWithName:(NSString *)name icon:(NSString *)icon;

@end

//  CustomModel.m
#import "CustomModel.h"

@implementation CustomModel
+ (instancetype)modelWithName:(NSString *)name icon:(NSString *)icon
{
    return [[self alloc] initWithName:name icon:icon];
}

- (instancetype)initWithName:(NSString *)name icon:(NSString *)icon
{
    if (self = [super init]) {
        self.name = name;
        self.icon = icon;
    }
    return self;
}

@end

// ViewController.m
#import "ViewController.h"
#import "DiyCoinView.h"
#import "CustomModel.h"
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addUpView];
    //[self addcoinView];
}

-(void)addUpView{
    
    
    // 创建自定义的View
    DiyCoinView *customView = [[DiyCoinView alloc] init];
    // 设置数据
    CustomModel *model = [CustomModel modelWithName:@"hosea_zhou" icon:@"1"];
    customView.model = model;
    // 设置frame
    customView.frame = CGRectMake(100, 100, 100, 100);
    // 添加子控件
    [self.view addSubview:customView];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

上述就是实现的代码哦,位置我没有仔细给你调整,你可以自己再设置一个比较好的位置,调整一下就好。如果还有什么不明白的地方,可以在问答里继续提问,老师都会帮助解决。祝学习愉快~

0

0 学习 · 1878 问题

查看课程