问一个关于手势解锁的问题

woaipb 2016-02-16 10:09:08
刚学ios没多久,现在学习手势解锁,在网上下了个示例程序,模仿着做了一个,示例程序是通过故事版设计的界面,我的是通过纯代码构建的,运行后发现有如下问题就是在滑动时,界面会闪动,删的比较厉害,滑动的路径都成曲折的:


示例程序:


我的代码如下:
Touchview .m
@interface Touchview : UIView
@property(nonatomic)NSMutableArray *bntarry;
@property(nonatomic)CGPoint currpoint;
@end
@implementation Touchview

-(id)initWithFrame:(CGRect)frame
{
if(self == [super initWithFrame:frame])
{
[self awakeFromNib];
}
return self;

}

-(NSMutableArray *)bntarry{
if (_bntarry ==nil) {
_bntarry = [NSMutableArray array];
}
return _bntarry;
}

-(void)awakeFromNib{
//用循环创建按钮
for (NSInteger i=0; i < 9; i++) {
UIButton *button = [[UIButton alloc]init];
//设置按钮默认图片
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
//设置按钮选中图片
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
//禁止交互
button.userInteractionEnabled = NO;
//给按钮一个 tag
button.tag = i+1;
//添加到view中
[self addSubview:button];
}
}

//设置按钮的 frame
-(void)layoutSubviews{
[super layoutSubviews];
//设置按钮的宽
CGFloat bntW = 75;
//设置按钮的高
CGFloat bntH = bntW;
//设置父控件的宽
CGFloat supW = self.frame.size.width;
//设置父控件的高
CGFloat supH = self.frame.size.height;

//竖直两边的间隔
CGFloat marginR = (supW - bntW *3)/4;
//横向两边的间隔
CGFloat marginC = (supH - bntH *3)/4;

for (NSInteger i=0; i < 9; i++) {
CGFloat bntX = marginR + (bntW + marginR) * (i / 3);
CGFloat bntY = marginC + (bntH + marginC) * (i % 3);
UIButton *bnt = [[UIButton alloc]init];
bnt = self.subviews[i];
bnt.frame = CGRectMake(bntX, bntY, bntW, bntH);
}

}

//手指点击
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取触控对象
UITouch *touch = [touches anyObject];
//获取触控对象的点坐标
CGPoint touchP = [touch locationInView:touch.view];
//判断点坐标是否在按钮区域内
for (NSInteger i=0; i < self.subviews.count; i++) {
//获取按钮
UIButton *bnt = self.subviews[i];
//判断这点是否在按钮区域内
if (CGRectContainsPoint(bnt.frame, touchP) && bnt.selected ==NO) {
//如果是则为选中状态
bnt.selected = YES;
//把选中的按钮添加到数组中
[self.bntarry addObject:bnt];
}
}
}

//手指移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建出最后一条没有连接的线路
UITouch *touch = [touches anyObject];
self.currpoint = [touch locationInView:touch.view];
[self touchesBegan:touches withEvent:event];

[self setNeedsDisplay];
}

//手指离开
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//把最后一个点的中心点赋值给属性记录的那个点
self.currpoint = [[self.bntarry lastObject] center];
//重绘
[self setNeedsDisplay];
//拼接字符
NSMutableString *strM = [[NSMutableString alloc]init];
for (NSInteger i=0; i < self.bntarry.count; i++) {
UIButton *bnt = [[UIButton alloc]init];
bnt = self.bntarry[i];
//拼接
[strM appendFormat:@"%@",@(bnt.tag)];
}
//移除所有按钮的选中状态
for (UIButton *bnt in self.bntarry) {
bnt.selected = NO;
}
//移除数组里面的子控件
[self.bntarry removeAllObjects];
//重绘
[self setNeedsDisplay];


NSLog(@"%@",strM);
}

- (void)drawRect:(CGRect)rect {
//如果数组不为空就执行
if (self.bntarry.count != 0) {
//绘制路径
UIBezierPath *path = [UIBezierPath bezierPath];
//设置线宽
path.lineWidth = 5;
//设置颜色
[[UIColor yellowColor]set];
//设置其实处样式
path.lineCapStyle = kCGLineCapRound;
//设置交点样式
path.lineJoinStyle = kCGLineJoinRound;
//设置起始点和线路
for (NSInteger i=0; i < self.bntarry.count; i++) {
UIButton * Bntcen = self.bntarry[i];
CGPoint cen = Bntcen.center;
if (i == 0 ) {
[path moveToPoint:cen];
}else{
[path addLineToPoint:cen];
}

}
//把最后一条线路添加进去
[path addLineToPoint:self.currpoint];
//绘制
[path stroke];
}
}
@end


TouchUnlock.m:
@implementation TouchUnlock

- (void)viewDidLoad {
[super viewDidLoad];
Touchview *TV = [[Touchview alloc] initWithFrame:CGRectMake(10,10, 200,200)];
[self.view addSubview:TV];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

请高手帮忙看看,究竟问题出在哪?
...全文
241 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ravatar0423 2016-03-12
  • 打赏
  • 举报
回复
发现另一种简单的方案,就是设置Touchview的背景色,即可
ravatar0423 2016-03-12
  • 打赏
  • 举报
回复
把你的代码放在Xcode运行,终于找到了原因: 请在drawRect最开始添加如下代码,清空上次的内容
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, self.bounds);
dMdM~ 2016-03-09
  • 打赏
  • 举报
回复
加个有颜色的VIew就可以了不是麽
dirdirdir3 2016-02-19
  • 打赏
  • 举报
回复
不要自己画,用图片来解决就好了,如滑到了某个键加上一个直线的图片就行

29,031

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