UIButton长按背景颜色更改

从新浪博客迁移
方法一:

[self.quitBtn addTarget:self action:@selector(quitBtnAction:) forControlEvents:UIControlEventTouchUpInside];

[self.quitBtn addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];

- (void)quitBtnAction:(UIButton*)sender{

[self.quitBtn setBackgroundColor:kProjectColorBlue];

}

- (void)changeColor:(UIButton*)sender{

self.quitBtn.backgroundColor = kProjectColorOrange;

}

方法二:

[self.commitBtn setBackgroundImage:[self buttonImageFromColor:kProjectColorOrange]forState:UIControlStateHighlighted];

- (UIImage*)buttonImageFromColor:(UIColor*)color{

CGRectrect = CGRectMake(0,0,100,30);

UIGraphicsBeginImageContext(rect.size);

CGContextRefcontext= UIGraphicsGetCurrentContext();  CGContextSetFillColorWithColor(context,[color CGColor]);

CGContextFillRect(context,rect);

UIImage*img= UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnimg;

}

你可能感兴趣的:(UIButton长按背景颜色更改)