现在的位置: 首页 > 综合 > 正文

谁说ios不可以自定义控件?(可用xib拖动的)

2013年01月26日 ⁄ 综合 ⁄ 共 1174字 ⁄ 字号 评论关闭

一个月前,作者接到任务要封装自定义可拖动的控件,当时花了1秒的时间浏览大脑做过的所有程序(包括网络上的Demo)没有发现有此类控件的封装啊?在Xcode的控件表里,确实有一个是“Custom Objects”,但是后来发现只有4.0以前版本的Xcode才能支持这个东西,它被apple抛弃了,这个是官方的解释:

http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_1.html

(见最后一段)。

但是两天前,作者发现在“Indentity Inspector”导航栏下,有一个很可疑的参数——“Custom Class”它的下面还有一个更可疑的参数“User Defined Runtime Attributes”见下图

既然Apple给出了“Custom Class”,那么就意味了我们可以用自己定制的类,我是我就实验了一下,结果,果断成功!下面就给大家介绍具体的步骤:

1、首先要建立一个类:MyImgView继承UIButton

[plain] view
plain
copy

  1. #import <Foundation/Foundation.h>  
  2. @interface MyImgView : UIButton  
  3. //对外接口  
  4. @property NSNumber *cornerRadius;  
  5. @end  


MyImgView.m 内容:

[plain] view
plain
copy

  1. #import "MyImgView.h"  
  2. #import <QuartzCore/CALayer.h>  
  3. @implementation MyImgView  
  4. @synthesize <span style="color:#ff0000;">cornerRadius</span>;  
  5. - (void)drawRect:(CGRect)rect  
  6. {  
  7.     // Drawing code  
  8.     [[self layer] setCornerRadius:[cornerRadius floatValue]];  
  9.     [[self layer] setMasksToBounds:YES];  
  10.     NSLog(@"111111");  
  11. }  
  12.   
  13. @end  

2、将一个button控件拖入xib,设置一张按钮图片,如下图:

3、设置此按钮的“Custom Class”和“User Defined Runtime Attributes”,如下图:

4、Run it!

本文适合懒人一族,如有纰漏,请多指正!

转自:http://blog.csdn.net/a8221379/article/details/9139487

抱歉!评论已关闭.