iOS实现双向滑动条效果_IOS

最近做项目,碰到一种双向滑动条,自己实现了一下,随便写一下思路,方便以后开发,避免重复写代码,以后粘贴就行了。封装了一下,代码如下:

#import <UIKit/UIKit.h>

typedef NSString* (^HLDoubleSlideViewSwitchStrBock)(CGFloat count);

@interface HLDoubleSlideView : UIView

@property(nonatomic,assign)CGFloat maxValue;
@property(nonatomic,assign)CGFloat minValue;
@property(nonatomic,assign)CGFloat currentLeftValue;
@property(nonatomic,assign)CGFloat currentRightValue;

//格式化显示文本
@property(nonatomic,copy)HLDoubleSlideViewSwitchStrBock block;

@end

源文件如下:

#import "HLDoubleSlideView.h"
#import "UIView+Add.h"

@interface HLDoubleSlideView ()<UIGestureRecognizerDelegate>

@property(nonatomic,strong)UIImageView *leftImageView;
@property(nonatomic,strong)UIImageView *rightImageView;
@property(nonatomic,strong)UILabel *leftLabel;
@property(nonatomic,strong)UILabel *rightLabel;

@property(nonatomic,strong)UIButton *leftBtn;
@property(nonatomic,strong)UIButton *rightBtn;

@property(nonatomic,assign)CGFloat leftBtnOrgx;
@property(nonatomic,assign)CGFloat rightBtnOrgx;

@end

@implementation HLDoubleSlideView

-(id)init
{
 if (self = [super init]) {
  [self setupUI];
 }
 return self;
}

-(void)setupUI
{

 _leftImageView = [[UIImageView alloc] init];
 _leftImageView.image = [UIImage imageNamed:@"progressImage"];
 _leftImageView.frame = CGRectMake(0, 5, 60, 40);
 [self addSubview:_leftImageView];

 _leftLabel = [[UILabel alloc] initWithFrame:_leftImageView.bounds];
 _leftLabel.backgroundColor = [UIColor clearColor];
 _leftLabel.font = [UIFont systemFontOfSize:13];
 _leftLabel.textAlignment = NSTextAlignmentCenter;
 _leftLabel.textColor = [UIColor whiteColor];
 [_leftImageView addSubview:_leftLabel];

 _rightImageView = [[UIImageView alloc] init];
 _rightImageView.image = [UIImage imageNamed:@"progressImage"];
 _rightImageView.frame = CGRectMake(0, 5, 60, 40);
 [self addSubview:_rightImageView];

 _rightLabel = [[UILabel alloc] initWithFrame:_rightImageView.bounds];
 _rightLabel.backgroundColor = [UIColor clearColor];
 _rightLabel.font = [UIFont systemFontOfSize:13];
 _rightLabel.textAlignment = NSTextAlignmentCenter;
 _rightLabel.textColor = [UIColor whiteColor];
 [_rightImageView addSubview:_rightLabel];

 _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 _leftBtn.frame = CGRectMake(0, 50, 20,20);
 _leftBtn.backgroundColor = [UIColor blueColor];
 _leftBtn.layer.cornerRadius = 10;
 [self addSubview:_leftBtn];
 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
 panGesture.delegate = self;
 [_leftBtn addGestureRecognizer:panGesture];

 _leftImageView.centerX = _leftBtn.centerX;

 _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 _rightBtn.backgroundColor = [UIColor blueColor];
 _rightBtn.frame = CGRectMake(240, 50, 20, 20);
 _rightBtn.layer.cornerRadius = 10;
 panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
 panGesture.delegate = self;
 [_rightBtn addGestureRecognizer:panGesture];
 _rightImageView.centerX = _rightBtn.centerX;

 [self addSubview:_rightBtn];

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
 return YES;
}

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
 NSLog(@"doubleView hitTest");
 return [super hitTest:point withEvent:event];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
 NSLog(@"began");
 [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
 NSLog(@"move");
 [super touchesMoved:touches withEvent:event];
}

-(void)layoutSubviews
{

 CGFloat centenX = (_currentLeftValue - _minValue) * (self.bounds.size.width - 20)/(_maxValue - _minValue) + 10;
 _leftBtn.centerX = centenX;

 if (_currentLeftValue != 0) {
  CGFloat centenX = (_currentRightValue - _minValue) * (self.bounds.size.width - 20) / (_maxValue - _minValue) + 10;
  _rightBtn.centerX = centenX;

 }
 else
 {
  _rightBtn.centerX = self.bounds.size.width - 10;

 }

 _leftImageView.centerX = _leftBtn.centerX;
 _rightImageView.centerX = _rightBtn.centerX;
 if (_block) {
  _leftLabel.text = _block(_currentLeftValue);
  _rightLabel.text = _block(_currentRightValue);
 }
}

