足球盘口软件
当前位置: 足球盘口软件 > 前端 >
教你如何实现手势密码足球盘口软件:

iOS 教你如何实现手势密码,ios实现手势密码

本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果

同样先上效果图

足球盘口软件 1

其实就是对画图功能的一个实现,再加上手势操作结合起来

 

屏幕宽度高度,方便下面操作,不做解释

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height

#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

控制器.m文件

这里的imageView是用来装手势画图之后的image,看后面就清楚了

1 @property (nonatomic,strong)NSMutableArray *buttonArr;//全部手势按键的数组
2 @property (nonatomic,strong)NSMutableArray *selectorArr;//选中手势按键的数组
3 @property (nonatomic,assign)CGPoint startPoint;//记录开始选中的按键坐标
4 @property (nonatomic,assign)CGPoint endPoint;//记录结束时的手势坐标
5 @property (nonatomic,strong)UIImageView *imageView;//画图所需

添加九个按键,设置状态图片,实际开发中一般有三种状态,即默认,选中正确和选择错误,错误一般指的是我们要记录下用户的手势密码,需要用户

画出两次相同的手势密码才能保存,若两次输入不一致,就是错误状态的一种,当然还包括其它的,不多说了

这里要强调

 btn.userInteractionEnabled = NO;
这句的重要性,如果不关闭按键的用户交互,下面的UITouch则无法在按键中触发,所以这里必须关闭

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     self.view.backgroundColor = [UIColor whiteColor];
 4 
 5    
 6     if (!_buttonArr) {
 7         _buttonArr = [[NSMutableArray alloc]initWithCapacity:9];
 8     }
 9     
10     self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
11     [self.view addSubview:self.imageView];
12 
13     for (int i=0; i<3; i++) {
14         for (int j=0; j<3; j++) {
15             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
16             btn.frame = CGRectMake(ScreenWidth/12+ScreenWidth/3*j, ScreenHeight/3+ScreenWidth/3*i, ScreenWidth/6, ScreenWidth/6);
17             [btn setImage:[UIImage imageNamed:@"pbg"] forState:UIControlStateNormal];
18             [btn setImage:[UIImage imageNamed:@"pbg01"] forState:UIControlStateHighlighted];
19             btn.userInteractionEnabled = NO;
20             [self.buttonArr addObject:btn];
21             [self.imageView addSubview:btn];
22         }
23         
24     }
25 }

这个方法就是实现画图的方法

 1 -(UIImage *)drawLine{
 2     UIImage *image = nil;
 3     
 4     UIColor *col = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
 5     UIGraphicsBeginImageContext(self.imageView.frame.size);//设置画图的大小为imageview的大小
 6     CGContextRef context = UIGraphicsGetCurrentContext();
 7     CGContextSetLineWidth(context, 5);
 8     CGContextSetStrokeColorWithColor(context, col.CGColor);
 9     
10     CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);//设置画线起点
11 
12     //从起点画线到选中的按键中心,并切换画线的起点
13     for (UIButton *btn in self.selectorArr) {
14         CGPoint btnPo = btn.center;
15         CGContextAddLineToPoint(context, btnPo.x, btnPo.y);
16         CGContextMoveToPoint(context, btnPo.x, btnPo.y);
17     }
18     //画移动中的最后一条线
19     CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
20     
21     CGContextStrokePath(context);
22     
23     image = UIGraphicsGetImageFromCurrentImageContext();//画图输出
24     UIGraphicsEndImageContext();//结束画线
25     return image;
26 }

