平行四辺形の形をしたカスタムボタンを描く必要があります。これを行うための最良の方法は何ですか?image-way(つまり、コントロールの背景画像を設定する)は適切ではありません。
1840 次
2 に答える
5
ボタンの画像を設定する必要がない場合は、 Brain89が提供するソリューションが適しています。背景とコンテンツの画像があり、画像を変更せずに平行四辺形のボタンを作成する必要があります。次のコードを使用します:
// Pass 0..1 value to either skewX if you want to compress along the X axis
// or skewY if you want to compress along the Y axis
- (void)parallelogramButtonWithButton:(UIButton *)button withSkewX:(CGFloat)skewX withSkewY:(CGFloat)skewY {
CGAffineTransform affineTransform = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMake(1, skewY, skewX, 1, 0, 0));
button.layer.affineTransform = affineTransform;
}
于 2014-05-14T11:11:56.743 に答える
4
うまくいけば、それは非常に簡単でした。いくつかのグーグルの後、私は解決策を思いついた
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
于 2012-09-12T11:30:55.237 に答える