iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现垂直滑动条效果
  • 996
分享到

iOS实现垂直滑动条效果

2024-04-02 19:04:59 996人浏览 八月长安
摘要

我们知道在 iOS 开发中,有一个控件经常用到,那就是滑动条(UISlider),可以满足我们滑动取值的需求。但是现在有一个需求,就是需要一个垂直的滑动条,而 UISlider 并不

我们知道在 iOS 开发中,有一个控件经常用到,那就是滑动条(UISlider),可以满足我们滑动取值的需求。但是现在有一个需求,就是需要一个垂直的滑动条,而 UISlider 并不能设置为垂直滑动,所以我们就需要自己定义一个控件来实现垂直的要求。

整理之后,我们可以得出需要以下的基本需求:

  • 可以上下滑动
  • 按钮可以自定义图片
  • 可以设置最小值
  • 可以设置最大值
  • 可以在滑动过程中获取实时的值
  • 可以在滑动结束时获取到最终的值
  • 可以设置进度背景色

我们的实现原理就是实现一个自定义的 UIView,然后在上面添加需要用到的控件,对控件添加一定的手势功能,从而实现垂直滑动。实现了一个单独的类,功能不多,但是能满足以上基本的需求,代码如下,代码中用到的宏可以自行替换,开箱即用,简单明了:

VerticalSlider.h

//
//  VerticalSlider.h
//  
//
//  Created by huang zhengguo on 2019/8/30.
//  Copyright © 2019 huang zhengguo . All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
NS_ASSUME_NONNULL_BEGIN
 
@interface VerticalSlider : UIView
 
@property (assign, nonatomic) float value;
@property (strong, nonatomic) UIImage *thumImage;
@property (assign, nonatomic) float minimumValue;
@property (assign, nonatomic) float maximumValue;
 
@property (copy, nonatomic) void (^passValue) (float);
@property (copy, nonatomic) void (^passEndValue) (float);
 

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title progressColor:(UIColor *)progressColor thumImage:(NSString *)thumImage;
 
@end
 
NS_ASSUME_NONNULL_END

VerticalSlider.m

//
//  VerticalSlider.m
// 
//
//  Created by huang zhengguo on 2019/8/30.
//  Copyright © 2019 zhengguohuang. All rights reserved.
//
 
#import "VerticalSlider.h"
 
#define THUM_BTN_WIDTH 30.0
#define THUM_BTN_HEIGHT 50.0
 
@interface VerticalSlider()
 
@property (strong, nonatomic) UIButton *thumBtn;
// 使用两个label表示进度,一个背景,一个进度
@property (strong, nonatomic) UILabel *backLabel;
@property (strong, nonatomic) UILabel *progressLabel;
// 底部标题
@property (strong, nonatomic) UILabel *titleLabel;
// 值标题
@property (strong, nonatomic) UILabel *valueLabel;
 
@end
 
@implementation VerticalSlider
 
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title progressColor:(UIColor *)progressColor thumImage:(NSString *)thumImage {
    if (self = [super initWithFrame:frame]) {
        // 滑动按钮
        self.thumBtn = [[UIButton alloc] init];
 
        [self.thumBtn setBackgroundImage:[UIImage imageNamed:thumImage] forState:UIControlStateNORMal];
        self.thumBtn.translatesAutoresizingMaskIntoConstraints = NO;
        
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(thumbPanAction:)];
 
        [self.thumBtn addGestureRecognizer:panGestureRecognizer];
        [self addSubview:self.thumBtn];
 
        // 进度条
        self.backLabel = [[UILabel alloc] init];
 
        self.backLabel.backgroundColor = [progressColor colorWithAlphaComponent:0.3];
        self.backLabel.translatesAutoresizingMaskIntoConstraints = NO;
 
        [self addSubview:self.backLabel];
 
        self.progressLabel = [[UILabel alloc] init];
 
        self.progressLabel.backgroundColor = progressColor;
        self.progressLabel.translatesAutoresizingMaskIntoConstraints = NO;
 
        [self addSubview:self.progressLabel];
 
        // 底部标题
        self.titleLabel = [[UILabel alloc] init];
 
        self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.text = title;
 
        [self addSubview:self.titleLabel];
 
        // 顶部值
        self.valueLabel = [[UILabel alloc] init];
 
        self.valueLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.valueLabel.textAlignment = NSTextAlignmentCenter;
        self.valueLabel.textColor = [UIColor whiteColor];
 
        [self addSubview:self.valueLabel];
 
        [self bringSubviewToFront:self.thumBtn];
 
        [self setConstraints];
        
        // 初始化数据
        self.value = 0.0;
    }
    
    return self;
}
 
