美文网首页
UIButton长按背景颜色更改

UIButton长按背景颜色更改

作者: tobin_ | 来源:发表于2017-03-16 09:48 被阅读191次

从新浪博客迁移
方法一:

[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长按背景颜色更改

      本文链接:https://www.haomeiwen.com/subject/qeednttx.html