好久不寫UI、正好項(xiàng)目里遇到一個(gè)需要自定義圖片的SwitchView。
寫了寫封裝了一下、感覺挺有意思干脆貼出來好了、沒準(zhǔn)對(duì)誰(shuí)有用。
效果圖如下、有需要可以自取。
主要寫了以下的幾個(gè)功能
- 可以設(shè)置X秒內(nèi)不允許點(diǎn)擊。
- 可以設(shè)置X秒后無新動(dòng)作再的捕獲回調(diào)。
- 自定義文字顏色、大小
- 自定義背景色
- 自定義圖片
.h文件如下
//
// KTSwitchView.h
// BaiSongInternational
//
// Created by 劉嵩野 on 2018/4/3.
// Copyright ? 2018年 maqihan. All rights reserved.
//
#import <UIKit/UIKit.h>
@class KTSwitchView;
typedef NS_OPTIONS(NSUInteger, KTSwitchViewStyle) {
//選中狀態(tài)
KTSwitchViewStyle_Selected = 1 << 0,
//普通狀態(tài)
KTSwitchViewStyle_Default = 1 << 1,
};
@protocol KTSwitchViewDelegate<NSObject>
/**
* selected屬性改變的時(shí)候調(diào)用
*/
- (void)KTSwichViewDidChange:(KTSwitchView *)swichView;
/**
* selected一定時(shí)間后沒有再改變時(shí)調(diào)用 默認(rèn)0s、也就是不走這個(gè)回調(diào)
*/
- (void)KTSwichViewDidDelayChange:(KTSwitchView *)swichView;
@end
@interface KTSwitchView : UIView
@property (nonatomic, weak) id <KTSwitchViewDelegate> delegate;
/* 延遲多久可以再次點(diǎn)擊 */
@property (nonatomic, assign) NSTimeInterval eventInterval;
/* 延遲多久回調(diào) BSSwichViewDidDelayChange 默認(rèn)0s*/
@property (nonatomic, assign) NSTimeInterval delayTime;
@property (nonatomic) UIFont * font;
@property (nonatomic) BOOL selected;
/**
* 可以KTSwitchViewStyle_Default|KTSwitchViewStyle_Selected同時(shí)設(shè)定兩種狀態(tài)
*/
- (void)setContentText:(NSString *)contentText style:(KTSwitchViewStyle)style;
- (void)setTextColor:(UIColor *)textColor style:(KTSwitchViewStyle)style;
- (void)setImage:(UIImage *)img style:(KTSwitchViewStyle)style;
- (void)setImageBgColor:(UIColor *)color style:(KTSwitchViewStyle)style;
- (void)setBgcolor:(UIColor *)bgcolor style:(KTSwitchViewStyle)style;
@end
需要注意的是
- 布局使用的是
Masonry
、需要項(xiàng)目支持。 - 延遲回調(diào)用的是NSOperation隊(duì)列、每次點(diǎn)擊廢棄隊(duì)列中的舊操作。
其實(shí)也可以(或者說從場(chǎng)景上更適合)用定時(shí)器、但是個(gè)人情感上不太喜歡不斷的開關(guān)某個(gè)定時(shí)器。