OC 不规则的UIButton点击效果

####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];
复制代码

下载demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值