压力后用贝塞尔曲线改变按钮形状

时间:2014-01-20 06:11:16

标签: objective-c uibutton uibezierpath

我想这是另一个新手问题,但是在按下按钮后,四处寻找将按钮的图像和按钮背景更改为xcode的方法, 我只能找到setBackgroundImage:forState:UIControlStatePressed方法,看起来很简单但仅限于使用光栅图形。

如果使用UIBezierPath使用曲线和渐变制作按钮,我真的找不到如何产生相同的效果。

提前感谢您的帮助

这里我插入我放入自定义按钮类的方法:CButton.m 我试图设置一个if .. else条件链接到按钮压力,但仍然没有结果

 - (void)drawRect:(CGRect)rect

{

    if (UIControlEventAllTouchEvents) {


    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor* color2 = [UIColor colorWithRed:0 green:0.429 blue:0.429 alpha:1];
    UIColor* color3 = [UIColor colorWithRed:0.8 green: 0.933 blue: 1 alpha:1];



    UIColor* shadow = [color2 colorWithAlphaComponent: 0.09];
    CGSize shadowOffset = CGSizeMake(13.1, 13.1);
    CGFloat shadowBlurRadius = 0.5;

    myButton = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(8, 6.5, 185, 50.8)      cornerRadius:3.1];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, shadowOffset,shadowBlurRadius, shadow.CGColor);
    [color3 setFill];
    [myButton fill];
    CGContextRestoreGState(context);
    }
    else if (!UIControlEventAllTouchEvents) {


        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = UIGraphicsGetCurrentContext();

        UIColor* color2 = [UIColor colorWithRed:1 green:0.933 blue:0.8 alpha:1];
        UIColor* color3 = [UIColor colorWithRed:1 green:0.114 blue:0.114 alpha:1];

        NSArray* gradientColors = [NSArray arrayWithObjects:
                               (id)color2.CGColor,
                               (id)[UIColor colorWithRed:1 green:0.524 blue:0.457    alpha:1].CGColor,
                               (id)color3.CGColor,nil];
        CGFloat gradientLocations[] = {0, 0.29, 1};
        CGGradientRef gradient =  CGGradientCreateWithColors(colorSpace, (__bridge   CFArrayRef)(gradientColors), gradientLocations);
        myButton = [UIBezierPath bezierPathWithArcCenter:CGPointMake(32.2, 30) radius:0.5 startAngle:5 endAngle:50 clockwise:YES];
       [myButton moveToPoint:CGPointMake(102.5, 59)];
       [myButton addCurveToPoint:CGPointMake(88.5, 97) controlPoint1:CGPointMake(103.07, 64.7) controlPoint2:CGPointMake(88.5, 97)];
       [myButton addLineToPoint:CGPointMake(113.5, 66)];
       [myButton addLineToPoint:CGPointMake(144.5, 114)];
       [myButton addLineToPoint:CGPointMake(131.5, 59)];
       [myButton addLineToPoint:CGPointMake(211.5, 66)];
       [myButton addLineToPoint:CGPointMake(131.5, 41)];
       [myButton addLineToPoint:CGPointMake(126.5, 7)];
       [myButton addLineToPoint:CGPointMake(113.5, 41)];
       [myButton addLineToPoint:CGPointMake(77.5, 23)];
       [myButton addCurveToPoint:CGPointMake(102.5, 59) controlPoint1:CGPointMake(77.5, 23)     controlPoint2:CGPointMake(101.93, 53.3)];
        [myButton closePath];
        myButton.miterLimit = 11;

        CGContextSaveGState(context);
        [myButton addClip];
        CGContextDrawRadialGradient(context, gradient, CGPointMake(118.72, 52.63), 3.79,
                                CGPointMake(107.76, 55.37), 94.63,
                                kCGGradientDrawsBeforeStartLocation |    kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(context);

        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorSpace);
        }
    }


@end    

在我的ViewController中我当然是一个用于检测按钮压力的IBActyion。

抱歉我缺乏适当的解释,但我仍然很擅长objc 再次提前致谢

2 个答案:

答案 0 :(得分:0)

使用相同的代码

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

控件的状态

控件一次可以有多个状态。取决于控制,国家的识别方式不同

enum {
   UIControlStateNormal               = 0,
   UIControlStateHighlighted          = 1 << 0,
   UIControlStateDisabled             = 1 << 1,
   UIControlStateSelected             = 1 << 2,
   UIControlStateApplication          = 0x00FF0000,
   UIControlStateReserved             = 0xFF000000
};

答案 1 :(得分:0)

好的,我解决了删除我的自定义按钮类并实现“普通”UIButton设置UIControlState,如Ramshad提供的枚举所示。 我也可以实现一个UIBezierPath将它加载到一个单独的UIImage *对象

这是我至少在ViewController.m中使用的代码:

  • 此部分将UIImage设置为贝塞尔对象

这样可以在触摸时实现按钮更改:

-(UIImage*) preTouch{




CGRect rect = CGRectMake(0.0, 0.0, 100.0, 100.0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContext(rect.size);

UIColor* color2 = [UIColor colorWithRed:1 green:0.933 blue:0.8 alpha:1];
UIColor* color3 = [UIColor colorWithRed:1 green:0.114 blue:0.114 alpha:1];

NSArray* gradientColors = [NSArray arrayWithObjects:
                           (id)color2.CGColor,
                           (id)[UIColor colorWithRed:1 green:0.524 blue:0.457 alpha:1].CGColor,
                           (id)color3.CGColor,nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient =  CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)(gradientColors), gradientLocations);
UIBezierPath* newBezier = [UIBezierPath bezierPath];//]WithArcCenter:CGPointMake(32.2, 30) radius:0.5 startAngle:5 endAngle:50 clockwise:YES];
[newBezier moveToPoint:CGPointMake(31.48, 59.3)];

[newBezier addLineToPoint:CGPointMake(17.81, 65.66)];
[newBezier addLineToPoint:CGPointMake(39.29, 67.25)];
[newBezier addLineToPoint:CGPointMake(51, 89.5)];
[newBezier addLineToPoint:CGPointMake(56.86,68.84)];
[newBezier addLineToPoint:CGPointMake(92, 59.3)];
[newBezier addLineToPoint:CGPointMake(63.69, 50.56)];
[newBezier addLineToPoint:CGPointMake(76.38, 0.5)];
[newBezier addLineToPoint:CGPointMake(51,45)];
[newBezier addLineToPoint:CGPointMake(31.48,16.39)];
[newBezier addLineToPoint:CGPointMake(39.29,45)];
[newBezier addLineToPoint:CGPointMake(10,35.46)];
[newBezier addLineToPoint:CGPointMake(31.48,59.3)];


[newBezier closePath];
newBezier.miterLimit = 18;

[color3 setFill];
[newBezier fill];

CGContextSaveGState(context);
[newBezier addClip];
CGContextDrawRadialGradient(context, gradient, CGPointMake(118.72, 52.63), 3.79,
                            CGPointMake(107.76, 55.37), 94.63,
                            kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);

CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
CGContextAddPath(context, newBezier.CGPath);
UIImage *buttonImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
UIGraphicsEndImageContext();


return buttonImage1;}

这是在触摸按钮时设置行为

- (IBAction)buttonPressed: (UIButton*) sender{


UIImage* buttonImage = [[UIImage alloc]init];
buttonImage = [self preTouch];
UIImage* myImage = buttonImage;
[newButton setBackgroundImage: myImage forState:1 <<0];

再次感谢支持