iOS不规则按钮实现
2015-06-12 11:05
399 查看
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
UIImage+GetPixelRGBA.m
再來建立 NonRectButton 類別並繼承 UIButton
並覆寫 -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 方法
NonRectButton.h
NonRectButton.m
End~
即使點到透明的部份依然會觸發 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
UIImage+GetPixelRGBA.m
@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
NonRectButton.m
#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~
相关文章推荐
- IOS AVFoundation QRCode Scan
- IOS CIFilter QRCode Generate
- Xamarin iOS教程之申请付费开发者账号下载证书
- iOS 局部变量 setTag后可在别的地方调用 viewWithTag
- iOS开发中出现的奇葩问题
- IOS开发--源码篇 MBProgressHUD
- IOS已解决 在构建版本上传后, 一直处于处理状态
- IOS开发--枚举类型使用
- ios6、7 版本适配
- 那些在学习iOS开发前就应该知道的事(part 2)
- iOS 拨打电话功能
- iOS 数据持久化
- 第二章:nagios监控for windows
- iOS沙盒路径的查看和使用
- iOS autolayout的Content Hugging 和 Content Compression Resistance
- ios 中pch 文件里面的预编译命令符
- Xamarin iOS教程之申请付费开发者账号下载证书
- 44、自定义仿IOS对话框
- 大杂烩(二)
- Common Scenarios to avoid with DataWarehousing