0

Paintcode を使用して生成したカスタム ボタン クラスがあります。次のようになります。

//
//  TGCustomConfirmButton.m
//  Indego
//
//  Created by 
//  Copyright
//

#import "TGCustomConfirmButton.h"

@implementation TGCustomConfirmButton

+ (TGCustomConfirmButton *)buttonWithType:(UIButtonType)type
{
  return [super buttonWithType:UIButtonTypeCustom];
}

- (void)drawRect:(CGRect)rect
{
  NSLog(@"drawRect enter");

  //// General Declarations
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef context = UIGraphicsGetCurrentContext();

  //// Color Declarations
  UIColor* strokeColor =    [UIColor colorWithRed: 0.143 green: 0.429 blue: 0 
                                            alpha: 1];
  UIColor* shadowColor2 =   [UIColor colorWithRed: 0.712 green: 0.972 blue: 0.489 
                                            alpha: 1];
  UIColor* gradientColor =  [UIColor colorWithRed: 0.391 green: 0.925 blue: 0.262 
                                            alpha: 1];
  UIColor* gradientColor2 = [UIColor colorWithRed: 0.052 green: 0.454 blue: 0.044 
                                            alpha: 1];
  UIColor* fillColor2 =     [UIColor colorWithRed: 0.833 green: 0.833 blue: 0.833 
                                            alpha: 1];
  UIColor* gradientColor3 = [UIColor colorWithRed: 1 green: 1 blue: 1 
                                            alpha: 1];

  //// Gradient Declarations
  NSArray* greenHighlightColors = [NSArray arrayWithObjects:
                                   (id)gradientColor.CGColor,
                                   (id)[UIColor colorWithRed: 0.221 green: 0.69 
                                            blue: 0.153 alpha: 1].CGColor,
                                   (id)gradientColor2.CGColor, nil];
  CGFloat greenHighlightLocations[] = {0, 0.23, 0.58};
  CGGradientRef greenHighlight = CGGradientCreateWithColors(colorSpace, 
                                            (__bridge CFArrayRef)greenHighlightColors,
                                              greenHighlightLocations);
  NSArray* innerShadow002Colors = [NSArray arrayWithObjects:
                                   (id)fillColor2.CGColor,
                                   (id)[UIColor colorWithRed: 0.917 green: 0.917 
                                            blue: 0.917 alpha: 1].CGColor,
                                   (id)gradientColor3.CGColor, nil];
  CGFloat innerShadow002Locations[] = {0, 0, 0.66};
  CGGradientRef innerShadow002 = CGGradientCreateWithColors(colorSpace, 
                                            (__bridge CFArrayRef)innerShadow002Colors,
                                             innerShadow002Locations);

  //// Shadow Declarations
  UIColor* shadow3 = shadowColor2;
  CGSize shadow3Offset = CGSizeMake(0.1, 3.1);
  CGFloat shadow3BlurRadius = 0;

  //// Rounded Rectangle 2 Drawing
  UIBezierPath* roundedRectangle2Path = [UIBezierPath bezierPathWithRoundedRect: 
                                        CGRectMake(36, 913, 577, 126) cornerRadius: 11];
  CGContextSaveGState(context);
  [roundedRectangle2Path addClip];
  CGContextDrawLinearGradient(context, innerShadow002, CGPointMake(324.5, 913),
                                             CGPointMake(324.5, 1039), 0);
  CGContextRestoreGState(context);


  //// Rounded Rectangle Drawing
  UIBezierPath* roundedRectanglePath=[UIBezierPath bezierPathWithRoundedRect: 
                                     CGRectMake(48.5, 926, 550, 86.5) cornerRadius: 6];
  CGContextSaveGState(context);
  [roundedRectanglePath addClip];
  CGContextDrawLinearGradient(context, greenHighlight, CGPointMake(323.5, 926),
                                    CGPointMake(323.5, 1012.5), 0);
  CGContextRestoreGState(context);

  ////// Rounded Rectangle Inner Shadow
  CGRect roundedRectangleBorderRect = CGRectInset([roundedRectanglePath bounds], 
                                            -shadow3BlurRadius, -shadow3BlurRadius);
  roundedRectangleBorderRect = CGRectOffset(roundedRectangleBorderRect, 
                                        -shadow3Offset.width, -shadow3Offset.height);
  roundedRectangleBorderRect = CGRectInset(CGRectUnion(roundedRectangleBorderRect,
                                             [roundedRectanglePath bounds]), -1, -1);

  UIBezierPath* roundedRectangleNegativePath = [UIBezierPath bezierPathWithRect:
                                             roundedRectangleBorderRect];
  [roundedRectangleNegativePath appendPath: roundedRectanglePath];
  roundedRectangleNegativePath.usesEvenOddFillRule = YES;

  CGContextSaveGState(context);
  {
    CGFloat xOffset = shadow3Offset.width 
                      + round(roundedRectangleBorderRect.size.width);
    CGFloat yOffset = shadow3Offset.height;
    CGContextSetShadowWithColor(context,
                                CGSizeMake(xOffset + copysign(0.1, xOffset),
                                             yOffset + copysign(0.1, yOffset)),
                                shadow3BlurRadius,
                                shadow3.CGColor);

    [roundedRectanglePath addClip];
    CGAffineTransform transform = 
                    CGAffineTransformMakeTranslation
                          (-round(roundedRectangleBorderRect.size.width), 0);
    [roundedRectangleNegativePath applyTransform: transform];
    [[UIColor grayColor] setFill];
    [roundedRectangleNegativePath fill];
  }
  CGContextRestoreGState(context);

  [strokeColor setStroke];
  roundedRectanglePath.lineWidth = 1.5;
  [roundedRectanglePath stroke];


  //// Cleanup
  CGGradientRelease(greenHighlight);
  CGGradientRelease(innerShadow002);
  CGColorSpaceRelease(colorSpace);

  NSLog(@"drawRect exit");

}

