自定义 UISwitch

来源:互联网 发布:2017网络灰色赚钱项目 编辑:程序博客网 时间:2024/04/30 06:17

暂不支持 Xib 拖放的方式创建

Demo下载地址 http://download.csdn.net/download/xiaofei125145/9565323

[objc] view plaincopy



  1. //  
  2. //  CLSwitch.h  
  3. //  UISwitchFremw  
  4. //  
  5. //  Created by Wangdy on 16/7/1.  
  6. //  Copyright © 2016年 Wangdy. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface CLSwitch : UIControl  
  12.   
  13. //事件类型  UIControlEventValueChanged  
  14. @property (nonatomic,assign)BOOL isOn;  
  15.   
  16. //默认初始化方法, onImage 和 offImage 是必选参数不能为 nil,  frame.size 为无效值 ,size 根据图片size设置大小  
  17. - (instancetype)initWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage frame:(CGRect)frame;  
  18.   
  19. @end  



[objc] view plaincopy



  1. //  
  2. //  CLSwitch.m  
  3. //  UISwitchFremw  
  4. //  
  5. //  Created by Wangdy on 16/7/1.  
  6. //  Copyright © 2016年 Wangdy All rights reserved.  
  7. //  
  8.   
  9. #import "CLSwitch.h"  
  10.   
  11. @interface CLSwitch ()  
  12. @property(nonatomic,strong)UIImage *onImage;  
  13. @property(nonatomic,strong)UIImage *offImage;  
  14. @end  
  15.   
  16. @implementation CLSwitch  
  17.   
  18. - (instancetype)initWithFrame:(CGRect)frame {  
  19.     NSAssert(NO@"请使用 -initWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage frame:(CGRect)frame 初始化CLSwitch");  
  20.     return nil;  
  21. }  
  22.   
  23. - (instancetype)initWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage frame:(CGRect)frame{  
  24.     NSAssert(onImage&&offImage, @"onImage & offImage 不能为空");  
  25.     frame.size = onImage.size;  
  26.     self = [super initWithFrame:frame];  
  27.     if (self) {  
  28.         self.backgroundColor = [UIColor whiteColor];  
  29.         _onImage = onImage;  
  30.         _offImage = offImage;  
  31.         [self addTarget:self action:@selector(switchClicked) forControlEvents:UIControlEventTouchUpInside];  
  32.         self.isOn = NO;  
  33.     }  
  34.     return self;  
  35. }  
  36.   
  37. - (void)switchClicked {  
  38.     self.isOn = !self.isOn;  
  39.     [self sendActionsForControlEvents:UIControlEventValueChanged];  
  40. }  
  41.   
  42. - (void)setIsOn:(BOOL)isOn {  
  43.     _isOn = isOn;  
  44.       
  45.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contents"];  
  46.     animation.fromValue = self.layer.contents;  
  47.     animation.toValue = (id)(_isOn ? _onImage.CGImage: _offImage.CGImage);  
  48.     animation.duration = .3;  
  49.     [self.layer addAnimation: animation forKey@"animation"];  
  50.       
  51.     self.layer.contents = (id)(_isOn ? _onImage.CGImage: _offImage.CGImage);  
  52. }  
  53.   
  54. - (void)setFrame:(CGRect)frame {  
  55.     if (_onImage) {  
  56.         frame.size = _onImage.size;  
  57.     }  
  58.     super.frame = frame;  
  59. }  
  60.   
  61. - (CGSize)sizeThatFits:(CGSize)size {  
  62.     return _onImage.size;  
  63. }  
  64.   
  65. @end  
0 0
原创粉丝点击