自定义 UISwitch
来源:互联网 发布:2017网络灰色赚钱项目 编辑:程序博客网 时间:2024/04/30 06:17
暂不支持 Xib 拖放的方式创建
Demo下载地址 http://download.csdn.net/download/xiaofei125145/9565323
[objc] view plaincopy
- //
- // CLSwitch.h
- // UISwitchFremw
- //
- // Created by Wangdy on 16/7/1.
- // Copyright © 2016年 Wangdy. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface CLSwitch : UIControl
- //事件类型 UIControlEventValueChanged
- @property (nonatomic,assign)BOOL isOn;
- //默认初始化方法, onImage 和 offImage 是必选参数不能为 nil, frame.size 为无效值 ,size 根据图片size设置大小
- - (instancetype)initWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage frame:(CGRect)frame;
- @end
[objc] view plaincopy
- //
- // CLSwitch.m
- // UISwitchFremw
- //
- // Created by Wangdy on 16/7/1.
- // Copyright © 2016年 Wangdy All rights reserved.
- //
- #import "CLSwitch.h"
- @interface CLSwitch ()
- @property(nonatomic,strong)UIImage *onImage;
- @property(nonatomic,strong)UIImage *offImage;
- @end
- @implementation CLSwitch
- - (instancetype)initWithFrame:(CGRect)frame {
- NSAssert(NO, @"请使用 -initWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage frame:(CGRect)frame 初始化CLSwitch");
- return nil;
- }
- - (instancetype)initWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage frame:(CGRect)frame{
- NSAssert(onImage&&offImage, @"onImage & offImage 不能为空");
- frame.size = onImage.size;
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor whiteColor];
- _onImage = onImage;
- _offImage = offImage;
- [self addTarget:self action:@selector(switchClicked) forControlEvents:UIControlEventTouchUpInside];
- self.isOn = NO;
- }
- return self;
- }
- - (void)switchClicked {
- self.isOn = !self.isOn;
- [self sendActionsForControlEvents:UIControlEventValueChanged];
- }
- - (void)setIsOn:(BOOL)isOn {
- _isOn = isOn;
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contents"];
- animation.fromValue = self.layer.contents;
- animation.toValue = (id)(_isOn ? _onImage.CGImage: _offImage.CGImage);
- animation.duration = .3;
- [self.layer addAnimation: animation forKey: @"animation"];
- self.layer.contents = (id)(_isOn ? _onImage.CGImage: _offImage.CGImage);
- }
- - (void)setFrame:(CGRect)frame {
- if (_onImage) {
- frame.size = _onImage.size;
- }
- super.frame = frame;
- }
- - (CGSize)sizeThatFits:(CGSize)size {
- return _onImage.size;
- }
- @end
0 0
- 自定义 UISwitch
- 自定义 UISwitch
- iphone 自定义UISwitch
- iphone 自定义UISwitch
- IOS之自定义UISwitch
- IOS之自定义UISwitch
- 自定义UISwitch按钮
- IOS自定义UISwitch
- 自定义UISwitch颜色
- UISwitch 及其自定义方法
- UISwitch用法-以及-自定义UISwitch控件
- iOS自定义的UISwitch按钮
- iOS自定义的UISwitch按钮
- iOS自定义的UISwitch按钮
- iOS自定义的UISwitch按钮
- iOS自定义的UISwitch按钮
- iOS自定义的UISwitch按钮
- UISwitch
- 【好玩的计算机视觉】自动玩“别踩白块儿”
- HashTable源码
- 如何保护好个人隐私
- Activity启动方式的总结(2)
- Python 语言及其应用 Chapter_4_Exercise
- 自定义 UISwitch
- 六月英语总结
- hive总结
- 指派问题与匈牙利解法
- 回溯法求解背包个数问题
- IMF SparkSQLwithJoin 测试sparksql
- hdu 5677(manacher + 二维背包)
- MarkWord - 可发布到CSDN的Markdown编辑器(支持图片同步)
- 安卓与H5之间的调用,本地模版动态刷新数据