2014-01-20 46 views
0

我想這是另一個新手問題,但是四處尋找將按鈕的圖像和按鈕背景更改爲xcode的方法,在按下按鈕之後, 可以找到setBackgroundImage:forState:UIControlStatePressed方法,這看起來很簡單,但僅限於使用光柵圖形。改變壓力後用貝塞爾曲線做出的按鈕形狀

我真的無法找到如何使同樣的效果,如果按鈕已被使用曲線和漸變而成,具有UIBezierPath。

預先感謝幫助

這裏我插入我把我的自定義按鈕的方法:CButton.m 我嘗試設置與按鈕鏈接的壓力,如果.. 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很新的提前 再次感謝

回答

0

使用相同的代碼

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

控制的狀態

的控制可以同時有一個以上的狀態。根據控制

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

THKS此設置的行爲,但我仍然不能使它工作,也許我要創建從UIBezierPath的UIImage的,但我不知道如何實現這一點。 – user3190749

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]; 

感謝支持