iOS 带拖拽效果的滑动解锁

先上效果

滑动解锁.gif

效果大概就是点击绿色部分可以左右滑动 滑动的同时 绿色滑块部分内部的文字和图片会根据滑动方向产生拖拽或者挤压碰撞效果 当滑块滑动到控件最右端时 不可再继续滑动 松开手时 完成解锁 滑块归位。 下面来说说实现


  • 第一步

创建一个继承自UIView的View 并在.h中声明代理或者block

@protocol LJKSlideViewDelegate <NSObject>

- (void)slideNeedDoSometing;

@end

@interface LJKSlideView : UIView

@property (nonatomic, weak)id<LJKSlideViewDelegate> delegate;

@property (nonatomic, copy)void(^slideNeedDoSometingBlock)();
//如果在上下文比较紧密的地方使用控件 可以使用block 这样比较方便获取上文中的内容 但是要注意循环引用的问题
//代理和block二选其一即可
@end
  • 第二步

在@interface中声明变量

@interface LJKSlideView (){

    UIView *_touchView;//滑动的点击区域 即绿色部分
    UIView *_shadowView;//滑动区域内部最右边的阴影 增加层次感

    UIImageView *_arrowImageView;//滑动区域内部 右箭头ImageView
    UIImageView *_imageView;//滑动区域右边 三个点的ImageView
    
    UILabel *_messageLabel;//立即解锁Label
    
    CGRect _touchViewFrame;//滑动区域的初始frame 重置的时候会用到
    
    CGFloat _labelXCutImageX;//立即解锁与右箭头 x坐标的差值  之后做动画会用到
    
}
  • 第三步

实现创建视图的方法

- (void)initUI{
        _touchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width * 0.55, self.bounds.size.height)];
        //这里我的滑动区域初始宽度为控件宽度的0.55 可以根据需要改动
        _touchView.layer.masksToBounds = YES;//防止做动画的时候视图错乱
        _touchView.backgroundColor = COLOFOR0X(0x2F8C53);//这是一个16进制转RGB宏 放在文章最后
        
        _touchViewFrame = _touchView.frame;
        CGSize touchViewSize = _touchView.frame.size;
        
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.text = @"立即解锁";
        _messageLabel.textColor = [UIColor whiteColor];
        [_messageLabel sizeToFit];
        [_touchView addSubview:_messageLabel];
        _messageLabel.center = _touchView.center;
        
        _arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(touchViewSize.width - 23, touchViewSize.height / 2.0 - 10, 15, 20)];
        //箭头的frame可以根据需要改动 需要注意的只有x坐标就是(seize.with - (imageView的宽度 + imageView到父视图右边的距离))
        _arrowImageView.image = [UIImage imageNamed:@"右"];
        _arrowImageView.contentMode = UIViewContentModeScaleAspectFill;
        [_touchView addSubview:_arrowImageView];
        
        _labelXCutImageX = _messageLabel.frame.origin.x - _arrowImageView.frame.origin.x;
        
        _shadowView = [[UIView alloc] initWithFrame:CGRectMake(touchViewSize.width - 1, 5, 1, touchViewSize.height - 10)];
        _shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
        _shadowView.layer.shadowOpacity = 1;
        _shadowView.layer.shadowRadius = 2.5;
        _shadowView.layer.shadowOffset = CGSizeMake(0,0);
        _shadowView.layer.shadowPath = [UIBezierPath bezierPathWithRect:_shadowView.bounds].CGPath;
        [_touchView addSubview:_shadowView];//可以根据需要调节阴影效果
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(touchViewSize.width, 0, self.bounds.size.width - touchViewSize.width, touchViewSize.height)];
        label.text = @"右滑动操作";
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = COLOFOR0X(0xd2d2d2);
        label.textAlignment = NSTextAlignmentCenter;
        
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(touchViewSize.width, touchViewSize.height / 2.0 - 10, 20, 20)];
        _imageView.image = [UIImage imageNamed:@"ur"];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        
        [self addSubview:label];
        [self addSubview:_imageView];
        [self addSubview:_touchView];//要注意保证滑动区域在视图最上层
}
  • 第四步

实现滑动方法

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (touch.view != _touchView || _touchView.frame.size.width == self.bounds.size.width) {
        return;//如果点击的不是滑动区域  或者滑动区域已经滑动到控件最右边 都不继续做变动
    }
    CGPoint pointNow = [touch locationInView:self];
    CGPoint pointPrevious = [touch previousLocationInView:self];
    
    CGRect frame = _touchView.frame;
    frame.size.width += pointNow.x - pointPrevious.x;
  
    if (frame.size.width > self.bounds.size.width) {
        frame.size.width = self.bounds.size.width;//产生滑块滑到控件最右边刚好停止的效果
    }
 
    [self changeTouchViewFrameWithFrame:frame];
}
  • 第五步

实现滑动动画

