iOS 自定义UIView使用示例

一、 方式一:

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


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,307评论 30 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • 掌握 UIView的常见属性和方法 九宫格计算方法 字典转模型 Xib的使用 自定义view(view的封装) 简...
    JonesCxy阅读 5,208评论 1 3
  • 也许,我们生活中,都会碰到过朋友向你借钱的经历吧。其实,在自己能力允许的情况下,借钱给身边需要帮助的人,也是一...
    orget阅读 4,556评论 0 1
  • 今天我打完羽毛球,回家吃晚饭饭的时候就七点半了。他娘俩早就吃完饭洗澡完了。我就拉过诺子来陪吃。吃饭期间,他说:“爸...
    橘子郡的天空阅读 1,378评论 0 1