iOS仿滚动轮盘键盘

#import <UIKit/UIKit.h>
@class JZNumPickerView;
@protocol JZNumPickerViewDelegate <NSObject>

@optional
/**
 *    按钮点击
 */
-(void)pickerView:(JZNumPickerView *)pickerView selectBtn:(UIButton*)sender;
/**
 *    某位数据发生变化
 */
-(void)pickerViewNumChange:(NSString *)numString;

@end

@interface JZNumPickerView : UIView

@property(nonatomic,strong)UIPickerView* myPickerView;
@property(nonatomic,strong)UIButton* choseKeyboard;

@property(nonatomic,strong)NSString* leftBtnStr;
@property(nonatomic,strong)NSString* rightBtnStr;
@property(nonatomic,assign)NSInteger dataNum; // 数据位数
@property(nonatomic,weak)id<JZNumPickerViewDelegate> delegate;

-(instancetype)initWithFrame:(CGRect)frame DataNum:(NSInteger)dataNum DecimalsNum:(NSInteger)decimalsNum;
/**
 *    改变数据显示
 */
-(void)changePickerViewNum:(NSString *)numString;

/**
 *  获取当前轮盘数据
 *
 *  @return 当前数据
 */
-(NSString *)getCurrentPickerViewNum;

@end

JZNumPickerView.h

#import "JZNumPickerView.h"
#import <math.h>

#define toolHeight 40
#define buttonWidth 60
#define cusKeyboardHeight 40
#define cusKeyboardWidth 40
#define cusPointWidth 15
#define cusBorderWidth 20
#define maxNum 16380

@interface JZNumPickerView  ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)UIButton* leftBtn;
@property(nonatomic,strong)UIButton* rightBtn;
//纪录停止时每一位数据,方便计算数据增减总数
@property(nonatomic,strong)NSMutableArray* lastDataArr;
//当前数据
@property(nonatomic,assign)NSInteger numOfNow;
//最大显示数据
@property(nonatomic,assign)NSInteger numOfMax;

//是否显示小数
@property(nonatomic,assign)NSInteger decimalsNum;
@end

@implementation JZNumPickerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}

-(void)setLeftBtnStr:(NSString *)leftBtnStr{

    if (self.leftBtn) {

        [self.leftBtn setTitle:leftBtnStr forState:UIControlStateNormal];
        [self.leftBtn setTitleColor:[UIColor barItemTintColor] forState:UIControlStateNormal];

    }
}

-(void)setRightBtnStr:(NSString *)rightBtnStr{

    if (self.rightBtn) {

        [self.rightBtn setTitle:rightBtnStr forState:UIControlStateNormal];
        [self.rightBtn setTitleColor:[UIColor barItemTintColor] forState:UIControlStateNormal];
    }
}

-(instancetype)initWithFrame:(CGRect)frame DataNum:(NSInteger)dataNum DecimalsNum:(NSInteger)decimalsNum{

    if (self = [super initWithFrame:frame]) {

        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        self.numOfNow = 0;
        self.dataNum = dataNum;
        self.decimalsNum = decimalsNum;
        [self creatView];
        [self loadData];
    }
    return self;
}

-(void)loadData{

    self.lastDataArr = [[NSMutableArray alloc] init];
    NSNumber *temp = [NSNumber numberWithInteger:maxNum/2];
    for (NSInteger i = self.dataNum - 1; i >= 0;i--) {
        if (i == self.dataNum - self.decimalsNum-1) {
            continue;
        }
        [self.lastDataArr addObject:temp];
    }
    [self updatePickerData];
    self.numOfMax = [self getMaxNumWithNum:self.dataNum - 1];
}

//-(void)updatePickerData{
//
//    NSInteger tempNum = self.numOfNow;
//    for (NSInteger i = self.dataNum - 1; i >= 0;i--) {
//        if (i == self.dataNum - 3) {
//            continue;
//        }
//        [self.myPickerView selectRow:maxNum/2 + (tempNum % 10) inComponent:i animated:NO];
//        [self.lastDataArr replaceObjectAtIndex:i > self.dataNum - 3 ? i - 1 : i withObject:[NSNumber numberWithInteger:maxNum/2 + (tempNum % 10)]];
//        tempNum /= 10;
//    }
//}

