Masonry实现的滑块

这是《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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,041评论 25 709
  • 风萧萧兮易水寒,壮士一去兮不复还。 祸兮福之所倚,福兮祸之所伏。
    一场美梦被惊醒阅读 981评论 0 0
  • 你我若星我总是很大声地很大声地跟你说话就像星辰很努力地很努力地闪着光明我害怕你会听不见抑或是听不清就像星辰会害怕黑...
    i张小呵阅读 1,644评论 2 4
  • 今天是11月22号,周二,雨天,在深圳的一个甜品店的角落,敲着文字。 或许和很多深漂女生一样,25岁,过得稍...
    激进的菲比阅读 1,664评论 0 2