Masonry实现的滑块

作者: raingu24 | 来源:发表于2017-08-28 12:20 被阅读37次

这是《iOS Auto Layout开发秘籍》中的一个例子。这里使用Masonry改写了一下。

实现的效果如下:

2017-08-28 12_15_49.gif

代码如下:

#import "LockControl.h"
#import "Masonry.h"

@implementation LockControl

{
    UIImageView *lockView;
    UIImageView *trackView;
    UIImageView *thumbView;
}

#pragma mark - Layout

- (void) layoutConstraints {
    
    // Self
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(256, 256)).priority(UILayoutPriorityRequired);
    }];
    
    // Lock
    [lockView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self).centerOffset(CGPointMake(0, -38)).priority(UILayoutPriorityRequired);

    }];
    
    // Track
    [trackView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self).centerOffset(CGPointMake(0, 80)).priority(UILayoutPriorityRequired);
    }];
    
    // Thumb
    [thumbView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(trackView.mas_centerY).priority(UILayoutPriorityRequired);
        
// 约束的关键!!!左边是大于等于,右边是小于等于
make.left.mas_greaterThanOrEqualTo(12).priorityHigh();
        make.right.mas_lessThanOrEqualTo(-12).priorityHigh();
        make.leading.mas_equalTo(trackView).priorityMedium();
    }];
}

- (void) buildView { 
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3f];
    self.layer.cornerRadius = 32;
    self.layer.masksToBounds = YES;
    
    lockView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lockClosed"]];
    [self addSubview:lockView];
    
    trackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"track"]];
    hug(trackView, UILayoutPriorityRequired);
    [self addSubview:trackView];
    
    thumbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"thumb"]];
    [trackView addSubview:thumbView];
    
    [self layoutConstraints];
    
}

#pragma mark - Creation

- (instancetype) initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self == nil) {
        return self;
    }
    
    self.backgroundColor = [UIColor clearColor];
    [self buildView];
    return self;
    
}

- (instancetype) init {
    return [self initWithFrame:CGRectZero];
}

#pragma mark - UIControl

- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    
    // 测试开始时候的touch
    
    CGPoint touchPoint = [touch locationInView:self];
    CGRect largeTrack = CGRectInset(trackView.frame, -20.0f, -20.0f);
    if (!CGRectContainsPoint(largeTrack, touchPoint)) {
        return NO;
    }
    
    touchPoint = [touch locationInView:trackView];
    CGRect largeThumb = CGRectInset(thumbView.frame, -20.0f, -20.0f);
    if (!CGRectContainsPoint(largeThumb, touchPoint)) {
        return NO;
    }
    
    // 开始跟踪
    [self sendActionsForControlEvents:UIControlEventTouchDown];
    return YES;
}

- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    
    CGPoint touchPoint = [touch locationInView:self];
    
    // 更新滑动的位置
    touchPoint = [touch locationInView:trackView];
    
    [UIView animateWithDuration:0.1f animations:^{
        
       
        [thumbView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.leading.mas_equalTo(trackView).mas_offset(touchPoint.x).priorityMedium();
            
        }];
    }];
    return YES;
}

- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    
    CGPoint touchPoint = [touch locationInView:trackView];
    
    if (touchPoint.x > trackView.frame.size.width * 0.75f) {
        // 默认为解锁
        self.userInteractionEnabled = NO;
        [self sendActionsForControlEvents:UIControlEventValueChanged];
        
        // 自我移除
        [UIView animateWithDuration:0.5f animations:^{
            self.alpha = 0.0f;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    } else {
        // 约束的动画
        [UIView animateWithDuration:0.2f animations:^{
            [thumbView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(trackView).priorityMedium();
            }];
            // 动画的关键!!!
            [trackView layoutIfNeeded];
        }];
    }
    
    if (CGRectContainsPoint(trackView.bounds, touchPoint)) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    } else {
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    }
}

- (void) cancelTrackingWithEvent:(UIEvent *)event {
    
    [self sendActionsForControlEvents:UIControlEventTouchCancel];
    
    [UIView animateWithDuration:0.2 animations:^{
        [thumbView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(trackView).priorityMedium();
        }];
        
        [trackView layoutIfNeeded];
    }];
}


#pragma mark - Helper

void hug(UIView *view, UILayoutPriority priority) {
    
    hug_H(view, priority);
    hug_V(view, priority);
}

void hug_H(UIView *view, UILayoutPriority priority){
    [view setContentHuggingPriority:(priority) forAxis:UILayoutConstraintAxisHorizontal];
}

void hug_V(UIView *view, UILayoutPriority priority) {
    [view setContentHuggingPriority:priority forAxis:UILayoutConstraintAxisVertical];
}
@end

素材:

  1. lock


    lockClosed.png
  2. thumb


    thumb.png
  3. track


    track.png

相关文章

  • Masonry实现的滑块

    这是《iOS Auto Layout开发秘籍》中的一个例子。这里使用Masonry改写了一下。 实现的效果如下: ...

  • Autolayout

    有趣的Autolayout示例-Masonry实现 有趣的Autolayout示例2-Masonry实现 有趣的A...

  • Autolayout、VFL、Masonry

    适配 VFL语言 Masonry 代码实现Autolayout VFL代码 Masonry使用 总结 使用代码实现...

  • Masonry

    有趣的Autolayout示例-Masonry实现有趣的Autolayout示例2-Masonry实现有趣的Aut...

  • iOS开发拓展篇—音频处理(音乐播放器5)

    实现效果: 一、半透明滑块的设置 复制代码1 /**2 *拖动滑块3 /4 - (IBAction)panSl...

  • iOS--怎么获取UISlider 的滑块frame

    本文将分享怎么实现:怎么获取UISlider 的滑块frame。 由于项目需要,当滑块滑动时,标签Label要实时...

  • vue实现单滑块双滑块

    功能比较简单,就讲讲实现逻辑。1.首先是监听鼠标按下,然后表示开始移动,接着去监听mousemove,可以得到鼠标...

  • React Native 实现环形滑块

    最近在项目中需要实现一个半圆环形的滑块组件用于实现温度的调节,基本的效果如下: 要求可以控制开口的角度,滑块支持渐...

  • 安卓自定义Switch开关控件

    实现效果 实现方案 背景: switch_track.xml 滑块: switch_thumb.xml 选中背景:...

  • UITextView高度自适应

    一、Masonry实现自适应高度 二、子类化实现自适应高度

网友评论

    本文标题: Masonry实现的滑块

    本文链接:https://www.haomeiwen.com/subject/zmnwdxtx.html