#warning
-(void)updatePickerData{

    NSInteger tempNum = self.numOfNow;
    for (NSInteger i = self.dataNum - 1; i >= 0;i--) {
        if (i == self.dataNum - self.decimalsNum-1) {
            continue;
        }
        [self.myPickerView selectRow:maxNum/2 + (tempNum % 10) inComponent:i animated:NO];
        [self.lastDataArr replaceObjectAtIndex: i > (self.dataNum - self.decimalsNum-1) ? i - 1 : i withObject:[NSNumber numberWithInteger:maxNum/2 + (tempNum % 10)]];
        tempNum /= 10;
    }
}

//#warning test
//-(void)updatePickerData{
//
//    NSInteger tempNum = self.numOfNow;
//    for (NSInteger i = self.dataNum - 1; i >= 0;i--) {
//        if (i == self.dataNum - 3) {
//            continue;
//        }
//        [self.myPickerView selectRow:(tempNum % 10) inComponent:i animated:NO];
//        [self.lastDataArr replaceObjectAtIndex:i > self.dataNum - 3 ? i - 1 : i withObject:[NSNumber numberWithInteger:(tempNum % 10)]];
//        tempNum /= 10;
//    }
//}

-(NSInteger)getMULWithNum:(NSInteger)num{

    NSInteger tempNum = 1;
    for (; num > 0; num--) {

        tempNum *= 10;
    }
    return tempNum;
}

-(NSInteger)getMaxNumWithNum:(NSInteger)num{

    NSInteger tempNum = 0;
    for (; num > 0; num--) {

        tempNum *= 10;
        tempNum += 9;
    }
    return tempNum;
}

-(void)creatView{

    self.myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, toolHeight, self.frame.size.width, self.frame.size.height - toolHeight - 35)];
    [self addSubview:self.myPickerView];
    self.myPickerView.delegate = self;
    self.myPickerView.dataSource = self;

    UIView *btnBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, toolHeight)];
    btnBackView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    [self addSubview:btnBackView];

    self.leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 2, buttonWidth, toolHeight - 2*2)];
    [self.leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.leftBtn];

    self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - buttonWidth, 2, buttonWidth, toolHeight - 2*2)];
    [self.rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.rightBtn];

    self.choseKeyboard = [UIButton buttonWithType:UIButtonTypeCustom];
    self.choseKeyboard.frame = CGRectMake(self.frame.size.width - cusKeyboardWidth, self.frame.size.height- cusKeyboardHeight, cusKeyboardWidth, cusKeyboardHeight);

    [self.choseKeyboard setImage:[UIImage imageNamed:@"ic_keyboard"] forState:UIControlStateNormal];
    [self.choseKeyboard setImage:[UIImage imageNamed:@"ic_keyboard_press"] forState:UIControlStateHighlighted];
    [self.choseKeyboard addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.choseKeyboard];

}

-(void)changePickerViewNum:(NSString *)numString{

    if (self.decimalsNum != 0) {
        self.numOfNow = [[NSString stringWithFormat:@"%f",[numString floatValue]*pow(10, self.decimalsNum)] floatValue];
    }else {
        self.numOfNow = [[NSString stringWithFormat:@"%.f",[numString floatValue]] floatValue];
    }
//    NSInteger numOfNowtest = self.numOfNow;
    [self updatePickerData];
}

-(NSString *)getCurrentPickerViewNum{

    if (self.decimalsNum != 0) {
//        CGFloat h = pow(10, -self.decimalsNum);
        return [NSString stringWithFormat:@"%f",(int)self.numOfNow * pow(10, -self.decimalsNum)];
    }else {
        return [NSString stringWithFormat:@"%.f",(self.numOfNow*1.0)];
    }
    //return [NSString stringWithFormat:@"%.2f",((float)self.numOfNow)*0.01];
}

#pragma mark- 事件响应
-(void)btnAction:(UIButton *)sender{

    if ([sender.titleLabel.text isEqualToString:@"取消"]) {

        [self dismissPopover];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"JZYBCBScrollviewfamechange" object:nil];

    }
    if ([self.delegate respondsToSelector:@selector(pickerView:selectBtn:)]) {

        [self.delegate pickerView:self selectBtn:sender];
    };
}