@end

このコードは、タイプがカスタムに設定されている UIButton に適用されています。ボタンはアプリ内の 2 つの場所 (開発が進むにつれてさらに多くの場所) にあり、1 つは UITableView の下に配置されたビュー内にあり、もう 1 つは通常の平面の Jane UIViewController 内にあります。どちらの場合も、ボタンのラベル テキストは実行時にレンダリングされますが、ボタン自体のデザインはどこにも表示されません。

PaintCode を使用して何かを描画するためのチュートリアルはたくさんありますが (これについては私は助けを必要としません)、作成したカスタム ボタン クラスを実装する方法についてはゼロです。PaintCode 以外で作成されたクラスを使用する前にこれを行ったことがあり、それが正しいと確信しています。ここでどこが間違っているのか分かりますか?

4

4 に答える 4

2

Apple Docs からの私のコメントも参照してください。

+ (id)buttonWithType:(UIButtonType)buttonType

このメソッドは、特定の構成を持つボタン オブジェクトを作成するための便利なコンストラクターです。UIButton をサブクラス化すると、このメソッドはサブクラスのインスタンスを返しません。特定のサブクラスのインスタンスを作成する場合は、ボタンを直接割り当て/初期化する必要があります。

于 2013-05-26T17:11:01.933 に答える
1

背景色/グラデーションが表示されない理由は、drawRect コードの CGPoints がハードコーディングされているためです。そのため、ストロークと塗りつぶしは、おそらくボタンの四角形で発生することはありません。

私は置き換えました:

CGRectMake(36, 913, 577, 126) と rect,

CGPointMake(324.5, 913) with rect.origin,

CGPointMake(323.5, 1012.5) と CGPointMake(rect.origin.x+ rect.size.width, rect.origin.y+rect.size.height)

背景の塗りつぶしとグラデーションをほとんど取得できます。要件に基づいて、さらに微調整を行う必要がある場合があります。

ボタンをxibに追加し、そのクラスをTGCustomConfirmButtonに変更して、ボタンを使用しました

于 2013-05-26T18:16:37.657 に答える
0

buttonWithType を避ける

TGCustomConfirmButton *button = [[TGCustomConfirmButton alloc] initWithFrame:CGRectZero];

または、このようにします

+ (TGCustomConfirmButton *)buttonWithType:(UIButtonType)type {
  return type==UIButtonTypeCustom ? [[self alloc] init] : [super buttonType:type];
}

利用方法:

TGCustomConfirmButton *button = [TGCustomConfirmButton buttonWithType:UIButtonTypeCustom];
于 2013-05-26T18:15:34.923 に答える