123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- //
- // CustomSwitch.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/1/17.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "CustomSwitch.h"
- @interface CustomSwitch ()
- @property (nonatomic, readwrite, strong) UIImageView *bgImageView;
- @property (nonatomic, readwrite, strong) UIImageView *selectImageView;
- @property (nonatomic, readwrite, assign) BOOL feedStatus;
- @end
- @implementation CustomSwitch
- #pragma mark - Public Method
- - (void)setOn:(BOOL)on {
- _on = on;
- // 主动设置的都没有震动效果
- self.feedStatus = NO;
- [self changeStatus];
- }
- - (void)setBgColor:(UIColor *)bgColor {
- _bgColor = bgColor;
- self.bgImageView.backgroundColor = bgColor;
- }
- - (void)setBgImage:(UIImage *)bgImage {
- _bgImage = bgImage;
- self.bgImageView.image = bgImage;
- }
- #pragma mark - Private Method
- - (instancetype)initWithCoder:(NSCoder *)coder {
- if (self = [super initWithCoder:coder]) {
- // 初始化
- [self _setup];
-
- // 创建子控件
- [self _setupSubViews];
-
- // 布局子控件
- [self _makeSubViewsConstraints];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- // 初始化
- [self _setup];
-
- // 创建子控件
- [self _setupSubViews];
-
- // 布局子控件
- [self _makeSubViewsConstraints];
- }
- return self;
- }
- #pragma mark - 事件处理Or辅助方法
- - (void)topAction:(UITapGestureRecognizer *)tap {
- self.on = !self.on;
- self.feedStatus = YES;
- [self changeStatus];
- }
- - (void)changeStatus {
- [UIView animateWithDuration:.3 animations:^{
- CGFloat width = self.height - self.margin * 2;
- if (self.on) {
- self.selectImageView.frame = CGRectMake(self.f_width - self.margin - width, self.margin, width, width);
- }else {
- self.selectImageView.frame = CGRectMake(self.margin, self.margin, width, width);
- }
- } completion:^(BOOL finished) {
- if (self.on) {
- if (self.onImage) {
- self.selectImageView.image = self.onImage;
- }else {
- self.selectImageView.backgroundColor = self.onTintColor;
- }
- }else {
- if (self.offImage) {
- self.selectImageView.image = self.offImage;
- }else {
- self.selectImageView.backgroundColor = self.thumbTintColor;
- }
- }
- if (self.feedStatus) {
- // 都是轻度震动
- if (@available(iOS 10.0, *)) {
- UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
- [feedBackGenertor impactOccurred];
- } else {
- // Fallback on earlier versions
- AudioServicesPlaySystemSound(1519);
- }
- }
- }];
- }
- #pragma mark - Private Method
- // 当用户点击到当前控件bounds时,会调用该方法,返回值决定了当前控件是否响应该事件
- - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
-
- return [super beginTrackingWithTouch:touch withEvent:event]; // 返回系统默认处理
- }
- // 如果 beginTrackingWithTouch 返回值 为YES,则以下方法 会在 点击手机屏幕移动 时 调用,如果这里返回值为YES,则继续移动会多次调用。
- // 如果 返回 NO,则 即使 继续移动也不会再调用当前方法了。
- - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
-
- return [super continueTrackingWithTouch:touch withEvent:event];
- }
- // 当点击屏幕释放时,调用该方法
- - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
- self.on = !self.on;
- self.feedStatus = YES;
- [self changeStatus];
- [super endTrackingWithTouch:touch withEvent:event];
- }
- // 取消时会调用,如果当前视图被移除。或者来电
- - (void)cancelTrackingWithEvent:(UIEvent *)event {
- [super cancelTrackingWithEvent:event];
- }
- - (void)_setup{
- }
- #pragma mark - 创建子控件
- - (void)_setupSubViews{
- [self addSubview:self.bgImageView];
- [self addSubview:self.selectImageView];
-
- // self.userInteractionEnabled = YES;
- // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topAction:)];
- // tap.numberOfTapsRequired = 1;
- // tap.numberOfTouchesRequired = 1;
- // [self addGestureRecognizer:tap];
- }
- #pragma mark - 布局子控件
- - (void)_makeSubViewsConstraints{
-
- self.bgImageView.frame = self.bounds;
- self.selectImageView.frame = CGRectMake(self.margin, self.margin, self.height - self.margin * 2, self.height - self.margin * 2);
- }
- - (UIImageView *)bgImageView {
- if (!_bgImageView) {
- _bgImageView = [UIImageView new];
- }
- return _bgImageView;
- }
- - (UIImageView *)selectImageView {
- if (!_selectImageView) {
- _selectImageView = [UIImageView new];
- }
- return _selectImageView;
- }
- @end
|