#pragma mark- UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return self.dataNum;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component == self.dataNum - self.decimalsNum-1) {
        return 1;
    }
    return maxNum;
}

#pragma mark- UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 28;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (self.decimalsNum != 0) {
        if (component == (self.dataNum - (self.decimalsNum+1))) {

            return cusPointWidth;
        };
        return (self.frame.size.width-self.decimalsNum*cusPointWidth-cusBorderWidth)/(self.dataNum-1);
    }else {
        return (self.frame.size.width-cusBorderWidth)/(self.dataNum+1);
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if (component == (self.dataNum - self.decimalsNum-1)) {

        return @".";
    };
    return [NSString stringWithFormat:@"%ld",row % 10];
}

//-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//
//    //差值
//    NSInteger num = row - [[self.lastDataArr objectAtIndex:component > self.dataNum - 3 ? component - 1 : component] integerValue];
//    if (num <= 0) {
//        NSMutableString *mstr = [[NSMutableString alloc]init];
//        for (int i = 0; i < self.dataNum ; i++) {
//            if (i != self.dataNum - 3) {
//                [mstr appendFormat:@"%ld",[pickerView selectedRowInComponent:i] % 10];
//            }
//        }
//        self.numOfNow = [mstr integerValue];
//    }
//    else
//    {
//        self.numOfNow += num * [self getMULWithNum:component > self.dataNum - 3 ? self.dataNum - component - 1: self.dataNum - component - 2];
//        if (self.numOfNow > self.numOfMax) {
//            self.numOfNow -= self.numOfMax + 1;
//            self.numOfNow = pow(10, self.dataNum) -1;
//        }else if (self.numOfNow < 0){
//
//            self.numOfNow += self.numOfMax + 1;
//        }
//    }
//    [self updatePickerData];
//
//    if (self.delegate && [self.delegate respondsToSelector:@selector(pickerViewNumChange:)]) {
//
//        [self.delegate pickerViewNumChange:[self getCurrentPickerViewNum]];
//    }
//}
//
#warning
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    //差值
    NSInteger num = row - [[self.lastDataArr objectAtIndex:component > (self.dataNum - self.decimalsNum-1) ? component - 1 : component] integerValue];
    if (num <= 0) {
        NSMutableString *mstr = [[NSMutableString alloc]init];
        for (int i = 0; i < self.dataNum ; i++) {
            if (i != self.dataNum - self.decimalsNum-1) {
                [mstr appendFormat:@"%ld",[pickerView selectedRowInComponent:i] % 10];
            }
        }
        self.numOfNow = [mstr integerValue];
    }
    else
    {
        self.numOfNow += num * [self getMULWithNum:component > (self.dataNum - self.decimalsNum-1)? self.dataNum - component - 1: self.dataNum - component - 2];

        if (self.numOfNow > self.numOfMax) {
            self.numOfNow -= self.numOfMax + 1;
            self.numOfNow = pow(10, self.dataNum) -1;
//            NSLog(@"现在的数%ld",self.numOfNow);
        }else if (self.numOfNow < 0){

            self.numOfNow += self.numOfMax + 1;
        }
    }
    [self updatePickerData];

    if (self.delegate && [self.delegate respondsToSelector:@selector(pickerViewNumChange:)]) {

        [self.delegate pickerViewNumChange:[self getCurrentPickerViewNum]];
    }
}
////更改字体大小
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//    UILabel* pickerLabel = (UILabel*)view;
//    pickerLabel = [[UILabel alloc]initWithFrame:view.bounds];
//    [pickerLabel setFont:[UIFont boldSystemFontOfSize:20]];
//    if (self.decimalsNum != 0) {
//        if (component > self.dataNum -self.decimalsNum-1)
//        {
//            [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
//        }
//    }
//    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
//    pickerLabel.textAlignment = NSTextAlignmentCenter;
//    return pickerLabel;
//
//}

@end

JZNumPickerView.m

时间: 2024-11-06 16:45:19

iOS仿滚动轮盘键盘的相关文章

