亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

iOS多邊形按鍵的創建

標簽:
iOS

前几天项目需要,要做一个楼盘或者户型图的原生交互页面,
不清楚有没有更简单直白又高级的方法,我第一个想到的是创建一堆
多边形按钮。
所以我们就需要一个抽象的类,可以由贝赛尔曲线创建按键,是UIButton的子类

内容如下,如果大家有好的方案,请不吝赐教:

按键抽象类头文件

//
//  RLCShapeButton.h
//  duobianxing
//
//  Created by Realank on 16/2/16.
//  Copyright © 2016年 iMooc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RLCShapeButton : UIButton

- (instancetype)initWithPath:(UIBezierPath *)path andOrigin:(CGPoint)point;

@end

按键抽象类实现文件:

//
//  RLCShapeButton.m
//  duobianxing
//
//  Created by Realank on 16/2/16.
//  Copyright © 2016年 iMooc. All rights reserved.
//

#import "RLCShapeButton.h"

@interface RLCShapeButton ()

@property(nonatomic, strong) UIBezierPath *path;

@end
@implementation RLCShapeButton

- (instancetype)initWithPath:(UIBezierPath *)path andOrigin:(CGPoint)point{
    if (self = [super initWithFrame:CGRectMake(point.x, point.y, path.bounds.size.width, path.bounds.size.height)]) {
        _path = path;
        CAShapeLayer *shapLayer = [CAShapeLayer layer];
        shapLayer.path = self.path.CGPath;
        shapLayer.strokeColor = [UIColor redColor].CGColor;
        shapLayer.lineWidth = 5;
        shapLayer.fillColor = [UIColor clearColor].CGColor;
//        self.layer.mask = shapLayer;
        [self.layer addSublayer:shapLayer];
    }
    return self;
}

//覆盖方法,点击时判断点是否在path内,YES则响应,NO则不响应
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL res = [super pointInside:point withEvent:event];
    if (res)
    {
        if ([self.path containsPoint:point])
        {
            return YES;
        }
        return NO;
    }
    return NO;
}

@end

在ViewController中创建并使用这个按键类:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBezierPath* path = [[UIBezierPath alloc]init];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 50)];
    [path addLineToPoint:CGPointMake(100,100)];
    [path addLineToPoint:CGPointMake(0, 100)];
    [path closePath];

    RLCShapeButton *button = [[RLCShapeButton alloc]initWithPath:path andOrigin:CGPointMake(100, 100)];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

- (void)click {
    NSLog(@"click");
}

按键的大小,根据创建时提供的贝赛尔曲线大小,自动设置,是不是很方便呢~~

點擊查看更多內容
10人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
移動開發工程師
手記
粉絲
11
獲贊與收藏
446

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消