Domanda

I guess it's another newbie question, but looking around for a way to change buttons' images and backgrounds of a button, into xcode, after the button itself has been pressed, I only could find the setBackgroundImage:forState:UIControlStatePressed method, that looks easy but limited to the use of raster graphic.

I really cannot find how to make the same effect if the button has been made using curves and gradients, with UIBezierPath.

Thanks in advance for help

Here i insert the method i put into my Custom Button Class: CButton.m I tried to set an if.. else condition linked to the button pressure, but still no results

 - (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    

in my ViewController i've of course an IBActyion to detect the button pressure.

sorry for my lack about proper therms in explanation but i'm still very new at objc Thanks in advance again

È stato utile?

Soluzione

Use the same code

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

The state of a control

A control can have more than one state at a time. States are recognized differently depending on the control

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

Altri suggerimenti

ok, i solved removing my custom button class and implementing a "normal" UIButton setting the UIControlState as showed into the enum provided by Ramshad. I could implement also a UIBezierPath loading it into a separate UIImage* object

this is the code i used into ViewController.m at least:

  • this part to set the UIImage to the bezier object

this to implement the button change at touch:

-(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;}

and this to set behavior as the button is touched

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


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

thanks again for supporting

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top