iOS - 用Masonry封装的slider,完全支持Masonry的适配

简介: iOS - 用Masonry封装的slider,完全支持Masonry的适配

上一篇博主用相对高度封装了一个slider,当时其实想用Masonry的,但是发现使用的并不熟练,所以今天又对这个Demo用Masonry重新进行了封装,感觉比较难的地方在于利用约束确定中心点的位置和约束的更新,具体的请看代码:

//
//  MySlider.m
//  MySlider
//
//  Created by 刘浩浩 on 16/7/21.
//  Copyright © 2016年 CodingFire. All rights reserved.
//
#import "MySlider.h"
@interface MySlider()
{
    UIView *lastView;
    CGFloat currentX;
    CGPoint clickCenter;
    NSString *type;
}
@property(nonatomic,strong)UIView *baseView;
/**
 *  slier背景
 */
@property(nonatomic,strong)UIView *preSliderView;
/**
 *  slider覆盖颜色
 */
@property(nonatomic,strong)UIView *lastSliderView;
/**
 *  滑块
 */
@property(nonatomic,strong)UIButton *sliderBtn;
/**
 *  模糊点击区域按钮
 */
@property(nonatomic,strong)UIButton *expandBtn;
@end
@implementation MySlider
- (id)init {
    self = [super init];
    if (!self) return nil;
    [self setSlierUI];
    return self;
}
- (void)setSlierUI
{
    lastView = self;
//    lastView.backgroundColor = [UIColor redColor];
    _baseView = [[UIView alloc]init];
    [self addSubview:_baseView];
    _preSliderView = [[UIView alloc]init];
    _preSliderView.layer.cornerRadius = 10;
    _preSliderView.backgroundColor = [UIColor grayColor];
    _preSliderView.clipsToBounds = YES;
    [_baseView addSubview:_preSliderView];
    _lastSliderView = [[UIView alloc]init];
    _lastSliderView.backgroundColor = [UIColor blueColor];
    [_preSliderView addSubview:_lastSliderView];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMoving:)];
    [_preSliderView addGestureRecognizer:tapGes];
    _sliderBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _sliderBtn.backgroundColor = [UIColor lightGrayColor];
    if (_slImage == nil) {
        _sliderBtn.layer.borderWidth = 0.5;
        _sliderBtn.layer.borderColor = [UIColor grayColor].CGColor;
        _sliderBtn.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        [_sliderBtn setImage:_slImage forState:UIControlStateNormal];
    }
    _sliderBtn.userInteractionEnabled = YES;
    _sliderBtn.layer.cornerRadius = _widthSameHeight / 2;
    [_sliderBtn addTarget:self action:@selector(dragMoving:withEvent:)forControlEvents: UIControlEventTouchDragInside];
    [_baseView addSubview:_sliderBtn];
    _expandBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _expandBtn.center = _sliderBtn.center;
    [_expandBtn addTarget:self action:@selector(dragMoving:withEvent:)forControlEvents: UIControlEventTouchDragInside];
    [_baseView addSubview:_expandBtn];
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.right.and.bottom.equalTo(lastView);
    }];
    [_preSliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(lastView);
        make.center.equalTo(lastView);
        make.height.mas_equalTo(_slHeight);
        make.width.equalTo(lastView.mas_width);
    }];
    [_lastSliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.bottom.equalTo(_preSliderView);
        make.height.equalTo(_preSliderView.mas_height);
        make.width.equalTo(_preSliderView.mas_width).multipliedBy(0.5);
        make.centerY.equalTo(_preSliderView.mas_centerY);
    }];
    [_sliderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_lastSliderView);
        make.width.and.height.mas_equalTo(20);
        make.left.equalTo(_lastSliderView.mas_right).offset(-20 / 2);
    }];
    [_expandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_lastSliderView);
        make.left.equalTo(_lastSliderView).offset(-20);
        make.width.and.height.mas_equalTo(20 * 2);
    }];
}
- (void)tapMoving:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:_preSliderView];
    currentX = point.x;
    type = @"tap";
    // 告诉self.view约束需要更新
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
}
- (void)updateConstraints
{
    if ([type isEqualToString:@"nottap"]) {
        [_preSliderView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(lastView);
            make.center.equalTo(lastView);
            make.height.mas_equalTo(_slHeight);
            make.width.equalTo(lastView.mas_width);
        }];
        [_lastSliderView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.and.top.and.bottom.equalTo(_preSliderView);
            make.height.equalTo(_preSliderView.mas_height);
            make.width.equalTo(_preSliderView.mas_width).multipliedBy(_value);
            make.centerY.equalTo(_preSliderView.mas_centerY);
        }];
        [_sliderBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_lastSliderView);
            make.width.and.height.mas_equalTo(_widthSameHeight);
            make.left.equalTo(_lastSliderView.mas_right).offset(-_widthSameHeight / 2);
        }];
        [_expandBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_lastSliderView);
            make.width.height.mas_equalTo(_widthSameHeight * 2);
            make.left.equalTo(_lastSliderView.mas_right).offset(-_widthSameHeight);
        }];
    }
    if ([type isEqualToString:@"tap"]) {
        [_lastSliderView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.and.top.and.bottom.equalTo(_preSliderView);
            make.height.equalTo(_preSliderView.mas_height);
            make.width.mas_equalTo(currentX);
            make.centerY.equalTo(_preSliderView.mas_centerY);
        }];
        [_sliderBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_lastSliderView);
            make.width.and.height.mas_equalTo(_widthSameHeight);
            make.left.mas_equalTo(currentX - _widthSameHeight / 2);
        }];
        [_expandBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_lastSliderView);
            make.width.and.height.mas_equalTo(_widthSameHeight * 2);
            make.left.mas_equalTo(currentX - _widthSameHeight * 2 / 2);
        }];
    }
    [super updateConstraints];
}
- (void)dragMoving: (UIButton *)btn withEvent:(UIEvent *)event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self];
    CGFloat x = point.x;
    if(x <= _widthSameHeight / 2)
    {
        point.x = _widthSameHeight / 2;
    }
    if(x >= self.bounds.size.width - _widthSameHeight / 2)
    {
        point.x = self.bounds.size.width - _widthSameHeight / 2;
    }
    point.y = self.frame.size.height / 2;
    currentX = point.x;
    type = @"tap";
    // 告诉self.view约束需要更新
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
}
- (void)setValue:(CGFloat)value
{
    _value = value;
    type = @"nottap";
    // 告诉self.view约束需要更新
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
}
- (void)setSlImage:(UIImage *)slImage
{
    _slImage = slImage;
    [_sliderBtn setImage:_slImage forState:UIControlStateNormal];
}
- (void)setSlHeight:(CGFloat)slHeight
{
    _slHeight = slHeight;
    if (_slHeight >= 2 && _slHeight <= 20) {
        type = @"nottap";
        // 告诉self.view约束需要更新
        [self setNeedsUpdateConstraints];
        // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
        [self updateConstraintsIfNeeded];
        [self layoutIfNeeded];
    }
}
- (void)setPreBackgroundColor:(UIColor *)preBackgroundColor
{
    _preBackgroundColor = preBackgroundColor;
    _preSliderView.backgroundColor = _preBackgroundColor;
}
- (void)setLastBackgroundColor:(UIColor *)lastBackgroundColor
{
    _lastBackgroundColor = lastBackgroundColor;
    _lastSliderView.backgroundColor = _lastBackgroundColor;
}
- (void)setWidthSameHeight:(CGFloat)widthSameHeight
{
    _widthSameHeight = widthSameHeight;
    type = @"nottap";
    _sliderBtn.layer.cornerRadius = _widthSameHeight / 2;
    // 告诉self.view约束需要更新
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
}
- (void)setSliderBackgroundColor:(UIColor *)sliderBackgroundColor
{
    _sliderBackgroundColor = sliderBackgroundColor;
    _sliderBtn.backgroundColor = _sliderBackgroundColor;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
@end

加约束并没有想象的那么简单,只有真正动手去做了才知道其中的易错点,但是使用熟练了确实蛮方便的,对这里的约束有什么不懂的请留言或者自行百度。


Demo下载地址:https://github.com/codeliu6572/MySlider

目录
相关文章
|
4月前
|
IDE API Android开发
安卓与iOS开发环境的差异及适配策略
在移动应用开发的广阔舞台上,Android和iOS两大操作系统各据一方,各自拥有独特的开发环境和工具集。本文旨在深入探讨这两个平台在开发环境上的关键差异,并提供有效的适配策略,帮助开发者优化跨平台开发流程。通过比较Android的Java/Kotlin和iOS的Swift/Objective-C语言特性、IDE的选择、以及API和系统服务的访问方式,本文揭示了两个操作系统在开发实践中的主要分歧点,并提出了一套实用的适配方法,以期为移动开发者提供指导和启示。
|
6月前
|
iOS开发
SwiftUI适配iOS16导航控制器引起的闪退
SwiftUI适配iOS16导航控制器引起的闪退
73 0
|
6月前
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
98 0
|
小程序 开发工具 Android开发
Donut多端框架小程序打包适配ios和安卓app
腾讯新出了一个 Donut 多端框架,可以直接将微信小程序转成 ios 和 安卓 app,小程序开发者工具里也集成了 app 相关升级、调试和打包的功能,终于可以一套代码开发出3个客户端了!
258 0
Donut多端框架小程序打包适配ios和安卓app
|
安全 前端开发 开发工具
iOS12、iOS11、iOS10、iOS9常见适配
iOS12、iOS11、iOS10、iOS9常见适配
236 0
|
小程序 iOS开发
uniapp中IOS端小程序底部黑线适配的方法(整理)
uniapp中IOS端小程序底部黑线适配的方法(整理)
|
Shell iOS开发
iOS 逆向编程(九 - 2)将端口映射、USB连接手机封装成 .sh 脚本
iOS 逆向编程(九 - 2)将端口映射、USB连接手机封装成 .sh 脚本
138 0
|
前端开发 JavaScript 定位技术
iOS 逆向编程(十六)DZMCycript 脚本使用(封装了常用的快捷函数,后续会继续添加)
iOS 逆向编程(十六)DZMCycript 脚本使用(封装了常用的快捷函数,后续会继续添加)
134 0
|
缓存 JavaScript iOS开发
iOS 逆向编程(十五)Cycript 语法进阶(封装 .cy 脚本文件)
iOS 逆向编程(十五)Cycript 语法进阶(封装 .cy 脚本文件)
185 0
|
开发工具 iOS开发 开发者
iOS 暗黑模式的适配总结
iOS 暗黑模式的适配总结