实现不规则形状的按钮
2013-07-18 16:46
148 查看
用到了四个文件:
UIImage+ColorAtPixel.h UIImage+ColorAtPixel.m OBShapedButton.h OBShapedButton.m
作者:Ole Begemann
View Code
使用:
使用IB,添加一个UIButton,把关联类选择为OBShapedButton,把Button的Type修改为Custom,添加一个异形的Image,然后编译运行即可看到效果。
UIImage+ColorAtPixel.h UIImage+ColorAtPixel.m OBShapedButton.h OBShapedButton.m
作者:Ole Begemann
/* Copyright (c) 2009 Ole Begemann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* OBShapedButton.m Created by Ole Begemann October, 2009 */ #import "OBShapedButton.h" #import "UIImage+ColorAtPixel.h" @interface OBShapedButton () @property (nonatomic, assign) CGPoint previousTouchPoint; @property (nonatomic, assign) BOOL previousTouchHitTestResponse; - (void)resetHitTestCache; @end @implementation OBShapedButton @synthesize previousTouchPoint = _previousTouchPoint; @synthesize previousTouchHitTestResponse = _previousTouchHitTestResponse; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self resetHitTestCache]; } return self; } - (id)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (self) { [self resetHitTestCache]; } return self; } #pragma mark - Hit testing - (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.bounds.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; } // Don't check again if we just queried the same point // (because pointInside:withEvent: gets often called multiple times) if (CGPointEqualToPoint(point, self.previousTouchPoint)) { return self.previousTouchHitTestResponse; } else { self.previousTouchPoint = point; } // 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]; BOOL response = NO; if (buttonImage == nil && buttonBackground == nil) { response = YES; } else if (buttonImage != nil && buttonBackground == nil) { response = [self isAlphaVisibleAtPoint:point forImage:buttonImage]; } else if (buttonImage == nil && buttonBackground != nil) { response = [self isAlphaVisibleAtPoint:point forImage:buttonBackground]; } else { if ([self isAlphaVisibleAtPoint:point forImage:buttonImage]) { response = YES; } else { response = [self isAlphaVisibleAtPoint:point forImage:buttonBackground]; } } self.previousTouchHitTestResponse = response; return response; } // Reset the Hit Test Cache when a new image is assigned to the button - (void)setImage:(UIImage *)image forState:(UIControlState)state { [super setImage:image forState:state]; [self resetHitTestCache]; } - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state { [super setBackgroundImage:image forState:state]; [self resetHitTestCache]; } - (void)resetHitTestCache { self.previousTouchPoint = CGPointMake(CGFLOAT_MIN, CGFLOAT_MIN); self.previousTouchHitTestResponse = NO; } @end
View Code
使用:
使用IB,添加一个UIButton,把关联类选择为OBShapedButton,把Button的Type修改为Custom,添加一个异形的Image,然后编译运行即可看到效果。
相关文章推荐
- Android 实现不规则形状按钮
- win32 api 实现不规则形状按钮
- C++实现UMG中自定义不规则形状按钮
- 亲测:NGUI不规则按钮实现 !!!!
- iOS不规则按钮实现
- 通过检测png图片透明区实现不规则按钮
- Qt5:不规则按钮的实现---通过贴图实现
- cocos2d-x中实现不规则按钮的点击效果
- android不规则按钮的实现方式
- cocos2d-x 不规则形状按钮的点击判定
- 使用GDI加载PNG实现自定义不规则按钮
- Paip.提升用户体验------在C++ Builder 不规则图片按钮实现总结
- cocos2d-x中实现不规则按钮的点击效果
- Unity3d笔记:如何实现不规则多边形按钮
- Qt简述如何实现不规则按钮
- html5 canvas实现不规则形状图片触发事件
- Path我就写一篇(更新:添加Region用法实现不规则按钮,2018.3.23)
- Android按钮shape形状资源代码实现
- C#基础温习(11):不规则按钮实现
- MFC实现不规则形状窗口