简体   繁体   English

IOS 6通过paintcode自定义UIView

[英]IOS 6 Custom UIView by paintcode

I am an IOS beginner. 我是IOS的初学者。

I am trying to show a custom button with code generated by paintcode. 我试图显示一个自定义按钮,其中包含由paintcode生成的代码。 I drag a View on Storyboard then set this generic view class to my custom view "GreenButton". 我在Storyboard上拖动一个View,然后将这个通用视图类设置为我的自定义视图“GreenButton”。 When I run the app, the button does not show up. 当我运行应用程序时,按钮不会显示。 I added NSLogs and could see the drawRect method worked successfully. 我添加了NSLogs,可以看到drawRect方法成功运行。

The code generated by paintcode seems to include everything to draw the button. paintcode生成的代码似乎包含了绘制按钮的所有内容。 I just use XCode to resize my view. 我只是使用XCode来调整我的视图。

Do I need to implement initWithFrame method at all? 我是否需要实现initWithFrame方法?

//
//  GreenButton.m
//  Test
//
//  Created by Tim on 13.04.2013.
//  Copyright (c) 2013 Tim. All rights reserved.
//

#import "GreenButton.h"

@implementation GreenButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // setup the initial properties of the view

    }
    return self;

}

#pragma mark - Touch event overrides


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    NSLog(@"drawRect enter");
    //// General Declarations
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = UIGraphicsGetCurrentContext();

    //// Color Declarations
    UIColor* strokeColor = [UIColor colorWithRed: 0.419 green: 0.434 blue: 0.455 alpha: 1];
    UIColor* baseColor = [UIColor colorWithRed: 0.218 green: 0.24 blue: 0.268 alpha: 1];
    CGFloat baseColorRGBA[4];
    [baseColor getRed: &baseColorRGBA[0] green: &baseColorRGBA[1] blue: &baseColorRGBA[2] alpha: &baseColorRGBA[3]];

    UIColor* upperColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.8 + 0.2) green: (baseColorRGBA[1] * 0.8 + 0.2) blue: (baseColorRGBA[2] * 0.8 + 0.2) alpha: (baseColorRGBA[3] * 0.8 + 0.2)];
    UIColor* lowerColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.9) green: (baseColorRGBA[1] * 0.9) blue: (baseColorRGBA[2] * 0.9) alpha: (baseColorRGBA[3] * 0.9 + 0.1)];
    UIColor* lightUpColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.5 + 0.5) green: (baseColorRGBA[1] * 0.5 + 0.5) blue: (baseColorRGBA[2] * 0.5 + 0.5) alpha: (baseColorRGBA[3] * 0.5 + 0.5)];
    UIColor* lightDownColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.8) green: (baseColorRGBA[1] * 0.8) blue: (baseColorRGBA[2] * 0.8) alpha: (baseColorRGBA[3] * 0.8 + 0.2)];

    //// Gradient Declarations
    NSArray* buttonGradientColors = [NSArray arrayWithObjects:
                                     (id)upperColor.CGColor,
                                     (id)lowerColor.CGColor, nil];
    CGFloat buttonGradientLocations[] = {0, 1};
    CGGradientRef buttonGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)buttonGradientColors, buttonGradientLocations);
    NSArray* overlayGradientColors = [NSArray arrayWithObjects:
                                      (id)lightUpColor.CGColor,
                                      (id)[UIColor clearColor].CGColor, nil];
    CGFloat overlayGradientLocations[] = {0, 1};
    CGGradientRef overlayGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)overlayGradientColors, overlayGradientLocations);
    NSArray* gradientColors = [NSArray arrayWithObjects:
                               (id)lightUpColor.CGColor,
                               (id)lightDownColor.CGColor, nil];
    CGFloat gradientLocations[] = {0, 1};
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);

    //// Shadow Declarations
    UIColor* buttonShadow = [UIColor blackColor];
    CGSize buttonShadowOffset = CGSizeMake(0.1, 1.1);
    CGFloat buttonShadowBlurRadius = 2;
    UIColor* textShadow = [UIColor blackColor];
    CGSize textShadowOffset = CGSizeMake(0.1, -0.1);
    CGFloat textShadowBlurRadius = 5;

    //// Abstracted Attributes
    NSString* textContent = @"Update";


    //// Back Rectangle Drawing
    UIBezierPath* backRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(48, 53, 120, 23) cornerRadius: 4];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, buttonShadowOffset, buttonShadowBlurRadius, buttonShadow.CGColor);
    CGContextBeginTransparencyLayer(context, NULL);
    [backRectanglePath addClip];
    CGContextDrawLinearGradient(context, gradient, CGPointMake(108, 53), CGPointMake(108, 76), 0);
    CGContextEndTransparencyLayer(context);
    CGContextRestoreGState(context);



    //// Rounded Rectangle Drawing
    UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(49, 54, 118, 21) cornerRadius: 3];
    CGContextSaveGState(context);
    [roundedRectanglePath addClip];
    CGContextDrawLinearGradient(context, buttonGradient, CGPointMake(108, 54), CGPointMake(108, 75), 0);
    CGContextRestoreGState(context);


    //// Overlay Drawing
    UIBezierPath* overlayPath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(48, 53, 120, 23) cornerRadius: 4];
    CGContextSaveGState(context);
    [overlayPath addClip];
    CGContextDrawRadialGradient(context, overlayGradient,
                                CGPointMake(54.72, 38.09), 10,
                                CGPointMake(108, 11.46), 102.2,
                                kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    CGContextRestoreGState(context);


    //// Text Drawing
    CGRect textRect = CGRectMake(55, 54, 104, 21);
    UIBezierPath* textPath = [UIBezierPath bezierPathWithRect: textRect];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, textShadowOffset, textShadowBlurRadius, textShadow.CGColor);
    CGContextBeginTransparencyLayer(context, NULL);
    [textPath addClip];
    CGContextDrawLinearGradient(context, buttonGradient, CGPointMake(107, 54), CGPointMake(107, 75), 0);
    CGContextEndTransparencyLayer(context);
    CGContextRestoreGState(context);

    [strokeColor setStroke];
    textPath.lineWidth = 1;
    [textPath stroke];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, textShadowOffset, textShadowBlurRadius, textShadow.CGColor);
    [[UIColor whiteColor] setFill];
    [textContent drawInRect: textRect withFont: [UIFont boldSystemFontOfSize: [UIFont systemFontSize]] lineBreakMode: NSLineBreakByWordWrapping alignment: NSTextAlignmentCenter];
    CGContextRestoreGState(context);



    //// Cleanup
    CGGradientRelease(buttonGradient);
    CGGradientRelease(overlayGradient);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);

    NSLog(@"drawRect exit");



}


@end

You do not need to implement initWithFrame: when using a Storyboard. 使用Storyboard时,您不需要实现initWithFrame: In fact, the storyboard will use initWithCoder: so it won't even be called. 实际上,故事板将使用initWithCoder:因此甚至不会被调用。

I tested your code, and it works fine, however, you are not drawing in the top left corner, but somewhere in the middle of the view, so be sure to make the view large enough if you want to see your button. 我测试了你的代码,它运行正常,但是,你没有画在左上角,而是在视图中间的某个地方,所以如果你想看到你的按钮,一定要让视图足够大。

Preferably draw in the top left corner. 最好画在左上角。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM