【深入浅出IOS开发】彩票-重写按钮控件

①创建子类继承UIButton,然后关联相应的UIButton控件

②重写initWithCoder 和 initWithFrame里面设置同样的代码,确保不管是通过代码还是xib,storyborad来创建Button都会执行

③重写titleRectForContentRect和imgeRectForContentRect

[objc]  view plain copy
  1. #import "MJTitleButton.h"  
  2. #import <Availability.h>  
  3.   
  4. @interface MJTitleButton()  
  5. @property (nonatomicstrongUIFont *titleFont;  
  6. @end  
  7.   
  8. // initWithCoder  --->  awakeFromNib  
  9. @implementation MJTitleButton  
  10.   
  11. /** 
  12.  *  从文件中解析一个对象的时候就会调用这个方法 
  13.  */  
  14. - (id)initWithCoder:(NSCoder *)decoder  
  15. {  
  16.     if (self = [super initWithCoder:decoder]) {  
  17.         [self setup];  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. /** 
  23.  *  通过代码创建控件的时候就会调用 
  24.  */  
  25. - (id)initWithFrame:(CGRect)frame  
  26. {  
  27.     if (self = [super initWithFrame:frame]) {  
  28.         [self setup];  
  29.     }  
  30.     return self;  
  31. }  
  32.   
  33. /** 
  34.  *  初始化 
  35.  */  
  36. - (void)setup  
  37. {  
  38.     self.titleFont = [UIFont systemFontOfSize:14];  
  39.     self.titleLabel.font = self.titleFont;  
  40.       
  41.     // 图标居中  
  42.     self.imageView.contentMode = UIViewContentModeCenter;  
  43. }  
  44.   
  45.   
  46. /** 
  47.  *  控制器内部label的frame 
  48.  *  contentRect : 按钮自己的边框 
  49.  */  
  50. - (CGRect)titleRectForContentRect:(CGRect)contentRect  
  51. {  
  52.     CGFloat titleX = 0;  
  53.     CGFloat titleY = 0;  
  54.     NSDictionary *attrs = @{NSFontAttributeName : self.titleFont};  
  55.     CGFloat titleW;  
  56.       
  57.     if (iOS7) {  
  58.         // 只有Xcode5才会编译这段代码  
  59. #ifdef __IPHONE_7_0  
  60.         titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;  
  61. #else  
  62.         titleW = [self.currentTitle sizeWithFont:self.titleFont].width;  
  63. #endif  
  64.     } else {  
  65.         titleW = [self.currentTitle sizeWithFont:self.titleFont].width;  
  66.     }  
  67.     CGFloat titleH = contentRect.size.height;  
  68.     return CGRectMake(titleX, titleY, titleW, titleH);  
  69. }  
  70.   
  71. /** 
  72.  *  控制器内部imageView的frame 
  73.  */  
  74. - (CGRect)imageRectForContentRect:(CGRect)contentRect  
  75. {  
  76.     CGFloat imageW = 30;  
  77.     CGFloat imageX = contentRect.size.width - imageW;  
  78.     CGFloat imageY = 0;  
  79.     CGFloat imageH = contentRect.size.height;  
  80.     return CGRectMake(imageX, imageY, imageW, imageH);  
  81. }  
  82.   
  83. @end  


在Button所在的控制器中,连线实现淡季事件

[objc]  view plain copy
  1. - (IBAction)titleClick:(UIButton *)sender {  
  2.     // 1.按钮旋转  
  3.     [UIView animateWithDuration:0.25 animations:^{  
  4.         sender.imageView.transform = CGAffineTransformMakeRotation(-M_PI);  
  5.     }];  
  6.       
  7.     // 2.添加uiview  
  8.     UIView *temp  = [[UIView alloc] init];  
  9.     temp.frame = CGRectMake(101010030);  
  10.     temp.backgroundColor = [UIColor redColor];  
  11.     [self.view addSubview:temp];  
  12. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值