最后部分是手势,每次在屏幕上点击的时候都会调用的方法

 1 //开始手势
 2 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 3 {
 4     UITouch *touch = [touches anyObject];//保存所有触摸事件
 5     if (touch) {
 6         
 7         
 8         for (UIButton *btn in self.buttonArr) {
 9             
10             CGPoint po = [touch locationInView:btn];//记录按键坐标
11             
12             if ([btn pointInside:po withEvent:nil]) {//判断按键坐标是否在手势开始范围内,是则为选中的开始按键
13                 
14                 [self.selectorArr addObject:btn];
15                 btn.highlighted = YES;
16                 self.startPoint = btn.center;//保存起始坐标
17             }
18         
19         }
20         
21     }
22     
23 }
24 
25 //移动中触发,画线过程中会一直调用画线方法
26 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
27 {
28     UITouch *touch = [touches anyObject];
29     if (touch) {
30         
31         self.endPoint = [touch locationInView:self.imageView];
32         for (UIButton *btn in self.buttonArr) {
33             CGPoint po = [touch locationInView:btn];
34             if ([btn pointInside:po withEvent:nil]) {
35                 
36                 BOOL isAdd = YES;//记录是否为重复按键
37                 for (UIButton *seBtn in self.selectorArr) {
38                     if (seBtn == btn) {
39                         isAdd = NO;//已经是选中过的按键,不再重复添加
40                         break;
41                     }
42                 }
43                 if (isAdd) {//未添加的选中按键,添加并修改状态
44                     [self.selectorArr addObject:btn];
45                     btn.highlighted = YES;
46                 }
47                 
48             }
49         }
50     }
51     self.imageView.image = [self drawLine];//每次移动过程中都要调用这个方法,把画出的图输出显示
52     
53 }
54 //手势结束触发
55 -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
56 {
57     self.imageView.image = nil;
58     self.selectorArr = nil;
59     for (UIButton *btn in self.buttonArr) {
60         btn.highlighted = NO;
61     }
62 }

开发中有时需要在最后时把画出的手势密码图显示保留一秒时,不能直接使用上面的画图image输出多一次,因为输出的连最后一条线都画出来了,如果要实现这个保留效果,

可以在画线方法里添加一个是否画最后一条线的判断,加个bool传参,在画线结束时再调用这个方法和参数,禁止最后一条线画出来就行了,当然不能在画的过程禁止,而是在结束的时候,不然一条线都画不出的,最后把图片展示多次就行了

需要的把btn和密码相关联,方法也有很多种,例如给btn设置tag值,把tag对应作为密码保存和验证就行了

教你如何实现手势密码,ios实现手势密码 本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果 同样先上...

ios 顶部tab

主要使用了scrolview做的

//

// HJPagerViewController.h

// MusicLove

//

// Created by niuxinghua on 15/1/29.

// Copyright (c) 2015年 Hjojo. All rights reserved.

//

#import

@interface HJPagerViewController :UIViewController

@property(nonatomic,strong)UIScrollView* scrolview;

@property(nonatomic,strong)UIButton* btn1;

@property(nonatomic,strong)UIButton* btn2;

@property(nonatomic,strong)UIButton* btn3;

@property(nonatomic,strong)UIImageView* imageLine;

@property(nonatomic,strong)UITableView* tableView;

@end

/

//

// HJPagerViewController.m

// MusicLove

//

// Created by niuxinghua on 15/1/29.

// Copyright (c) 2015年 Hjojo. All rights reserved.

//

#import "HJPagerViewController.h"

#import "MJRefresh.h"

NSString *const MJTableViewCellIdentifier = @"Cell1";

/**

* 随机数据

*/

#define MJRandomData [NSString stringWithFormat:@"随机数据---%d", arc4random_uniform(1000000)]

@interface HJPagerViewController ()

@property (strong, nonatomic) NSMutableArray *fakeData;

@end

@implementation HJPagerViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.tabBarController.tabBar.hidden=YES;

self.fakeData=[[NSMutableArray alloc]init];

CGFloat mainWith=[UIScreen mainScreen].bounds.size.width;

CGFloat mainHeight=[UIScreen mainScreen].bounds.size.height;

_btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 66, mainWith/3, 40)];

_btn1.backgroundColor=[UIColor colorWithRed:242/255.0 green:87/255.0 blue:96/255.0 alpha:1.0f];

// [_btn1 setBackgroundColor:[UIColor blackColor]];

[_btn1 setTitle:@"距离" forState:UIControlStateNormal];

_btn2=[[UIButton alloc]initWithFrame:CGRectMake(mainWith/3, 66, mainWith/3, 40)];

_btn2.backgroundColor=[UIColor colorWithRed:242/255.0 green:87/255.0 blue:96/255.0 alpha:1.0f];

[_btn2 setTitle:@"价格" forState:UIControlStateNormal];

_btn3=[[UIButton alloc]initWithFrame:CGRectMake(mainWith/3*2, 66, mainWith/3, 40)];

_btn3.backgroundColor=[UIColor colorWithRed:242/255.0 green:87/255.0 blue:96/255.0 alpha:1.0f];

[_btn3 setTitle:@"筛选" forState:UIControlStateNormal];

_imageLine=[[UIImageView alloc]initWithFrame:CGRectMake(0, 104, mainWith/3, 5)];

_imageLine.center=CGPointMake(mainWith/6, 107.5);