-(void)tapGestureAction:(UIPanGestureRecognizer*)panGesture
{
 UIView *vw = panGesture.view;

 CGPoint transPoint = [panGesture translationInView:self];
 NSLog(@"x:%lf,y:%lf",transPoint.x,transPoint.y);

 switch (panGesture.state) {
  case UIGestureRecognizerStateBegan:
  {
   if ([vw isEqual:_leftBtn])
   {
    _leftBtnOrgx = _leftBtn.orgX;
    NSLog(@"拖拽左边按钮");

   }
   else if([vw isEqual:_rightBtn])
   {
    _rightBtnOrgx = _rightBtn.orgX;
    NSLog(@"拖拽右边按钮");
   }

  }
   break;
  case UIGestureRecognizerStateChanged:
  {

   if ([vw isEqual:_leftBtn])
   {

    CGFloat orginX = _leftBtn.orgX;
    _leftBtn.orgX = _leftBtnOrgx + transPoint.x;
    if (_leftBtn.orgX < 0) {
     _leftBtn.orgX = 0;
    }
    else if(_leftBtn.orgX >= _rightBtn.orgX - 20)
    {
     _leftBtn.orgX = orginX;
    }
     _leftImageView.centerX = _leftBtn.centerX;
   }
   else if([vw isEqual:_rightBtn])
   {
    CGFloat orginX = _rightBtn.orgX;
    _rightBtn.orgX = _rightBtnOrgx + transPoint.x;
    if (_rightBtn.orgX >= self.bounds.size.width - 20) {
     _rightBtn.orgX = self.bounds.size.width - 20;
    }
    else if(_rightBtn.orgX <= _leftBtn.orgX + 20)
    {
     _rightBtn.orgX = orginX;
    }
     _rightImageView.centerX = _rightBtn.centerX;
   }

  }
   break;
  case UIGestureRecognizerStateEnded:
  {

  }
   break;

  default:
   break;
 }
 _currentLeftValue = _minValue + (_maxValue - _minValue) * ((_leftBtn.centerX - 10) / (self.bounds.size.width - 20));
 _currentRightValue = _minValue + (_maxValue - _minValue) * ((_rightBtn.centerX - 10) / (self.bounds.size.width - 20));
 if (_block) {
  _leftLabel.text = _block(_currentLeftValue);
  _rightLabel.text = _block(_currentRightValue);
 }

 NSLog(@"leftValue:%lf,rightValue:%lf",_currentLeftValue,_currentRightValue);

 [self setNeedsDisplay];
}

-(void)setCurrentLeftValue:(CGFloat)currentLeftValue
{
 _currentLeftValue = currentLeftValue;
 CGFloat centenX = (currentLeftValue - _minValue) * (self.bounds.size.width - 20)/(_maxValue - _minValue) + 10;
 _leftBtn.centerX = centenX;
 [self setNeedsDisplay];
}

-(void)setCurrentRightValue:(CGFloat)currentRightValue
{
 _currentRightValue = currentRightValue;
 CGFloat centenX = (_currentRightValue - _minValue) * (self.bounds.size.width - 20) / (_maxValue - _minValue) + 10;
 _rightBtn.centerX = centenX;
 [self setNeedsDisplay];

}

-(void)drawRect:(CGRect)rect
{
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineCap(context, kCGLineCapRound);
 CGContextSetLineWidth(context, 3);
 [[UIColor grayColor] setStroke];
 CGContextMoveToPoint(context, 0, 60);
 CGContextAddLineToPoint(context, self.bounds.size.width, 60);
 CGContextStrokePath(context);

 [[UIColor redColor] setStroke];
 CGContextMoveToPoint(context, _leftBtn.orgX + 10, 60);
 CGContextAddLineToPoint(context, _rightBtn.orgX,60);
 CGContextStrokePath(context);

}

@end

使用如下:

 HLDoubleSlideView *doubleSlideView = [[HLDoubleSlideView alloc] init];
 doubleSlideView.backgroundColor = [UIColor whiteColor];//HLColor(244, 244, 244);
 doubleSlideView.minValue = 1000;
 doubleSlideView.maxValue = 10000;
 doubleSlideView.block = ^NSString*(CGFloat count)
 {
  return [NSString stringWithFormat:@"%.0f元",count];
 };
 [self.view addSubview:doubleSlideView];

 doubleSlideView.frame = CGRectMake(60, 64, 250, 80);

 doubleSlideView.currentLeftValue = 1200;
 doubleSlideView.currentRightValue = 10000;

运行结果如下:

demo:https://github.com/jiangtaidi/HLDoubleSlideView.git

以上就是本文的全部内容,希望对大家的学习有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索iOS双向滑动条
, iOS滑动条
, iOS双向滑动
iOS滑动
ios 双向滑动、qt实现窗口滑动效果、vue实现页面滑动效果、js实现滑动效果、实现页面左右滑动效果,以便于您获取更多的相关知识。

