• Touch event in certain color rect


     UIImage+ColorAtPixel category

    #import <UIKit/UIKit.h>

    /*

     A category on UIImage that enables you to query the color value of arbitrary 

     pixels of the image.

     */

    @interface UIImage (ColorAtPixel)

    - (UIColor *)colorAtPixel:(CGPoint)point;

    @end

    #import <CoreGraphics/CoreGraphics.h>

    #import "UIImage+ColorAtPixel.h"

    @implementation UIImage (ColorAtPixel)

    /*

     Returns the color of the image pixel at point. Returns nil if point lies outside the image bounds.

     If the point coordinates contain decimal parts, they will be truncated.

     To get at the pixel data, this method must draw the image into a bitmap context.

     For minimal memory usage and optimum performance, only the specific requested

     pixel is drawn.

     If you need to query pixel colors for the same image repeatedly (e.g., in a loop),

     this approach is probably less efficient than drawing the entire image into memory

     once and caching it.

     */

    - (UIColor *)colorAtPixel:(CGPoint)point {

        // Cancel if point is outside image coordinates

        if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {

            return nil;

        }

        // Create a 1x1 pixel byte array and bitmap context to draw the pixel into.

        // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage

        NSInteger pointX = trunc(point.x);

        NSInteger pointY = trunc(point.y);

        CGImageRef cgImage = self.CGImage;

        NSUInteger width = CGImageGetWidth(cgImage);

        NSUInteger height = CGImageGetHeight(cgImage);

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        int bytesPerPixel = 4;

        int bytesPerRow = bytesPerPixel * 1;

        NSUInteger bitsPerComponent = 8;

        unsigned char pixelData[4] = { 0, 0, 0, 0 };

        CGContextRef context = CGBitmapContextCreate(pixelData, 

                                                     1,

                                                     1,

                                                     bitsPerComponent, 

                                                     bytesPerRow, 

                                                     colorSpace,

                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

        CGColorSpaceRelease(colorSpace);

        CGContextSetBlendMode(context, kCGBlendModeCopy);

        // Draw the pixel we are interested in onto the bitmap context

        CGContextTranslateCTM(context, -pointX, -pointY);

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);

        CGContextRelease(context);

        // Convert color values [0..255] to floats [0.0..1.0]

        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;

        return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

    }

    @end

    Custom button

    #define kAlphaVisibleThreshold (0.1f);

    @interface OBShapedButton : UIButton {

        // Our class interface is empty. OBShapedButton only overwrites one method of UIView.

        // It has no attributes of its own.

    }

    @end

    #import "OBShapedButton.h"

    #import "UIImage+ColorAtPixel.h"

    @implementation OBShapedButton

    // UIView uses this method in hitTest:withEvent: to determine which subview should receive a touch event.

    // If pointInside:withEvent: returns YES, then the subview’s hierarchy is traversed; otherwise, its branch

    // of the view hierarchy is ignored.

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

        // Return NO if even super returns NO (i.e., if point lies outside our bounds)

        BOOL superResult = [super pointInside:point withEvent:event];

        if (!superResult) {

            return superResult;

        }

        // We can't test the image's alpha channel if the button has no image. Fall back to super.

        UIImage *buttonImage = [self backgroundImageForState:UIControlStateNormal];

        if (buttonImage == nil) {

            return YES;

        }

        CGColorRef pixelColor = [[buttonImage1 colorAtPixel:point] CGColor];//[[buttonImage1 colorAtPixel:point] CGColor];

        CGFloat alpha = CGColorGetAlpha(pixelColor);

        return alpha >= kAlphaVisibleThreshold;

    }

    @end

  • 相关阅读:
    Charles抓包工具
    JQuery 实现表单验证,所有验证通过方可提交
    卡巴斯基注册信息清除
    Nginx 404 Not Found 解决办法
    php mysql 多表查询之子查询语句
    搜狗拼音、QQ拼音输入法、2345拼音输入法、百度输入法 、手心输入法对比。(个人体会)
    Notepad++使用-如何导出/导入配置文件
    深蓝词库转换2.4版发布,支持最新的搜狗用户词库备份bin格式
    网站更换服务器,百度站长后台抓取诊断时间
    阿里云代金券领取
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2458489.html
Copyright © 2020-2023  润新知