IOS ScrollView滚动控件使用

1.ScrollView概述: ScrollView是UIScrollView类,是容器类型的视图. 它有两个子类: UITextView和UITableView,它们在内容超出屏幕时提供水平或垂直滚动条. 2.其包含3个属性: contentSize:表示SCrollView中的内容视图 contentInset:用于在ScrollView中的内容视图周边添加边框,这往往为了留出空白以放置工具栏.标签或导航栏等 contentOffset:内容视图坐标原点与ScrollVIew坐标原点的偏移量

iOS仿京东分类菜单之UICollectionView内容

 iOS仿京东分类菜单之UICollectionView内容 在 上<iOS仿京东分类菜单实例实现>已经实现了大部分主体的功能,本文是针对右边集合列表进行修改扩展,使它达到分组的效果,本文涉及到的主要是UICollectionView的知识内容,左边列表的实现见上一篇文章,先看实现的效果图: 一:实体的创建 1.1分组实体的创建(tagID跟左边表格进行关联,roomArray是存放房间的数组,也就是单元格的集合) #import <Foundation/Foundation.h>

iOS_仿QQ表情键盘

当UITextFiled和UITextView这种文本输入类控件成为第一响应者时,弹出的键盘由他们的一个UIView类的inputView属性来控制,当inputView为nil时会弹出系统的键盘,想要弹出自定义的键盘,将我们自定义的UIView对象给inputView属性赋值即可.表情键盘重点在于排列各个表情和删除键,以及表情键盘上的各种回调设置: 下面为键盘预览图,兼容了竖屏各版本适配,横屏没有兼顾.横屏适配参见这篇博客iOS之自定义表情键盘 图1为6的常用表情,图2为6的全部表情,图3为5

ios登陆页以及键盘关闭demo

1.新建一个single view 的project 2.另外新建两个类(非必要) DElabel.h DETextField.h 将共用属性以及方法都放类当中,特殊属性以及方法直接放VC中 3.声明全局变量tfuser tfpass两个textfield 4.键盘关闭关键在新建一个背景,让背景触发事件让两个textfield失去控制权. =========================== // //  DEViewController.m //  testLoginPage // // 

iOS 开发之 - 关闭键盘 退出键盘 的5种方式

iOS 开发之 - 关闭键盘 退出键盘 的5种方式 1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5.关于键盘遮蔽的问题 1,点击编辑区以外的地方(UIView) 这是一种很直觉的方法,当不再需要使用虚拟键盘时,只要点击虚拟键盘和编辑区域外的地方,就可以将键盘收起,下面程式码是在 UIView 中内建的触碰事件方法函式,您可以参考 Touch Panel / 触碰萤幕 / 压力感应器的基本使用方式一文

IOS UItableView 滚动到底 触发事件

开发过程中,在使用UItableView 总会遇到加载更多的问题,到底是手势响应瀑布流的方法好? 还是添加一个底端cell点击触发加载更多好?我也想有自己的判断.但是我们老板总说了算,没办法,谁叫我给人家打工呢? cell触发式略,这个很简单. 下边讲下怎么响应瀑布流. 主要有上拉刷新 和下提加载 这两种操作.关键是判断何时执行:滚动到最上边,滚动到最下边 给出一个方法,目前我是这么做的,有更好的方法我再备份. - (void)scrollViewDidEndDragging:(UIScroll

[iOS]UITableViewController完成收回键盘操作

UITableViewController 本身能够实现键盘适配(cell中控件焦点会移动到键盘上方 在做键盘收回的时候思考过如下方案 1.tableview添加点击事件 结果:点击事件和tableview的didselect 冲突,导致didselect失效 2.scrollview代理滚动收回键盘 结果:目的可以达到,但是当点击textfield的时候,此时键盘会出现之后直接收回.原因是先适配→调用scrollview代理. 最后采用如下方案 如图: <span style="font

iOS文字滚动效果 之纵向滚动

原文链接: iOS文字滚动效果 之纵向滚动 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

iOS 关于滚动视图contentSize、contentOffset、contentInset 的整理

iOS 关于滚动视图contentSize.contentOffset.contentInset 的整理 contentSize 是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍. contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是