时间: 2024-09-29 10:02:39

iOS实现双向滑动条效果_IOS的相关文章

双向滑动条选择急。。。。。。求大神指教

问题描述 双向滑动条选择急......求大神指教 解决方案 http://gundumw100.iteye.com/blog/2001720

Slider 滑动条效果

这个滑动条(拖动条)效果,一开始是参考了BlueDestiny的slider和Apple产品展示的样式,做了程序的原型. 在做了拖放效果之后,我想应该可以做一个更好的了,所以重做一遍,完善和扩展了一些功能. 碍于时间没有做得很强大,都是一些基本功能,希望各位多提意见! 效果预览 预览效果1: 这个是仿Apple滑动条产品展示效果. 除了原来的效果(包括点击缓动滑移.拖动滑移),我还加入了本程序特有的滚轮和键盘控制,滑动条两端鼠标放上去会自动滑动,滑动到两边还会自动换一个表示停止的图案 预览效果2

IOS实现点击滑动抽屉效果_IOS

最近,看到好多Android上的抽屉效果,也忍不住想要自己写一个.在Android里面可以用SlidingDrawer,很方便的实现.IOS上面就只有自己写了.其实原理很简单就是 UIView 的移动,和一些手势的操作. 效果图: // // DrawerView.h // DrawerDemo // // Created by Zhouhaifeng on 12-3-27. // Copyright (c) 2012年 CJLU. All rights reserved. // #import

iOS自定义collectionView实现毛玻璃效果_IOS

先来看看效果图,由于录屏软件不给力,毛玻璃效果不明显,请见谅. 步骤详解: 说下思路,很简单,首先自定义一个collectionView, 重写它的initWithFrame:collectionViewLayout:方法,在这里面做配置,这里用的是AXECollectionView. 与之对应的自定义一个collectionViewCell,在cell里配置操作:设置layer涂层,加载图片等操作,这里用的是AXECollectionViewCell. 最后在需要展示的控制器里调用AXECol

iOS实现左右拖动抽屉效果_IOS

本文实例介绍了iOS实现左右拖动抽屉效果,具体内容如下 利用了触摸事件滑动 touchesMoved: 来触发左右视图的出现和消失 利用loadView方法中添加view 在self.view载入前就把 左右中View都设置好frame 每一个方法都由单独的功能. #import "DarwViewController.h" @interface DarwViewController () @property (nonatomic, weak) UIView *leftView; @p

iOS实现侧拉栏抽屉效果_IOS

本文实例介绍了iOS实现侧拉栏抽屉效果的相关代码,分享给大家供大家参考,具体内容如下 需要导入第三方的类库如下: 抽屉效果所需第三方类库下载 效果:既可以两侧都实现抽屉效果也可只实现左侧栏或者右侧栏的抽屉效果 关于抽屉效果主要是AppDelegate的代码 AppDelegate.h文件代码: <span style="font-size:18px;"><span style="font-size:18px;">#import <UIK

高仿网易新闻顶部滑动条效果实现代码_Android

这个是网易新闻的主界面,我们知道底部可以用tabhost实现,这个很容易,我们在其他软件中也会经常用到. 至于顶部的滑动条,个人感觉还是比较漂亮的所以今天也模仿了下,网易顶部滑动条的效果,由于初次模仿这种效果,可能有些地方还不够完美,不过基本已经实现,希望大家能够喜欢. 废话不多说,下面上代码: 首先是布局layout下的main.xml 复制代码 代码如下: <?xmlversion="1.0"encoding="utf-8"?> <Relati

高仿网易新闻顶部滑动条效果实现代码

这个是网易新闻的主界面,我们知道底部可以用tabhost实现,这个很容易,我们在其他软件中也会经常用到. 至于顶部的滑动条,个人感觉还是比较漂亮的所以今天也模仿了下,网易顶部滑动条的效果,由于初次模仿这种效果,可能有些地方还不够完美,不过基本已经实现,希望大家能够喜欢. 废话不多说,下面上代码: 首先是布局layout下的main.xml 复制代码 代码如下: <?xmlversion="1.0"encoding="utf-8"?> <Relati

iOS 屏幕解锁文字动画效果_IOS

   CAGradientLayer相信大家都比较熟悉,关于其如何使用,我就不在这里废话了,网上有很多介绍,想详细了解的话,可以去看看.我只简单说下如何利用CAGradientLayer制作屏幕解锁文字动画效果. 1.创建一个CAGradientLayer对象gradLayer,设置它的frame和label.bounds一样(这里要注意一下是Label的bounds不是frame); 2.这里我给gradLayer.colors一共设置了三个颜色值,首尾颜色透明度设置成0.3,中间的颜色值透明