CustomSliderView
2016-02-04 15:22
447 查看
自定义的一个SliderView
手势拖动,也可以点击小的节点。按钮个数可自定义。拖动图片也可自行更换。拖动到两个点之间会有吸附效果,跳到距离更近的点上。
如图
代码:
#import "ViewController.h"
#import "CustomSliderView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CustomSliderView *slider = [[CustomSliderView alloc]initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-10, 50)];
slider.value = 6;
[self.view addSubview:slider];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface CustomSliderView : UIView
/**
* 显示到第几个按钮 从0开始 0~(btnCount-1)
*/
@property (nonatomic,assign) CGFloat value;
@end
最后附上demo 下载地址:https://github.com/likangios/CustomSliderViewDemo 非常简单的Demo高手忽略。
手势拖动,也可以点击小的节点。按钮个数可自定义。拖动图片也可自行更换。拖动到两个点之间会有吸附效果,跳到距离更近的点上。
如图
代码:
#import "ViewController.h"
#import "CustomSliderView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CustomSliderView *slider = [[CustomSliderView alloc]initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-10, 50)];
slider.value = 6;
[self.view addSubview:slider];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface CustomSliderView : UIView
/**
* 显示到第几个按钮 从0开始 0~(btnCount-1)
*/
@property (nonatomic,assign) CGFloat value;
@end
#import "CustomSliderView.h" @interface CustomSliderView ()<UIGestureRecognizerDelegate> { int btnCount;//按钮个数 int btnSpace;//按钮之间间隙 } @property (nonatomic,strong) UIView *clipView; @property (nonatomic,strong) UIImageView *touchView; @end @implementation CustomSliderView -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { btnCount = 10;//小按钮的个数 // 默认灰色图片 UIImage *BottomLineImg = [UIImage imageNamed:@"btn_jian_gray"]; // 高亮图片 UIImage *TopLineImg = [UIImage imageNamed:@"btn_jian_red"]; // 拖动按钮图片 UIImage *TouchBtnImg = [UIImage imageNamed:@"btn_yuan"]; // 中间小按钮图片 // UIImage *BtnsImg = [UIImage imageNamed:@""]; // UIImage *BtnHightlightImg = [UIImage imageNamed:@""]; UIImageView *bottomImageview = [[UIImageView alloc]init]; bottomImageview.image = BottomLineImg; bottomImageview.bounds = CGRectMake(0, 0, frame.size.width-TouchBtnImg.size.width, 2); bottomImageview.center = CGPointMake(CGRectGetWidth(frame)/2.0, CGRectGetHeight(frame)/2.0); [self addSubview:bottomImageview]; _clipView = [[UIView alloc]initWithFrame:CGRectMake(bottomImageview.frame.origin.x, bottomImageview.frame.origin.y,0, CGRectGetHeight(bottomImageview.frame))]; _clipView.clipsToBounds = YES; [self addSubview:_clipView]; UIImageView *topImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, CGRectGetWidth(bottomImageview.frame), CGRectGetHeight(bottomImageview.frame))]; topImageview.image = TopLineImg; [_clipView addSubview:topImageview]; btnSpace = (frame.size.width-TouchBtnImg.size.width)/(btnCount-1); for (int i = 0; i<btnCount; i++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.bounds = CGRectMake(0, 0, 13, 13); btn.center = CGPointMake(TouchBtnImg.size.width/2.0+btnSpace*i, frame.size.height/2.0); btn.tag = i+1; btn.backgroundColor = [UIColor whiteColor]; btn.layer.borderColor = [UIColor grayColor].CGColor; btn.layer.borderWidth = 3.0; btn.layer.cornerRadius = CGRectGetWidth(btn.frame)/2.0; btn.layer.masksToBounds = YES; // [btn setImage:BtnsImg forState:UIControlStateNormal]; // [btn setImage:BtnHightlightImg forState:UIControlStateHighlighted]; [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn]; } _touchView = [[UIImageView alloc]init]; _touchView.bounds = CGRectMake(0, 0, TouchBtnImg.size.width, TouchBtnImg.size.height); _touchView.center = CGPointMake(TouchBtnImg.size.width/2.0, frame.size.height/2.0); _touchView.image = TouchBtnImg; _touchView.backgroundColor = [UIColor clearColor]; _touchView.userInteractionEnabled = YES; [self addSubview:_touchView]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(TouchViewpan:)]; [_touchView addGestureRecognizer:pan]; } return self; } //TouchView 手势处理 - (void)TouchViewpan:(UIPanGestureRecognizer *)pan{ CGPoint point = [pan locationInView:self.touchView]; CGFloat origin_x = self.touchView.frame.origin.x; origin_x += point.x; origin_x = MIN(origin_x, self.frame.size.width-CGRectGetWidth(self.touchView.frame)/2.0); origin_x = MAX(origin_x, CGRectGetWidth(self.touchView.frame)/2.0); self.touchView.center = CGPointMake(origin_x, self.touchView.center.y); _clipView.frame = CGRectMake(_clipView.frame.origin.x, _clipView.frame.origin.y,CGRectGetMinX(self.touchView.frame), _clipView.frame.size.height); if (pan.state == UIGestureRecognizerStateEnded) { [self UpdateTouchViewOriginX:[self getTouchViewOriginX]]; }else if (pan.state == UIGestureRecognizerStateChanged){ [self setBtnSelected:self.touchView.frame.origin.x/btnSpace+1]; } } // 计算 touchView 最接近按钮的位置 - (CGFloat)getTouchViewOriginX{ CGFloat touchConstant = self.touchView.frame.origin.x; CGFloat Space = btnSpace/2.0; CGFloat count = touchConstant/Space; NSInteger index = ceil(count/2.0); // [self setBtnSelected:index]; return index*btnSpace; } // 小按钮点击 - (void)BtnClick:(id)sender { UIButton *btn = (UIButton *)sender; NSInteger tag = btn.tag; [self UpdateTouchViewOriginX:(tag-1)*btnSpace]; [self setBtnSelected:tag]; } //更新TouchView - (void)UpdateTouchViewOriginX:(CGFloat)constant{ [UIView animateKeyframesWithDuration:0.2 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{ self.touchView.frame = CGRectMake(constant,CGRectGetMinY(self.touchView.frame), CGRectGetWidth(self.touchView.frame), CGRectGetHeight(self.touchView.frame)); _clipView.frame = CGRectMake(_clipView.frame.origin.x, _clipView.frame.origin.y,CGRectGetMinX(self.touchView.frame), _clipView.frame.size.height); } completion:^(BOOL finished) { NSLog(@"constant is %f tag is %.0f",constant,constant/btnSpace); }]; } //更新小按钮 - (void)setBtnSelected:(NSInteger)tag{ for (int i = 0; i<btnCount; i++) { UIButton *btn = (UIButton *)[self viewWithTag:i+1]; if (i<tag) { btn.backgroundColor = [UIColor redColor]; }else{ btn.backgroundColor = [UIColor whiteColor]; } } } // 属性设置 点击第几个按钮 - (void)setValue:(CGFloat)value{ value = MIN(value, btnCount-1); value = MAX(value, 0); [self UpdateTouchViewOriginX:value*btnSpace]; [self setBtnSelected:value]; } @end
最后附上demo 下载地址:https://github.com/likangios/CustomSliderViewDemo 非常简单的Demo高手忽略。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- 检测iOS设备是否越狱的方法
- 基于Android中手势交互的实现方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- 实例详解用户输入 i. 检测常用手势
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事
- IOS 改变键盘颜色代码