IOS自定义控件

1 #ifndef GlobalDefine_h
2 #define GlobalDefine_h
3
4 #define SCREENWIDTH [[UIScreen mainScreen] bounds].size.width
5 #define SCREENHEIGHT [[UIScreen mainScreen] bounds].size.height
6
7 #endif /* GlobalDefine_h */
1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController
4
5
6 @end
 1 #import "ViewController.h"
 2 #import "IntegratedController.h"
 3 #import "GlobalDefine.h"
 4 #import "IntegratedModel.h"
 5
 6 @interface ViewController ()
 7
 8 {
 9     NSMutableArray *array;
10 }
11
12 @end
13
14 @implementation ViewController
15
16 - (void)initModel
17 {
18     IntegratedModel *model = [[IntegratedModel alloc] initWithImgName:@"1.png" andTitleName:@"雪花啤酒大特卖" andPrice:1.5];
19     IntegratedModel *model1 = [[IntegratedModel alloc] initWithImgName:@"2.png" andTitleName:@"风扇大特卖" andPrice:20.9];
20     IntegratedModel *model2 = [[IntegratedModel alloc] initWithImgName:@"3.png" andTitleName:@"篮球特卖" andPrice:23.2];
21     IntegratedModel *model3 = [[IntegratedModel alloc] initWithImgName:@"4.png" andTitleName:@"水杯大特卖" andPrice:15.4];
22     IntegratedModel *model4 = [[IntegratedModel alloc] initWithImgName:@"5.png" andTitleName:@"插座大特卖" andPrice:15.9];
23
24     array = [[NSMutableArray alloc] initWithObjects:model, model1, model2, model3, model4, nil];
25 }
26
27 - (void)viewDidLoad
28 {
29     [super viewDidLoad];
30     self.view.backgroundColor = [UIColor yellowColor];
31
32     [self initModel];
33
34     for (int i = 0; i < 5; ++i)
35     {
36         IntegratedController *intView = [[IntegratedController alloc] initWithFrame:CGRectMake(i % 2 * SCREENWIDTH / 2, i / 2 * SCREENHEIGHT / 3 + 20, SCREENWIDTH / 2 - 2, SCREENHEIGHT / 3)];
37         [intView setValue:array[i]];
38         [self.view addSubview:intView];
39     }
40
41 }
42
43 - (void)didReceiveMemoryWarning {
44     [super didReceiveMemoryWarning];
45     // Dispose of any resources that can be recreated.
46 }
47
48 @end
 1 #import <UIKit/UIKit.h>
 2
 3 //自定义基础控件
 4 @interface IntegratedController : UIView
 5
 6 @property (nonatomic, strong) UIImageView *imgView;
 7 @property (nonatomic, strong) UILabel *introLabel;
 8 @property (nonatomic, strong) UILabel *priceLabel;
 9
10 - (void)setValue:(id)object;
11
12 @end
 1 #import "IntegratedController.h"
 2 #import "IntegratedModel.h"
 3
 4 @implementation IntegratedController
 5
 6 //(0, 0, screenWidth / 2 - 2, screenHeight / 3)
 7
 8 - (id)initWithFrame:(CGRect)frame
 9 {
10     if (self = [super initWithFrame:frame])
11     {
12         _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height * 2 / 3)];
13         [self addSubview:_imgView];
14
15         _introLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _imgView.frame.size.height, frame.size.width, 20)];
16         //CGRectGetMaxY(_imgView.frame) = _imgView.frame.size.height
17         _introLabel.textAlignment = NSTextAlignmentCenter;
18         _introLabel.font = [UIFont systemFontOfSize:16.0];
19         _introLabel.textColor = [UIColor blackColor];
20         [self addSubview:_introLabel];
21
22         _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _imgView.frame.size.height + _introLabel.frame.size.height, frame.size.width, 20)];//_imgView.frame.size.height + _introLabel.frame.size.height = CGRectGetMaxY(_introLabel.frame)
23         _priceLabel.textAlignment = NSTextAlignmentCenter;
24         _priceLabel.textColor = [UIColor redColor];
25         _priceLabel.font = [UIFont boldSystemFontOfSize:16.0];
26         _priceLabel.textColor = [UIColor redColor];
27         [self addSubview:_priceLabel];
28     }
29
30     return self;
31 }
32
33 - (void)setValue:(id)object
34 {
35     IntegratedModel *obj = (IntegratedModel *)object;
36     _imgView.image = [UIImage imageNamed:obj.imgName];
37     _introLabel.text = obj.titleName;
38     _priceLabel.text = [NSString stringWithFormat:@"%.1f元", obj.price];
39 }
40
41 @end
 1 #import <Foundation/Foundation.h>
 2
 3 @interface IntegratedModel : NSObject
 4
 5 @property (nonatomic, strong) NSString *imgName;
 6 @property (nonatomic, strong) NSString *titleName;
 7 @property (nonatomic, assign) double price;
 8
 9 - (id)initWithImgName:(NSString *)iName andTitleName:(NSString *)tName andPrice:(double)p;
10
11 @end
#import "IntegratedModel.h"

