一、 方式一:
1.拿到一个格子视图
AppView *appOldView = [AppView loadNib];
// 赋值
AppViewModel *appViewModel = self.apps[i];
appOldView.appViewModel = appViewModel;
2. appView.h
#import <UIKit/UIKit.h>
@class AppViewModel;
@interface appView : UIView
@property(nonatomic,strong)AppViewModel *appViewModel;
+ (id)loadNib;
@end
3. appView.m
#import "appView.h"
#import "AppViewModel.h"
@interface appView()
@property (weak, nonatomic) IBOutlet UIImageView *head;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@end
@implementation appView
//重写模型的set方法
- (void)setAppViewModel:(AppViewModel *)appViewModel{
_appViewModel = appViewModel;
self.head.image = [UIImage imageNamed:appViewModel.icon];
self.nameLabel.text = appViewModel.name;
}
+ (id )loadNib{
return [[NSBundle mainBundle]loadNibNamed:@"appView" owner:nil options:nil][0];
}
@end
二、自定义UIView(xib方式)
eg.
CZWheelView.h
#import <UIKit/UIKit.h>
@interface CZWheelView : UIView
+ (instancetype)wheelView;
@end
CZWheelView.m
#import "CZWheelView.h"
#import "UIView+Ex.h"
@interface CZWheelView ()
//背景图片
@property (weak, nonatomic) IBOutlet UIImageView *wheelView;
@property (nonatomic, weak) UIButton *preButton;
@end
@implementation CZWheelView
+ (instancetype)wheelView{
return [[[NSBundle mainBundle] loadNibNamed:@"CZWheelView" owner:nil options:nil] lastObject];
}
//点击开始按钮
- (IBAction)startClick:(UIButton *)sender {
}
//1 生成12个按钮
- (void)awakeFromNib{
int count = 12; //12个按钮
for (int i = 0; i < count; i++) {
//创建按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.wheelView addSubview:btn];
//设置按钮选中的背景图片
UIImage *selBackImg = [UIImage imageNamed:@"LuckyRototeSelected"];
[btn setBackgroundImage:selBackImg forState:UIControlStateSelected];
//设置按钮的大小
btn.width = selBackImg.size.width;
btn.height = selBackImg.size.height;
btn.center = self.wheelView.center;
//旋转图片
btn.layer.anchorPoint = CGPointMake(0.5, 1);
btn.transform = CGAffineTransformMakeRotation(i * M_PI*2/count);
//点击按钮
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//
self.wheelView.userInteractionEnabled = YES;
}
}
- (void)btnClick:(UIButton *)sender{
self.preButton.selected = NO;
sender.selected = YES;
self.preButton = sender;
}
@end