欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

IOS自定義UISwitch - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?

您當(dāng)前位置>首頁 » 新聞資訊 » 技術(shù)分享 >

IOS自定義UISwitch

發(fā)表時間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):72


原創(chuàng)Blog,轉(zhuǎn)載請注明出處
blog.csdn.net/hello_hwc?viewmode=contents

下晝的時刻閑著無聊,簡單想了想,用三個UILabel來實(shí)現(xiàn)這個簡單的自定義UISwitch
效不雅圖,
[img]http://img.blog.csdn.net/20150105205346291?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGVsbG9fSHdj/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center                                [img]http://img.blog.csdn.net/20150105205351938?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGVsbG9fSHdj/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
當(dāng)然,有些粗拙,后續(xù)有時光了我會把界面優(yōu)化下。直接拿卻竽暌姑估計界面粗拙了點(diǎn),網(wǎng)上也有很多源代碼。在寫了這么多Blog后發(fā)明,其實(shí)學(xué)會思慮異常重要,這里欲望拋磚引玉,讓想要進(jìn)修的同窗知道若何去自定義一個控件。
第一步,分析下自定義控件的構(gòu)成要素
在我這里就是四個部分
1.左半部分視圖-UILabel
2.右半部分視圖-UILabel
3.滑動的視圖-UILabel
4.對滑動的視圖添加手勢-UITapGestureRecognizer
第二步,設(shè)計接口
在我這里接口比較簡單,就是這個控件的狀況,用Bool來表示
所以頭文件為
#import <UIKit/UIKit.h>

@interface HwcCustomUISwitch : UIView
@property (nonatomic)BOOL isOn;
@end





第三步,設(shè)計實(shí)現(xiàn)
1.對三個Label在Getter中采取惰性初始化,對滑動的View添加手勢辨認(rèn)
2.對isOn的Setter函數(shù)中進(jìn)行UI同步,包管每次Model改變了,View改變(MVC模式)
.m文件為
//
//  HwcCustomUISwitch.m
//  CustomActivityIndicator
//
//  Created by huangwenchen on 15/1/5.
//  Copyright (c) 2015年 BlogForCSDN. All rights reserved.
//

#import "HwcCustomUISwitch.h"

@interface HwcCustomUISwitch()
@property (strong,nonatomic) UILabel * leftLabel;
@property (strong,nonatomic) UILabel * rightLabel;
@property (strong,nonatomic) UILabel * tagLabel;
@end

@implementation HwcCustomUISwitch
-(UILabel*)tagLabel{
    if (!_tagLabel) {
        CGRect frame = self.isOn?CGRectMake(0, 0,self.frame.size.width/2, self.frame.size.height):CGRectMake(self.frame.size.width/2,0, self.frame.size.width/2,self.frame.size.height);
        _tagLabel = [[UILabel alloc] initWithFrame:frame];
    }
    return  _tagLabel;
}
-(UILabel*)leftLabel{
    if (!_leftLabel) {
        CGRect frame = CGRectMake(0, 0, self.frame.size.width/2, self.frame.size.height);
        _leftLabel = [[UILabel alloc] initWithFrame:frame];
        _leftLabel.text = @"ON";
        _leftLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _leftLabel;
}
-(UILabel*)rightLabel{
    if (!_rightLabel) {
        CGRect frame = CGRectMake(self.frame.size.width/2,0, self.frame.size.width/2, self.frame.size.height);
        _rightLabel = [[UILabel alloc] initWithFrame:frame];
        _rightLabel.text = @"OFF";
        _rightLabel.textAlignment = NSTextAlignmentCenter;

    }
    return _rightLabel;
}
-(void)setIsOn:(BOOL)isOn{
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    _isOn = isOn;
    if (isOn) {
        [UIView animateWithDuration:1.0
                              delay:0.01
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.tagLabel.center = CGPointMake(width/4,height/2);
                         } completion:^(BOOL finished) {
                             
                         }];
    }else{
        [UIView animateWithDuration:1.0
                              delay:0.01
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.tagLabel.center = CGPointMake(3*width/4,height/2);
                         } completion:^(BOOL finished) {
                             
                         }];
    }
}
-(void)setUp{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.tagLabel addGestureRecognizer:tap];
    self.tagLabel.userInteractionEnabled = YES;
    self.userInteractionEnabled = YES;
    self.rightLabel.backgroundColor = [UIColor brownColor];
    [self addSubview:self.rightLabel];
    self.leftLabel.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.leftLabel];
    self.tagLabel.backgroundColor = [UIColor greenColor];
    [self addSubview:self.tagLabel];
    self.layer.cornerRadius = self.frame.size.height/10;
    self.clipsToBounds = YES;
}
-(void)tap{
    self.isOn = !self.isOn;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setUp];
    }
    return self;
}
-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}
@end
然后,在應(yīng)用的處所
    HwcCustomUISwitch * hwcSwitch = [[HwcCustomUISwitch alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    [self.view addSubview:hwcSwitch];
如不雅想要應(yīng)用image直接設(shè)置UILabel的屬性就行了。

相關(guān)案例查看更多