@implementation IntegratedModel

- (id)initWithImgName:(NSString *)iName andTitleName:(NSString *)tName andPrice:(double)p
{
    if (self = [super init])
    {
        self.imgName = iName;
        self.titleName = tName;
        _price = p;
    }

    return self;
}

@end
时间: 2024-08-11 23:40:08

IOS自定义控件的相关文章

IOS自定义控件学习

IOS自定义控件 参考:http://www.oschina.net/question/262659_141737 基本概念 UIView控件只是一个矩形的空白区域,并没有任何内容.iOS应用的其他UI控件都继承了UIView,这些UI控件都是在UIView提供的空白区域上绘制外观.也就是 UIView是所有控件的基类,自定义控件要从这里派生,实现自己的绘制. 重写的方法 initWithFrame 初始化方法. initWithCoder::程序通过在nib文件中加载完该控件后会自动调用该方法

iOS 自定义控件开发

工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. #0 目标 希望可以写一个通用的图表控件(仅针对此项目),虽然开发难度增大,但是可以学习到很多知识.并且控件使用简单,可以自适应大小,支持屏幕旋转. #1 准备工作 网上各种查资料 研究了一下系统自带控件,全部基于UIView 开发过程中使用storyboard,在页面中加入一个View来控制大小,自定义控件放入此view中并且填充满,让程序可以自适应屏幕尺寸. #2 开始自定义 创建自定义控件,

iOS自定义控件教程:制作一个可重用的旋钮

当你的APP需要一些新功能时,自定义UI控件会十分有用,尤其是这些自定义控件可以在其他APP里面很好的重用.Colin Eberhart写过一篇很棒的介绍自定义UI控件的教程.这个教程涉及的是一个继承自UISlider类的自定义控件的生成:该控件的功能是给定一个(滑动)范围供(用户滑动)选择,并返回一个(与滑动位置相对应的)固定值. 本篇基于iOS 7的自定义UI教程在Colin Eberhart那篇的基础上更深入一步:受调音台旋钮的启发,这里介绍如何制作一个功能类似UISlider的圆形旋转控

iOS 自定义控件

关于UIToolbar,UINavigationController,UINavigationBar,UIBarButtonItem在ios7的使用的简单的介绍,经过搜索资料做了如下的一些汇集 ----------------------------UIBarButtonItem---------------------------- 1:  UIBarButtonItem 隐藏的方式 [self.btnPunctuation setWidth:0]; 2:  UIBarButtonItem 获

IOS 自定义控件之UIAlertview

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:这个系列的目的是写一些自定义控件的思路,并不是拿来就可以用的控件,想要直接用的控件库,去github上有的是.这个系列希望抛砖引玉,能够让读者学会如何去自定义控件,授之以渔总比授之以鱼强. 本文的内容 demo展示以及说明 如何自定义一个alertview 希望通过本文,读者能够学到 自定义控件的UI布局 自定义控件如何用代理传递事件 UIDynamic Animation 以及UIKit角度的Animation

IOS 自定义控件之UIActivityIndicatorView

原创blog,转载请注明出处 blog.csdn.net/hello_hwc 前言 这个系列的本身不是为了写一些东西让读者拿过去就直接可以用的.过段时间我会在github上传一些拿去就可以用的.这个系列的本身是希望抛砖引玉,提供一些自定义控件的思路. 本文的内容 阐述了实现自定义UIActivityIndicator的过程 希望通过本文,读者能够学会 CAShapeLayer的简单使用 CAGradientLayer的简单使用 自定义控件的一些思路 一 demo效果 二 实现的过程 用_shap

IOS 自定义控件之-显示下载过程的ImageView

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:这个系列的目的是为了提供一些思路,在Demo的过程中让读者学会一些自定义控件的思路,所以不适宜太复杂.当然,仅仅是抛砖引玉.这个控件我会上传Github,由于最近一直在搞IOT的应用,所以没时间把进行完善,有时间了我会把这个控件完善了,让读者那去直接就可以用. 完善好了我会更新下博客 Demo效果,支持两种显示过程的方式,沿着border绘制一圈和frame逐渐填充. 思路 NSURLSessionDataTas

iOS 自定义控件 progressView(环形进度条)

转帖:http://blog.csdn.net/xiangzhang321/article/details/42688133 之前做项目的时候有用到环形进度条,先是在网上找了一下第三方控件,发现好用是好用,就是东西太多了,有点复杂,还不如自己写一个简单点适合自己用的. 先把自定义控件的效果图贴出来.     其实我写的这个控件很简单.索性就直接把源码贴出来吧. .h文件的内容就是一些声明 #import <UIKit/UIKit.h> @interface ProgressView : UIV

IOS 自定义控件 跟随鼠标的圆点

1 #import <UIKit/UIKit.h> 2 3 @interface FKCustomView : UIView 4 5 @end 6 7 CustomView.m 8 #import "FKCustomView.h" 9 10 @implementation FKCustomView 11 12 //定义两个变量几率当前触碰点的坐标 13 int curX; 14 int curY; 15 -(void) touchesMoved:(NSSet<UITo