九宫格手势解锁
由于沒有圖片素材實(shí)現(xiàn)效果比較簡(jiǎn)單》
重新添加一個(gè)繼承與UIView 的類
YFNineFunctionView.h
初始化時(shí)加載按鈕,并設(shè)置屬性
- (instancetype)init {self = [super init];if (self) {//初始化時(shí)加載視圖[self setUpView];}return self; } //加載九個(gè)視圖按鈕,并且設(shè)置按鈕屬性 -(void)setUpView{for (int i = 0; i < 9; i++) {UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];btn.userInteractionEnabled = NO;[btn.layer setMasksToBounds:YES];//設(shè)置圓形按鈕btn.layer.cornerRadius = 40;btn.userInteractionEnabled = NO;//設(shè)置邊框btn.layer.borderWidth = 2.0;//[btn.layer setBorderWidth:2.0];//a設(shè)置邊框顏色CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){217/255.0,240/255.0,255/255.0,1});btn.layer.borderColor = color;[self addSubview:btn];} }
在layoutSubviews中布局按鈕位置
-(void)layoutSubviews{[super layoutSubviews];float Width = 80;float Height = 80;float margon = 30;for (int i = 0; i < 9; i++) {UIButton *btn = (UIButton *)self.subviews[i];btn.tag = i+1;btn.frame = CGRectMake((i/3)*(Width + margon), (i%3)*(Width + margon), Width, Height); } }
處理視圖的一系列觸摸事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)even{CGPoint point = [self getCurrentPoint:touches];UIButton *btn = [self getCurrentButton:point];if (btn&&btn.selected != YES) {btn.selected = YES;[self.buttons addObject:btn];}[self setNeedsDisplay]; } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{CGPoint point = [self getCurrentPoint:touches];UIButton *btn = [self getCurrentButton:point];if (btn&&btn.selected != YES) {btn.selected = YES;[self.buttons addObject:btn];}[self setNeedsDisplay]; } -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{} //獲得點(diǎn)擊點(diǎn) -(CGPoint)getCurrentPoint:(NSSet*)touches{UITouch *touch = [touches anyObject];CGPoint point = [touch locationInView:touch.view];return point; } //獲得點(diǎn)擊處的按鈕 -(UIButton *)getCurrentButton:(CGPoint)point{for (UIButton *btn in self.subviews) {//判斷point是否在按鈕內(nèi)if (CGRectContainsPoint(btn.frame, point)) {return btn;}}return nil; }
繪制線條
//繪圖 -(void)drawRect:(CGRect)rect{//獲取上下文CGContextRef ctx = UIGraphicsGetCurrentContext();for (int i = 0; i < self.buttons.count; i++) {UIButton *btn=self.buttons[i];if (0==i) {//設(shè)置起點(diǎn)(注意連接的是中點(diǎn))// CGContextMoveToPoint(ctx, btn.frame.origin.x, btn.frame.origin.y);CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);}else{// CGContextAddLineToPoint(ctx, btn.frame.origin.x, btn.frame.origin.y);CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);}}CGContextSetLineWidth(ctx, 10);CGContextSetRGBStrokeColor(ctx, 20/255.0, 107/255.0, 153/255.0, 1);CGContextStrokePath(ctx); }
?
FNineFunctionViewYFNineFunctionView
總結(jié)
- 上一篇: 安卓AccountManager数据库存
- 下一篇: Samsara v2.0 MIS数据流脚