`
ydbc
  • 浏览: 738305 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

iphone 不规则button

 
阅读更多

大体思路 如下

检测hit 点击返回来的色值 如果返回是透明 则说明没在button内部



以下为 公共代码

UIImage+ColorAtPixel.h


#import <UIKit/UIKit.h>


@interface UIImage (ColorAtPixel)


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


@end


UIImage+ColorAtPixel.m


#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 = self.size.width;

NSUInteger height = self.size.height;

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-(CGFloat)height);

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


OBShapedButton.h

#import <UIKit/UIKit.h>

#define kAlphaVisibleThreshold (0.1f)

@interface OBShapedButton : UIButton

{


}


@end


OBShapedButton.m


#import "OBShapedButton.h"

#import "UIImage+ColorAtPixel.h"

@implementation OBShapedButton


- (BOOL)isAlphaVisibleAtPoint:(CGPoint)point forImage:(UIImage *)image

{

// Correct point to take into account that the image does not have to be the same size

// as the button. See https://github.com/ole/OBShapedButton/issues/1

CGSize iSize = image.size;

CGSize bSize = self.frame.size;

point.x *= (bSize.width != 0) ? (iSize.width / bSize.width) : 1;

point.y *= (bSize.height != 0) ? (iSize.height / bSize.height) : 1;


CGColorRef pixelColor = [[image colorAtPixel:point] CGColor];

CGFloat alpha = CGColorGetAlpha(pixelColor);

return alpha >= kAlphaVisibleThreshold;

}



// 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 imageForState:UIControlStateNormal];

UIImage *buttonBackground = [self backgroundImageForState:UIControlStateNormal];


if (buttonImage == nil && buttonBackground == nil) {

return YES;

}

else if (buttonImage != nil && buttonBackground == nil) {

return [self isAlphaVisibleAtPoint:point forImage:buttonImage];

}

else if (buttonImage == nil && buttonBackground != nil) {

return [self isAlphaVisibleAtPoint:point forImage:buttonBackground];

}

else {

if ([self isAlphaVisibleAtPoint:point forImage:buttonImage]) {

return YES;

} else {

return [self isAlphaVisibleAtPoint:point forImage:buttonBackground];

}

}

}

@end



git 地址 :https://github.com/ole/OBShapedButton.git



分享到:
评论

相关推荐

    Iphone 无规则按钮

    你可以根据需求绘制任意复杂的路径,例如不规则的多边形、曲线等。 2. **设置层属性**:将定义好的UIBezierPath设置为按钮的CAShapeLayer的path属性。这将使按钮的边界跟随自定义路径。 ```swift let shapeLayer...

    ios不规则按钮

    本文将深入探讨如何在iPhone和iPad上利用图片实现不规则形状的按钮,以及相关的技术要点。 首先,iOS系统默认的UIButton类提供的是矩形形状的按钮,但这并不限制我们创建自定义形状。为了实现不规则形状,我们需要...

    cocos2d-iphone之魔塔20层第七部分

    开发者需要熟练使用Cocos2d-iPhone的Label、Button等UI组件,并处理好触摸事件。 6. **存档与加载系统**:为了使玩家能够随时保存进度,开发者需要实现存档和加载功能。这通常涉及JSON或plist文件的读写,以及数据...

    Android 仿iphone 气泡短信 DEMO.zip

    气泡的形状通常使用Path对象来描绘,通过添加不同类型的线段(直线、曲线)来形成不规则的多边形。颜色可以通过设置Paint对象的color属性来改变,而阴影效果则可以使用`setShadowLayer()`方法添加。为了处理不同方向...

    Building iPhone Apps with HTML, CSS, and JavaScript

    document.getElementById("button").addEventListener("click", function() { alert("按钮被点击了!"); }); ``` - **DOM操作:**通过`document.getElementById()`等方法获取并操作页面元素。 - **事件处理:**...

    iOS 设计模式 工厂模式

    例如,可能需要创建不同类型的视图,或者根据不同平台(iPhone、iPad)加载不同的UI资源。工厂模式可以帮助我们优雅地处理这类问题。 工厂模式通常包含三个主要角色: 1. **抽象产品(Abstract Product)**: 定义了...

    iOSUI基础控件常用方法探微

    对于不规则形状的图片,可能需要使用 `UIViewContentModeScaleAspectFill` 模式。在这种模式下,图片同样按比例缩放,但会确保填满整个 UIImageView 区域,可能导致图片某些部分被裁剪。 示例代码: ```swift ...

    使用form-create动态生成vue自定义组件和嵌套表单组件

    formCreate.maker.radio('是否显示', 'is_show').options([{ value: 1, label: '显示' }, { value: 0, label: '不显示' }]) ] } ] } ]; ``` 这种嵌套结构使得构建复杂的表单布局变得容易,每个子组件都可以...

    打造优质产品体验的文案设计规范.docx

    不加标点符号:为了帮助用户更加高效地扫视文本内容,以下 6 个元素单独出现时,可以省略不必要的标点符号标签(Tag)、标题、输入框内辅助文字提示、输入框下的辅助文字提示、气泡卡片(Popover)、按钮(Button)...

    在b/s开发中经常用到的javaScript技术

    1.4 整数不能大于iMax 1.5 整数不能小于iMin 2、时间类 2.1 短时间,形如 (13:04:06) 2.2 短日期,形如 (2003-12-05) 2.3 长时间,形如 (2003-12-05 13:04:06) 2.4 只有年和月。形如(2003-05,或者2003-5) 2.5...

    JAVA上百实例源码以及开源项目源代码

    此时此景,笔者只专注Android、Iphone等移动平台开发,看着这些源码心中有万分感慨,写此文章纪念那时那景! Java 源码包 Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这...

Global site tag (gtag.js) - Google Analytics