前几天项目需要,要做一个楼盘或者户型图的原生交互页面,
不清楚有没有更简单直白又高级的方法,我第一个想到的是创建一堆
多边形按钮。
所以我们就需要一个抽象的类,可以由贝赛尔曲线创建按键,是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人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