iOS不规则按钮实现
来源:互联网 发布:求几本好看网络黄书 编辑:程序博客网 时间:2024/04/28 18:02
UIButton 在使用 png 作為按鈕時
即使點到透明的部份依然會觸發 touch 事件
為了達到更準確的點擊效果
需建立繼承 UIButton 的類別 (NonRectButton.h)
覆寫 -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 的觸發條件
和擴展 UIImage 類別的功能 (UIImage+GetPixelRGBA.h)
以配合 UIButton 圖片縮放取得點擊位置 pixel 資料
首先擴展 UIImage 功能 (add new category file)
UIImage+GetPixelRGBA.h@interface UIImage (GetPixelRGBA) -(UIColor *)colorAtPoint:(CGPoint)point WithImageSize:(CGSize)size; -(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize;@end
@implementation UIImage (GetPixelRGBA)//取得 point pixel color-(UIColor*)colorAtPoint:(CGPoint)point WithImageSize:(CGSize)size{ UIImage *resizeimage = [self reSizeImage:self toSize:size]; CGRect rect = CGRectMake(0.0f, 0.0f, resizeimage.size.width, resizeimage.size.height); if (CGRectContainsPoint(rect, point) == NO) {return nil;} CGImageRef image = resizeimage.CGImage; size_t width = CGImageGetWidth(image); size_t height = CGImageGetHeight(image); int bytesPerPixel = 4; int bytesPerRow = (bytesPerPixel*1); // 8bpp unsigned char pixelData[4] = {0, 0, 0, 0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); if (context == NULL) { NSLog(@"[colorAtPixel] Unable to create context!"); return nil; } CGContextSetBlendMode(context, kCGBlendModeCopy); CGFloat pointX = point.x; CGFloat pointY = height-point.y; CGContextTranslateCTM(context, -pointX, -pointY); CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), image); 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];}//更改 UIImage 大小-(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize{ UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height)); [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)]; UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return reSizeImage;}
再來建立 NonRectButton 類別並繼承 UIButton
並覆寫 -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 方法
NonRectButton.h#import <UIKit/UIKit.h>@interface NonRectButton : UIButton@end
#import "NonRectButton.h"#import "UIImage+GetPixelRGBA.h"@implementation NonRectButton-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { UIImage *image = [self backgroundImageForState:UIControlStateNormal]; if (image == nil) {return YES;} CGColorRef color = [[image colorAtPoint:point WithImageSize:self.frame.size] CGColor]; CGFloat alphaValue = CGColorGetAlpha(color); return (alphaValue >= 0.1f);}@end
End~
0 0
- iOS不规则按钮实现
- Android 实现不规则形状按钮
- cocos2dx 不规则按钮的实现
- Android不规则按钮实现方式
- 亲测:NGUI不规则按钮实现 !!!!
- Unity不规则按钮的实现
- iOS 不规则的UIImageView 实现
- win32 api 实现不规则形状按钮
- C#中实现不规则的按钮
- android不规则按钮的实现方式
- Unity3d笔记:如何实现不规则多边形按钮
- iOS九宫格布局按钮样式/不规则布局按钮样式/类似淘宝星星布局按钮样式
- Swing 实现简单的自定义不规则按钮组件
- Swing 实现简单的自定义不规则按钮组件2
- 通过检测png图片透明区实现不规则按钮
- WPF界面设计技巧—实现不规则动画按钮
- 使用GDI加载PNG实现自定义不规则按钮
- C++实现UMG中自定义不规则形状按钮
- T博士开讲 你听过却不懂的CPU专业名词
- mongo基本命令
- Wince 获取电源通知
- Swift版Facebook POP
- GRE考试高分作文的开头与结尾
- iOS不规则按钮实现
- 内核同步(死锁等,基础)
- [倚天屠龙记] vim 查找与替换(高级)
- Android初学习 - 以代码方式(非XML)创建一个布局
- c++ 实现结构体单链表的 创建 插入 删除等操作
- Linux GCC 编译 指令------- -c -o
- Android中通过intent启动另一个应用
- Windows平台下解决Oracle12c使用PDB数据库创建SDE的问题
- R语言画图——par()参数介绍