如何绘制平行四边形按钮 [英] How to draw a parallelogram button

查看:195
本文介绍了如何绘制平行四边形按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制一个具有平行四边形形状的自定义按钮。做这个的最好方式是什么?图像方式(即为控件设置背景图像)不合适。

I need to draw a custom button with parallelogram's shape. What is the best way to do this? The image-way (i.e. to set background image for the control) is not suitable.

推荐答案

希望很简单。经过一番谷歌搜索后,我想出了解决方案

Hopefully, it was very easy. After some googling I came up with the solution

UIParallelogramButton.h

#import <UIKit/UIKit.h>
#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

这篇关于如何绘制平行四边形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