[_imageLine setBackgroundColor:[UIColor greenColor]];

_scrolview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 111, mainWith, mainHeight-111)];

_scrolview.delegate=self;

_scrolview.pagingEnabled=YES;

_scrolview.showsHorizontalScrollIndicator=NO;

_scrolview.bounces=NO;

_scrolview.contentSize=CGSizeMake(mainWith*3, mainHeight-111);

self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, mainWith, mainHeight-111)];

// 2.集成刷新控件

[self setupRefresh];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MJTableViewCellIdentifier];

self.tableView.delegate=self;

self.tableView.dataSource=self;

UIImageView* image1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mainWith, mainHeight-111)];

UIImageView* image2=[[UIImageView alloc]initWithFrame:CGRectMake(mainWith, 0, mainWith, mainHeight-111)];

UIImageView* image3=[[UIImageView alloc]initWithFrame:CGRectMake(mainWith*2, 0, mainWith, mainHeight-111)];

image1.backgroundColor=[UIColor redColor];

image2.backgroundColor=[UIColor blackColor];

image3.backgroundColor=[UIColor greenColor];

[_scrolview addSubview:self.tableView];

[_scrolview addSubview:image2];

[_scrolview addSubview:image3];

[self.view addSubview:_btn1];

[self.view addSubview:_btn2];

[self.view addSubview:_btn3];

[self.view addSubview:_scrolview];

[self.view addSubview:_imageLine];

[_btn1 setTag:101];

[_btn2 setTag:102];

[_btn3 setTag:103];

[_btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

[_btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

[_btn3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)btnClicked:(UIButton*)sender{

int tag=[sender tag];

if (tag==101) {

[UIView animateWithDuration:0.5f animations:^{

_imageLine.center=CGPointMake(self.view.bounds.size.width/6, _imageLine.center.y);

_scrolview.contentOffset=CGPointMake(0, 0);

}];

}

else if (tag==102){

[UIView animateWithDuration:0.5f animations:^{

_imageLine.center=CGPointMake(self.view.bounds.size.width/2, _imageLine.center.y);

_scrolview.contentOffset=CGPointMake(self.view.bounds.size.width, 0);

}];

}

else{

[UIView animateWithDuration:0.5f animations:^{

_imageLine.center=CGPointMake(self.view.bounds.size.width/6*5, _imageLine.center.y);

_scrolview.contentOffset=CGPointMake(self.view.bounds.size.width*2, 0);

}];

}

}

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{

_imageLine.center=CGPointMake(scrollView.contentOffset.x/3+self.view.bounds.size.width/6, _imageLine.center.y);

}

/**

* 集成刷新控件

*/

- (void)setupRefresh

{

// 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)

// [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];

// dateKey用于存储刷新时间,可以保证不同界面拥有不同的刷新时间

[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];

[self.tableView headerBeginRefreshing];

// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)

[self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];

// 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)

self.tableView.headerPullToRefreshText = @"下拉可以刷新了";

self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";

self.tableView.headerRefreshingText = @"正在刷新中。。。";

self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";

self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";

self.tableView.footerRefreshingText = @"正在加载中。。。";

}

#pragma mark 开始进入刷新状态

- (void)headerRereshing

{

// 1.添加假数据

for (int i = 0; i<5; i++) {

[self.fakeData insertObject:MJRandomData atIndex:0];

}

// 2.模拟2秒后刷新表格UI(真实开发中,可以移除这段gcd代码)

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

// 刷新表格

[self.tableView reloadData];

// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态

[self.tableView headerEndRefreshing];

});

}

- (void)footerRereshing

{

// 1.添加假数据

for (int i = 0; i<5; i++) {

[self.fakeData addObject:MJRandomData];

}

// 2.模拟2秒后刷新表格UI(真实开发中,可以移除这段gcd代码)

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

// 刷新表格

[self.tableView reloadData];

// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态

[self.tableView footerEndRefreshing];

});

}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//{

// return 5;

//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return self.fakeData.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MJTableViewCellIdentifier forIndexPath:indexPath];

cell.textLabel.text = self.fakeData[indexPath.row];

NSLog(@"%@",self.fakeData[indexPath.row]);

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

足球盘口软件 2

顶部tab 主要使用了scrolview做的 // // HJPagerViewController.h // MusicLove // // Created by niuxinghua on 15/1/29. // Copyright (c) 2015年 Hjojo. All rights reserved....

上一篇:没有了 下一篇:第十一页,开发面试题
返回顶部