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

  http://blog.csdn.net/a8221379/article/details/9139487
一个月前,作者接到任务要封装自定义可拖动的控件,当时花了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

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

MyImgView.m 内容:

  1. #import "MyImgView.h"  
  2. #import <QuartzCore/CALayer.h>  
  3. @implementation MyImgView  
  4. @synthesize <span style="color: rgb(255, 0, 0);">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  
#import "MyImgView.h"
#import <QuartzCore/CALayer.h>
@implementation MyImgView
@synthesize cornerRadius;
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [[self layer] setCornerRadius:[cornerRadius floatValue]];
    [[self layer] setMasksToBounds:YES];
    NSLog(@"111111");
}

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

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

4、Run it!

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值