希望这很容易。一些谷歌搜索后,我想出了一个解决方案
UIParallelogramButton.h
#import
#import "QuartzCore/QuartzCore.h"
@interface UIParallelogramButton : UIButton
{
CGFloat offset;
}
@property CGFloat offset;
@end
UIParallelogramButton.m
#import "UIParallelogramButton.h"
@implementation UIParallelogramButton
@synthesize offset;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
offset = 0.0F;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
UIBezierPath* maskPath = [UIBezierPath bezierPath];
[maskPath moveToPoint:CGPointMake(rect.origin.x + offset, rect.origin.y)];
[maskPath addLineToPoint:CGPointMake(rect.size.width + rect.origin.x, rect.origin.y)];
[maskPath addLineToPoint:CGPointMake(rect.origin.x + rect.size.width - offset, rect.origin.y + rect.size.height)];
[maskPath addLineToPoint:CGPointMake(rect.origin.x, rect.origin.y + rect.size.height)];
[maskPath closePath];
CAShapeLayer* maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
@end