您的位置:首页 > 其它

实现不规则形状的按钮

2013-07-18 16:46 148 查看
用到了四个文件:

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,然后编译运行即可看到效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: