1、图片型
需求:根据UIButton里面的图片,处理点击的效果,点击图片的透明地方不需要响应事件。
解决思路:继承UIButton,获取点击点point对应的像素点的颜色透明度来判断(需要这个按钮透明)。
实现代码:
BBIrregularImageButton.h
@interface BBIrregularImageButton : UIButtonpublic methods.
@end
BBIrregularImageButton.m
#import "BBIrregularImageButton.h"
#import "UIImage+ColorAtPixel.h"
@implementation BBIrregularImageButton
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// 如果superResult = no(点击在bounds之外),直接返回NO,不处理
BOOL response = [super pointInside:point withEvent:event];
if (!response) {
return response;
}
// 判断设置有image
if (self.currentImage || self.currentBackgroundImage) {
// 重点
response = [self isAlphaVisibleAtPoint:point];
}
return response;
}
/**
截图
*/
-(UIImage *)shotViewImage{
UIImage *imageRet = [[UIImage alloc]init];
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
imageRet = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageRet;
}
/**
判断点击点像素的透明度
*/
- (BOOL)isAlphaVisibleAtPoint:(CGPoint)point
{
UIImage *shotViewImage = [self shotViewImage];// 截图button
UIColor *pixelColor = [shotViewImage colorAtPixel:point];// 获取图片某一像素点的颜色值,第三方
CGFloat alpha = 0.0;
[pixelColor getRed:NULL green:NULL blue:NULL alpha:&alpha];
return alpha >= 0.1;
}
@end
UIImage+ColorAtPixel.h
#import "UIImage+ColorAtPixel.h"
@implementation UIImage (ColorAtPixel)
//获取图片上某坐标点对应的像素的rgba值
- (UIColor *)colorAtPixel:(CGPoint)point{
//如果图片上不存在该点返回nil
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
return nil;
}
NSInteger pointX = truncl(point.x); //直接舍去小数,如1.58 -> 1.0
NSInteger pointY= truncl(point.y);
CGImageRef cgImage = self.CGImage;
NSUInteger width = self.size.width;
NSUInteger height = self.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //bitmap上下文使用的颜色空间
int bytesPerPixel = 4; //bitmap在内存中所占的比特数
int bytesPerRow = bytesPerPixel * 1; //bitmap的每一行在内存所占的比特数
NSUInteger bitsPerComponent = 8; //内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8.
unsigned char pixelData[4] = {0, 0, 0, 0}; //初始化像素信息
//创建位图文件环境。位图文件可自行百度 bitmap
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); //指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy); //当一个颜色覆盖上另外一个颜色,两个颜色的混合方式
CGContextTranslateCTM(context, -pointX, pointY - (CGFloat)height); //改变画布位置
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height ), cgImage); //绘制图片
CGContextRelease(context);
CGFloat red = (CGFloat)pixelData[0] / 255.0f;
CGFloat green = (CGFloat)pixelData[1] / 255.0f;
CGFloat blue = (CGFloat)pixelData[2] / 255.0f;
CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
UIColor *pointColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return pointColor;
}
@end
2、自画型
实现代码:
BBShapedButton.h
#import "BBShapedButton.h"
@implementation BBShapedButton
//绘制按钮时添加path遮罩
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CAShapeLayer *shapLayer = [CAShapeLayer layer];
shapLayer.path = self.path.CGPath;
self.layer.mask = shapLayer;
}
//覆盖方法,点击时判断点是否在path内,YES则响应,NO则不响应
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// 判断父类能不能响应点击
BOOL res = [super pointInside:point withEvent:event];
if (res){
// 再判断点击是不是在path内
res = [self.path containsPoint:point];
}
return res;
}
@end
使用例子
BBShapedButton *btn = [BBShapedButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 50, 200, 100);
// 设置UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 100)];
[path addLineToPoint:CGPointMake(20, 0)];
[path addLineToPoint:CGPointMake(45, 50)];
[path addLineToPoint:CGPointMake(70, 0)];
[path addLineToPoint:CGPointMake(150, 30)];
[path addLineToPoint:CGPointMake(175, 0)];
[path addLineToPoint:CGPointMake(200, 100)];
[path closePath];
btn.path = path;
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor greenColor];
[self.view addSubview:btn];