我想這是另一個新手問題,但是四處尋找將按鈕的圖像和按鈕背景更改爲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很新的提前 再次感謝
THKS此設置的行爲,但我仍然不能使它工作,也許我要創建從UIBezierPath的UIImage的,但我不知道如何實現這一點。 – user3190749