#pragma mark --- 按钮拖动方法
- (void)thumbPanAction:(UIPanGestureRecognizer *)panGestureRecognizer {
    // 转换坐标
    CGPoint point = [panGestureRecognizer translationInView:self];
    
    CGFloat yOriginPoint = panGestureRecognizer.view.frame.origin.y + point.y;
    if (yOriginPoint >=self.backLabel.frame.origin.y && yOriginPoint <= (self.backLabel.frame.origin.y + self.backLabel.frame.size.height - THUM_BTN_HEIGHT)) {
        panGestureRecognizer.view.frame = CGRectMake(panGestureRecognizer.view.frame.origin.x, panGestureRecognizer.view.frame.origin.y + point.y, THUM_BTN_WIDTH, THUM_BTN_HEIGHT);
        
        self.value = 1.0 - (yOriginPoint - self.backLabel.frame.origin.y) / (self.backLabel.frame.size.height - THUM_BTN_HEIGHT);
        if (self.passValue) {
            KMYLOG(@"colorValue = %f", self.value);
            self.passValue(self.value);
        }
    }
 
    // 转换成原来坐标系的坐标
    [panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self];
    
    if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        if (self.passEndValue) {
            KMYLOG(@"结束滑动");
            // 转为字符串,又转为float,是为了去的两位小数的浮点数
            self.passEndValue([[NSString stringWithFormat:@"%.2f", self.value] floatValue]);
        }
    }
}
 
- (void)setValue:(float)value {
    _value = value;
 
    self.thumBtn.frame = CGRectMake(self.thumBtn.frame.origin.x, (self.backLabel.frame.size.height - THUM_BTN_HEIGHT) * (1 - value) + self.backLabel.frame.origin.y, THUM_BTN_WIDTH, THUM_BTN_HEIGHT);
    self.progressLabel.frame = CGRectMake(self.progressLabel.frame.origin.x, self.thumBtn.frame.origin.y + THUM_BTN_HEIGHT, self.progressLabel.frame.size.width, self.backLabel.frame.origin.y + self.backLabel.frame.size.height - self.thumBtn.frame.origin.y - THUM_BTN_HEIGHT);
    self.valueLabel.text = [NSString stringWithFormat:@"%.0f%%", value * 100];
}
 
- (void)setConstraints {
    NSArray *titleLabelArray = @[self.titleLabel, self.valueLabel];
    for (UILabel *label in titleLabelArray) {
        NSLayoutConstraint *labelLeadingLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
        NSLayoutConstraint *labelTrailingLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
        NSLayoutConstraint *labelHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.0];
 
        [self addConstraints:@[labelLeadingLayoutConstraint, labelTrailingLayoutConstraint, labelHeightLayoutConstraint]];
 
        if (label == self.titleLabel) {
            NSLayoutConstraint *labelBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
 
            [self addConstraint:labelBottomLayoutConstraint];
        } else if (label == self.valueLabel) {
            NSLayoutConstraint *labelTopLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
 
            [self addConstraint:labelTopLayoutConstraint];
        }
    }
    
    NSArray *labelArray = @[self.backLabel, self.progressLabel];
    for (UILabel *label in labelArray) {
        NSLayoutConstraint *progressCenterXLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
        NSLayoutConstraint *progressBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:-8.0];
        NSLayoutConstraint *progressWidthLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:3.0];
 
        [self addConstraints:@[progressCenterXLayoutConstraint, progressBottomLayoutConstraint, progressWidthLayoutConstraint]];
 
        if (label == self.backLabel) {
            NSLayoutConstraint *progressTopLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.valueLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
 
            [self addConstraint:progressTopLayoutConstraint];
        } else {
            NSLayoutConstraint *progressheightLayoutConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.0];
 
            [self addConstraint:progressHeightLayoutConstraint];
        }
    }
 
    NSLayoutConstraint *thumBtnCenterXLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.thumBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
    NSLayoutConstraint *thumBtnBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.thumBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
    NSLayoutConstraint *thumBtnWidthLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.thumBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:THUM_BTN_WIDTH];
    NSLayoutConstraint *thumBtnHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.thumBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:THUM_BTN_HEIGHT];
 
    [self addConstraints:@[thumBtnCenterXLayoutConstraint, thumBtnBottomLayoutConstraint, thumBtnWidthLayoutConstraint, thumBtnHeightLayoutConstraint]];
}
 
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.thumBtn.frame = CGRectMake(self.thumBtn.frame.origin.x, (self.backLabel.frame.size.height - THUM_BTN_HEIGHT) * (1 - self.value) + self.backLabel.frame.origin.y, THUM_BTN_WIDTH, THUM_BTN_HEIGHT);
    self.progressLabel.frame = CGRectMake(self.progressLabel.frame.origin.x, self.thumBtn.frame.origin.y + THUM_BTN_HEIGHT, self.progressLabel.frame.size.width, self.backLabel.frame.origin.y + self.backLabel.frame.size.height - self.thumBtn.frame.origin.y - THUM_BTN_HEIGHT);
}
 

 
@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: iOS实现垂直滑动条效果