- (void)changeTouchViewFrameWithFrame:(CGRect)frame{
    
    [UIView animateWithDuration:frame.size.width == _touchViewFrame.size.width?0.218:0 delay:0 
                        options:UIViewAnimationOptionLayoutSubviews 
                     animations:^{
        //这里的三目运算符主要是为了判断是不是复位 如果是复位的时候给个动画效果 否则 平时滑动的时候不需要动画效果        

        _touchView.frame = frame;
        
        CGRect frame1 = _imageView.frame;
        frame1.origin.x = frame.size.width;
        _imageView.frame = frame1;
   
        CGRect frame2 = _shadowView.frame;
        frame2.origin.x = frame.size.width - 1;
        _shadowView.frame = frame2;
        
    } completion:^(BOOL finished) {
    }];

    
    [UIView animateWithDuration:0.168 delay:0.05 options:UIViewAnimationOptionLayoutSubviews animations:^{
        //箭头的动画 放在滑动区域尺寸变化之后 形成滑动区域右边框拖拽或者挤压箭头的效果
        CGRect frame1 = _arrowImageView.frame;
        frame1.origin.x = frame.size.width - 23;
        _arrowImageView.frame = frame1;
        
    } completion:^(BOOL finished) {
    }];
    
    [UIView animateWithDuration:0.168 delay:0.15 options:UIViewAnimationOptionLayoutSubviews animations:^{
        //Label的动画 放在箭头尺寸变化之后 形成箭头拖拽或者挤压Label的效果
        CGRect frame1 = _messageLabel.frame;
        frame1.origin.x = _arrowImageView.frame.origin.x + _labelXCutImageX;
        _messageLabel.frame = frame1;
        
    } completion:^(BOOL finished) {
    }];  

//时间差的越多 拖拽效果越明显 但是越不柔和
}
  • 第六步

实现松开手指的方法

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];

    if (touch.view != _touchView) {
        return;
    }
    
    if (_touchView.frame.size.width >= 0.7 * self.bounds.size.width) {
        //滑动解锁触发的宽度根据需要修改  我这里是滑动到超过或等于0.7个控件宽度就视为解锁成功
        //如果需要传统的滑动到最右边才视为解锁 此处改为_touchView.frame.size.width == self.bounds.size.width即可        

        if (self.slideNeedDoSometingBlock) {
            self.slideNeedDoSometingBlock();
        }
        
        if ([self.delegate respondsToSelector:@selector(slideNeedDoSometing)]) {
            [self.delegate slideNeedDoSometing]
        }
    }
    
    //回到初始状态
   [self changeTouchViewFrameWithFrame:_touchViewFrame];
}
  • 第七步

重写创建方法

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
  • 第八步

在需要使用的地方创建自己写的View

LJKSlideView *slide = [[LJKSlideView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
__weak typeof(self) weakSelf = self;
//实现解锁的block或者代理
slide.slideNeedDoSometingBlock = ^(){
    //实现触发解锁后你想做的事情 我这是做了一个渐隐的跳转tabBarController分栏
//        [UIView animateWithDuration:0.35 animations:^{
//            
//            weakSelf.view.alpha = 0.5;
//            
//        } completion:^(BOOL finished) {
//            weakSelf.tabBarController.selectedIndex = 1;
//            weakSelf.view.alpha = 1;
//        }];
};
[self.view addSubview: slide];

PS:如果你把这个View放在了ScrollView及其子类上时 有可能会出现你在左右滑动的同时,上下滑动了视图,滑动View就卡主不动了 需要重新滑动才可以动起来 此时你可以实现

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [slide resetSlide];//需要在.h中声明 resetSlide方法 
    
}

这样在你上下滑动的时候它就会自动重置


附上16进制转RGB的宏

#define COLOFOR0X(c)    [UIColor colorWithRed:((c>>16)&0xFF)/255.0  \
green:((c>>8)&0xFF)/255.0   \
blue:(c&0xFF)/255.0         \
alpha:1.0]

#define COLOFOR0XALPHA(c,a)    [UIColor colorWithRed:((c>>16)&0xFF)/255.0   \
green:((c>>8)&0xFF)/255.0   \
blue:(c&0xFF)/255.0         \
alpha:a]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,563评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,609评论 4 59
  • 变量住在哪里?换句话说,它们储存在哪里?最重要的是,程序需要时如何找到它们?这些问题说明需要一套设计良好的规则来存...
    焉知非鱼阅读 213评论 0 0
  • 我们以为的幸福其实不一定能带给我们幸福,倒是那些不期而遇的善意、关心就足够能温暖我们生命本身了。 2017年10月...
    古树蚂蚁阅读 973评论 6 22
  • 我不知道那段日子自己是怎么活下来的,身患癌症,男友背叛,一系列的打击接踵而至。但,我还是活下来了,为了报复他们……...
    在逃小羊羊阅读 847评论 5 2