日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

九宫格手势解锁

發布時間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 九宫格手势解锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于沒有圖片素材實現效果比較簡單》


重新添加一個繼承與UIView 的類

YFNineFunctionView.h

初始化時加載按鈕,并設置屬性

- (instancetype)init {self = [super init];if (self) {//初始化時加載視圖[self setUpView];}return self; } //加載九個視圖按鈕,并且設置按鈕屬性 -(void)setUpView{for (int i = 0; i < 9; i++) {UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];btn.userInteractionEnabled = NO;[btn.layer setMasksToBounds:YES];//設置圓形按鈕btn.layer.cornerRadius = 40;btn.userInteractionEnabled = NO;//設置邊框btn.layer.borderWidth = 2.0;//[btn.layer setBorderWidth:2.0];//a設置邊框顏色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{} //獲得點擊點 -(CGPoint)getCurrentPoint:(NSSet*)touches{UITouch *touch = [touches anyObject];CGPoint point = [touch locationInView:touch.view];return point; } //獲得點擊處的按鈕 -(UIButton *)getCurrentButton:(CGPoint)point{for (UIButton *btn in self.subviews) {//判斷point是否在按鈕內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) {//設置起點(注意連接的是中點)// 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); }

?

FNineFunctionView

YFNineFunctionView



總結

以上是生活随笔為你收集整理的九宫格手势解锁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。