本文链接: https://www.lsjlt.com/news/143251.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • iOS实现垂直滑动条效果
    我们知道在 iOS 开发中,有一个控件经常用到,那就是滑动条(UISlider),可以满足我们滑动取值的需求。但是现在有一个需求,就是需要一个垂直的滑动条,而 UISlider 并不...
    99+
    2022-11-13
  • iOS实现多个垂直滑动条并列视图
    本文实例为大家分享了iOS实现多个垂直滑动条并列视图的具体代码,供大家参考,具体内容如下 上一篇文章我们实现了一个垂直滑动条的类 (VerticalSlider),用来满足垂直滑动的...
    99+
    2022-11-13
  • iOS Segment带滑动条切换效果
    本文实例为大家分享了iOS Segment带滑动条切换效果的具体代码,供大家参考,具体内容如下 #import "ViewController.h"   @interface Vie...
    99+
    2022-11-13
  • iOS实现背景滑动效果
    本文实例为大家分享了iOS实现背景滑动效果的具体代码,供大家参考,具体内容如下 1、在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何才能够简单,快速的实现那样的效果呢...
    99+
    2022-11-13
  • iOS实现无限滑动效果
    在看到这个标题的时候,相信大家心里肯定会想,无限循环轮播的博客已经满天飞了,好有必要写么。这里我想声明一下,这里的无线滑动,但是数据却不循环。 实现原理 由于业务的需求,需要有大量的...
    99+
    2022-11-13
  • iOS如何实现背景滑动效果
    这篇文章主要介绍了iOS如何实现背景滑动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下第一步、在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何...
    99+
    2023-06-29
  • js如何实现垂直滚动条
    这篇文章主要为大家展示了“js如何实现垂直滚动条”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“js如何实现垂直滚动条”这篇文章吧。效果图:代码如下:<!D...
    99+
    2022-10-19
  • Android仿淘宝首页头条View垂直滚动效果
    之前本来是打算做TextView垂直向上滚动的,后来发现一位大神做得很好,https://github.com/sfsheng0322/MarqueeView 孙福生大神,然后...
    99+
    2022-06-06
    首页 淘宝 view 淘宝首页 动效 Android
  • Android TextView实现垂直滚动效果的方法
    本文实例讲述了Android TextView实现垂直滚动效果的方法。分享给大家供大家参考,具体如下: 在TextView中,如果文本很长,可能需要实现垂直滚动显示文本的效果。...
    99+
    2022-06-06
    方法 动效 Android
  • Android中TextView实现垂直滚动和上下滚动效果
    布局里面就是两个自定义的TextView,上面的左右滑动的是AutoHorizontalScrollTextView; 下面上下滚动的是AutoVerticalScrollT...
    99+
    2022-06-06
    动效 Android
  • vue实现垂直无限滑动日历组件
    用vue做了一个垂直无限滑动日历,在这里记录一下实现。 效果 组件 verticalCalendar.vue <template>   <div ref="con...
    99+
    2022-11-13
  • css3如何实现垂直翻转效果
    这篇文章主要介绍“css3如何实现垂直翻转效果”,在日常操作中,相信很多人在css3如何实现垂直翻转效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”css3如何实现垂直翻转...
    99+
    2022-10-19
  • css如何实现垂直伸缩效果
    本篇内容介绍了“css如何实现垂直伸缩效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!实现效果:实现代码:<!DOCTYPE&nbs...
    99+
    2023-07-04
  • CSS3如何模拟IOS实现滑动开关效果
    这篇文章主要介绍CSS3如何模拟IOS实现滑动开关效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!前言H5站点需要IOS滑动按钮的效果,想了想似乎CSS3能搞起,就折腾出来了......
    99+
    2022-10-19
  • Slider中怎么实现一个滑动条效果
    本篇文章给大家分享的是有关Slider中怎么实现一个滑动条效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。预览效果1:这个是仿Apple滑动...
    99+
    2022-10-19
  • js实现水平和竖直滑动条
    最近在做练手项目时候,需要用到滑动条,所以就稍微研究了一下。 首先来看水平滑动条,效果图如下: 代码如下: <html> <head> <m...
    99+
    2022-11-12
  • JS如何实现垂直手风琴效果
    这篇文章将为大家详细讲解有关JS如何实现垂直手风琴效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。效果如下:图 (1) 展开前图 (2) 展开后代码如下:<!DO...
    99+
    2022-10-19
  • iOS实现音频进度条效果
    前几天开发群里有一个老兄问了一个开发问题,他们的需求是要做一个类似音频进度条的东西,我感觉设计还不错,于是就写了个小demo供大家参考,在争得了他的同意的情况下写下这篇文章。 话不多...
    99+
    2022-06-05
    ios 音频 进度条
  • vue怎么实现垂直无限滑动日历组件
    这篇文章主要介绍“vue怎么实现垂直无限滑动日历组件”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue怎么实现垂直无限滑动日历组件”文章能帮助大家解决问题。效果组件verticalCalendar...
    99+
    2023-06-30
  • 怎么用css实现垂直时间线效果
    这篇文章主要介绍“怎么用css实现垂直时间线效果”,在日常操作中,相信很多人在怎么用css实现垂直时间线效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用css实现垂直...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作