zoukankan      html  css  js  c++  java
  • 自定义UISwitch

    ViewController.m

     1 #import "ViewController.h"
     2 #import "MyUISwitch.h"
     3 @interface ViewController ()
     4             
     5 
     6 @end
     7 
     8 @implementation ViewController
     9             
    10 - (void)viewDidLoad {
    11     [super viewDidLoad];
    12     
    13     UISwitch* sw = [[UISwitch alloc]initWithFrame:CGRectMake(200, 100, 0, 0)];
    14     [self.view addSubview:sw];
    15     
    16     MyUISwitch* s = [[MyUISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
    17     [self.view addSubview:s];
    18     
    19     
    20 }

    MyUISwitch.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface MyUISwitch : UIControl
    4 @property (nonatomic,assign,getter=isOn) BOOL on;
    5 @end

    MyUISwitch.m

     1 #import "MyUISwitch.h"
     2 
     3 @implementation MyUISwitch
     4 
     5 - (instancetype)init{
     6     return [self initWithFrame:CGRectZero];
     7 }
     8 
     9 - (instancetype)initWithFrame:(CGRect)frame
    10 {
    11     frame.size.width = 49;
    12     frame.size.height = 31;
    13     self = [super initWithFrame:frame];
    14     if (self) {
    15         UIView* backView = [[UIView alloc]initWithFrame:self.bounds];
    16         backView.backgroundColor = [UIColor greenColor];
    17         backView .tag = 99;
    18         backView.layer.cornerRadius = 15.5;
    19         [self addSubview:backView];
    20         
    21         CGRect r = self.bounds;
    22         r.origin.x = .5;
    23         r.origin.y = .5;
    24         r.size.height -= 1;
    25         r.size.width -= .5;
    26         UIView* offBackView = [[UIView alloc]initWithFrame:r];
    27         offBackView.backgroundColor = [UIColor whiteColor];
    28         offBackView.tag = 100;
    29         offBackView.layer.cornerRadius = 16;
    30         [self addSubview:offBackView];
    31         
    32         UIView* thumb = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    33         thumb.layer.cornerRadius = 15;
    34         thumb.tag= 101;
    35         thumb.backgroundColor = [UIColor whiteColor];
    36         thumb.layer.borderWidth = 0.5;
    37         thumb.layer.borderColor = [UIColor grayColor].CGColor;
    38         [self addSubview:thumb];
    39     }
    40     return self;
    41 }
    42 -(void)setFrame:(CGRect)frame{
    43     frame.size.width = 49;
    44     frame.size.height = 31;
    45     [super setFrame:frame];
    46 }
    47 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    48      // thumb frame
    49 }
    50 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    51     // UITouch *touch = [touches anyObject];
    52     // CGPoint p = [touch locationInView:self];
    53     _on =! _on;
    54     UIView* thumb = [self viewWithTag:101];
    55     UIView* back = [self viewWithTag:99];
    56     UIView* off = [self viewWithTag:100];
    57     
    58     [UIView animateWithDuration:.2 animations:^{
    59     if (_on) {
    60         thumb.center = CGPointMake(self.frame.size.width-thumb.frame.size.width/2,thumb.center.y);
    61     }else{
    62         back.backgroundColor = [UIColor greenColor];
    63         thumb.center = CGPointMake(thumb.frame.size.width/2, thumb.center.y);
    64     }}];
    65     off.hidden= _on;
    66 }
    67 
    68 @end
  • 相关阅读:
    JS经典面试题
    javascript数组(1) ——sort的工作原理及其他数组排序方法
    怎么去掉javascript 的Array的重复项
    Intellij IDEA运行Error ——Command line is too long
    angular-waring:global Angular与local Angular版本不一致问题
    idea(集成python)下载python插件失败
    PLSQL登录oracle显示无监听或协议适配器错误
    maven不能加载ojdbc6.jar的解决方法
    eclipse安装maven插件
    windows下gitbash安装教程
  • 原文地址:https://www.cnblogs.com/WillingToAsk1946zzh/p/4504524.html
Copyright © 2011-2022 走看看